串行通信:在传输过程中,0x0D被替换为0x0A。

4

我正在使用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;

有没有想过是哪个选项在接收过程中导致了这种数据转换?

2
寻找文本过滤器。换行符/回车符转换。 - Yakk - Adam Nevraumont
发送方是否在进行0xD到0xA的替换? - chux - Reinstate Monica
1个回答

7

您重置了ONLCR/OCRNL标志以禁用输出处理,但似乎忘记重置输入的反向标志(INLCR/ICRNL)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接