Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm reading some data from serial interface using Linux. From time to time there is a 0x0D within the data stream. On receiver side this value is replaced by 0x0A. This looks like a desired behaviour - unfortunately it is not desired in my case and I think it has to do with one of the options set during 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 this data conversion during reception?

like image 379
Elmi Avatar asked Mar 19 '23 23:03

Elmi


1 Answers

You reset the ONLCR/OCRNL flags to disable output processing, but you seem to miss resetting the reverse flags for input (INLCR/ICRNL).

like image 57
mfro Avatar answered Apr 07 '23 01:04

mfro