我正在使用Linux从串口读取一些数据。时不时地,数据流中会出现0x0D。在接收端,这个值被替换为0x0A。这看起来是一种期望的行为 - 不幸的是,在我的情况下并不是期望的行为,我认为这与打开端口时设置的选项有关:
struct termios options;
struct serial_struct sStruct;
*fd= open(serialParams->port, O_RDWR|O_NOCTTY);// | O_NDELAY);
if (*fd == -1) return OAPC_ERROR_DEVICE;
fcntl(*fd, F_SETFL,FNDELAY);
tcgetattr(*fd, &options);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CSIZE; // Mask the character size bits
options.c_cflag |= CS8;
options.c_cflag &= ~(PARENB|PARODD);
options.c_iflag &= ~(INPCK | ISTRIP);
options.c_iflag |=IGNPAR;
options.c_cflag&=~CSTOPB;
options.c_iflag |= (IXON | IXOFF | IXANY);
options.c_cflag &= ~CRTSCTS;
options.c_lflag &= ~(ICANON | ECHO | ECHOE |ECHOK|ISIG|IEXTEN|ECHONL);
options.c_iflag&=~(IGNCR|IUTF8);
options.c_oflag&=~(ONLCR|OCRNL);
ioctl(*fd, TIOCGSERIAL, &sStruct);
sStruct.flags &= ~ASYNC_SPD_MASK;
ioctl(*fd, TIOCSSERIAL, &sStruct);
int speed;
speed=1000000;
ioctl(*fd, TIOCGSERIAL, &sStruct);
sStruct.flags = (sStruct.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST;
sStruct.custom_divisor = (sStruct.baud_base + (speed / 2)) / speed;
ioctl(*fd, TIOCSSERIAL, &sStruct);
cfsetispeed(&options, B38400);
cfsetospeed(&options, B38400);
if (tcsetattr(*fd, TCSANOW, &options)!=0) return OAPC_ERROR_DEVICE;
有没有想过是哪个选项在接收过程中导致了这种数据转换?