我正在使用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;
知道哪些选项会在接收期间导致此数据转换?
答案 0 :(得分:7)
您重置ONLCR/OCRNL
标志以禁用输出处理,但您似乎错过了重置输入(INLCR/ICRNL
)的反向标志。