gpt4 book ai didi

c - 用c从串口读取值

转载 作者:行者123 更新时间:2023-11-30 19:44:46 25 4
gpt4 key购买 nike

我正在从串行端口获取数据:

i = 0   7e 126
i = 1 00 0
i = 2 02 2
i = 3 ffffff8a 4294967178
i = 4 03 3
i = 5 72 114
i = 6 7e 126
i = 7 00 0
i = 8 1d 29
i = 9 ffffff80 4294967168
i = 10 00 0
i = 11 7d 125
i = 12 33 51
i = 13 ffffffa2 4294967202
i = 14 00 0
i = 15 40 64
i = 16 ffffffc2 4294967234
i = 17 40 64
i = 18 30 48
i = 19 27 39
i = 20 00 0
i = 21 23 35
i = 22 32 50
i = 23 36 54
i = 24 2e 46
i = 25 31 49
i = 26 32 50
i = 27 23 35
i = 28 34 52
i = 29 33 51
i = 30 2e 46
i = 31 32 50
i = 32 31 49
i = 33 23 35
i = 34 30 48
i = 35 2e 46
i = 36 35 53
i = 37 36 54
i = 38 23 35
i = 39 ffffffeb 4294967275
i = 40 7e 126
i = 41 00 0
i = 42 02 2
i = 43 ffffff8a 4294967178
i = 44 03 3
i = 45 72 114
i = 46 00 0
i = 47 30 48
i = 48 30 48
i = 49 30 48
i = 50 30 48
i = 51 30 48
i = 52 30 48
i = 53 30 48
i = 54 30 48
i = 55 30 48
i = 56 30 48
i = 57 30 48
i = 58 30 48
i = 59 30 48
i = 60 30 48
i = 61 30 48
i = 62 30 48
i = 63 30 48
i = 64 30 48
i = 65 30 48
i = 66 30 48
i = 67 30 48
i = 68 30 48
i = 69 30 48
i = 70 30 48
i = 71 30 48
i = 72 30 48
i = 73 30 48
i = 74 30 48
i = 75 30 48
i = 76 30 48
i = 77 30 48
i = 78 30 48
i = 79 30 48
i = 80 30 48
i = 81 30 48
i = 82 30 48
i = 83 30 48
i = 84 30 48
i = 85 30 48
i = 86 30 48
i = 87 30 48
i = 88 30 48
i = 89 30 48
i = 90 30 48
i = 91 30 48
i = 92 30 48
i = 93 30 48
i = 94 30 48
i = 95 30 48
i = 96 30 48
i = 97 30 48
i = 98 30 48
i = 99 30 48
i = 100 30 48
i = 101 30 48
i = 102 30 48
i = 103 30 48
i = 104 30 48
i = 105 30 48
i = 106 30 48
i = 107 30 48
i = 108 30 48
i = 109 30 48
i = 110 30 48
i = 111 30 48
i = 112 30 48
i = 113 30 48
i = 114 30 48
i = 115 30 48
i = 116 30 48
i = 117 30 48
i = 118 30 48
i = 119 30 48
i = 120 30 48
i = 121 30 48
i = 122 30 48
i = 123 30 48
i = 124 30 48
i = 125 30 48
i = 126 30 48
i = 127 30 48
i = 128 30 48
i = 129 30 48
i = 130 30 48
i = 131 30 48
i = 132 30 48
i = 133 30 48
i = 134 30 48
i = 135 30 48
i = 136 30 48
i = 137 30 48
i = 138 30 48
i = 139 30 48
i = 140 30 48
i = 141 30 48
i = 142 30 48
i = 143 30 48
i = 144 30 48
i = 145 30 48
i = 146 30 48
i = 147 30 48
i = 148 30 48
i = 149 30 48
i = 150 30 48
i = 151 30 48
i = 152 30 48
i = 153 30 48
i = 154 30 48
i = 155 30 48
i = 156 30 48
i = 157 30 48
i = 158 30 48
i = 159 30 48
i = 160 30 48
i = 161 30 48
i = 162 30 48
i = 163 30 48
i = 164 30 48
i = 165 30 48
i = 166 30 48
i = 167 30 48
i = 168 30 48
i = 169 30 48
i = 170 30 48
i = 171 30 48
i = 172 30 48
i = 173 30 48
i = 174 30 48
i = 175 30 48
i = 176 30 48
i = 177 30 48
i = 178 30 48
i = 179 30 48
i = 180 30 48
i = 181 30 48
i = 182 30 48
i = 183 30 48
i = 184 30 48
i = 185 30 48
i = 186 30 48
i = 187 30 48
i = 188 30 48
i = 189 30 48
i = 190 30 48
i = 191 30 48
i = 192 30 48
i = 193 30 48
i = 194 30 48
i = 195 30 48
i = 196 30 48
i = 197 30 48
i = 198 30 48
i = 199 30 48
i = 200 30 48
i = 201 30 48
i = 202 30 48
i = 203 30 48
i = 204 30 48
i = 205 30 48
i = 206 30 48
i = 207 30 48
i = 208 30 48
i = 209 30 48
i = 210 30 48
i = 211 30 48
i = 212 30 48
i = 213 30 48
i = 214 30 48
i = 215 30 48
i = 216 30 48
i = 217 30 48
i = 218 30 48
i = 219 30 48
i = 220 30 48
i = 221 30 48
i = 222 30 48
i = 223 30 48
i = 224 30 48
i = 225 30 48
i = 226 30 48
i = 227 30 48
i = 228 30 48
i = 229 30 48
i = 230 30 48
i = 231 30 48
i = 232 30 48
i = 233 30 48
i = 234 30 48
i = 235 30 48
i = 236 30 48
i = 237 30 48
i = 238 30 48
i = 239 30 48
i = 240 30 48
i = 241 30 48
i = 242 30 48
i = 243 30 48
i = 244 30 48
i = 245 30 48
i = 246 30 48
i = 247 30 48
i = 248 30 48
i = 249 30 48
i = 250 30 48
i = 251 30 48
i = 252 30 48
i = 253 30 48
i = 254 30 48

它是 255 缓冲区的打印结果。在此缓冲区中,7e 126 是 i = 0 处的起始位。结束位是该缓冲区的 254 地址处的 30 48。现在我想从 i = 24 打印到 i = 40。这是我用于读取串行数据的代码:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>

#define BAUDRATE B115200
#define MODEMDEVICE "/dev/ttyUSB1"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

main()
{
int fd,c, res, i;
struct termios oldtio,newtio;
char buf[255];

fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) {perror(MODEMDEVICE); exit(-1); }

tcgetattr(fd,&oldtio); /* save current port settings */

memset(&newtio,'0', sizeof(newtio));
//bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;

/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;

//newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
//newtio.c_cc[VMIN] = 5; /* blocking read until 5 chars received */

tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);

int k = 7e 126;
int m = 30 48;
while (STOP==FALSE) { /* loop for input */
memset(buf,'0', 255);
res = read(fd,buf,255); /* returns after 5 chars have been input */
buf[res]=0; /* so we can printf... */
//printf("%d", res);
//printf(":%s:%d\n", buf, res);
for(i = 0; i<255; i++)
if(i[0] == k && i[254] == m)
{
for(i = 24; i<25; i++)
printf("i = %d %02x %u\n", i, buf[i], buf[i]);
}
//printf("");
if (buf[0]=='z') STOP=TRUE;
}

tcsetattr(fd,TCSANOW,&oldtio);
}

此代码能够打印所有值,但在我尝试打印该特定范围后无法编译。有人可以帮我吗?

编辑

serasync.c:22:42: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
if (fd <0) {perror(MODEMDEVICE); exit(-1); }
^
serasync.c:26:9: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by default]
memset(&newtio,'0', sizeof(newtio));
^
serasync.c:41:17: error: exponent has no digits
int k = 7e 126;
^
serasync.c:41:20: error: expected ‘,’ or ‘;’ before numeric constant
int k = 7e 126;
^
serasync.c:42:13: error: expected ‘,’ or ‘;’ before numeric constant
int m = 30 48;
^
serasync.c:50:6: error: subscripted value is neither array nor pointer nor vector
if(i[0] == k && i[254] == m)
^
serasync.c:50:19: error: subscripted value is neither array nor pointer nor vector
if(i[0] == k && i[254] == m)

最佳答案

      for(i = 0; i<255; i++)
if(i[0] == k && i[254] == m)

由于 iint 而不是数组或指针,因此 i[0]i[254]没有任何意义。也许您的意思是 buf[i]

坦率地说,你似乎不懂 C。像 int m = 30 48; 这样的东西根本没有任何意义。我什至无法想象你认为这意味着什么。 “30 48”是什么整数?

关于c - 用c从串口读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27400868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com