Serial communication: 0x0D is replaced by 0x0A during transmission

I am reading some data from a serial interface using Linux. From time to time, there is 0x0D in the data stream. On the receiver side, this value is replaced with 0x0A. This seems like the desired behavior - unfortunately, this is undesirable in my case, and I think this is due to one of the options set when opening the port:

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;

Any idea which of these options causes data conversion during reception?

+4
source share
1 answer

You reset the flags ONLCR/OCRNLto disable output processing, but you seem to have missed resetting the return flags for input ( INLCR/ICRNL).

+6
source

Source: https://habr.com/ru/post/1535094/


All Articles