gutelfuldead
gutelfuldead

Reputation: 602

Linux inserting carriage return-linefeed instead of just linefeed

I am writing ASCII commands over a UART interface in an embedded linux system.

I tested the code with a laptop to begin with. Both the embedded platform and the laptop share a ftdi usb dongle. The code should be identical on the two platforms (I thought).

I am creating a string and sending it like so,

char cmd[MAX_STR_LEN];
sprintf(cmd,"r %02x %02x\n",chipAddr,naddr);
ssize_t bytes_tx = write(fd, (char *)cmd, strlen(cmd));
if (bytes_tx < 0){
    ERROR_PRINT("%s\n",strerror(errno));
    return -1;
}

When I monitor the output of the serial device on an oscilloscope when running the code from the PC I see that \n is correctly interpreted as 0x0a.

When I run the same code snippet on the embedded platform I see \n is being interpreted as 0x0d 0x0a or \r\n.

How do I force the embedded platform to send only \n or 0x0a? The embedded platform is running Linux Kernel 4.4.x generated with buildroot.

--- UPDATED Full Test Code

Placed full code here : https://pastebin.com/5dWhaaDv

result on both embedded and laptop targets,

Connected to /dev/ttyUSB0
strlen 8 : r 40 00

tx cmd[0] = 72 r
tx cmd[1] = 20  
tx cmd[2] = 34 4
tx cmd[3] = 30 0
tx cmd[4] = 20  
tx cmd[5] = 30 0
tx cmd[6] = 30 0
tx cmd[7] = 0a 

bytes_tx = 8
sizeof(\n) : 4

Upvotes: 3

Views: 373

Answers (1)

gutelfuldead
gutelfuldead

Reputation: 602

The issue was with not properly setting the termios struct flag : c_oflag

From the termios docs ( https://en.wikibooks.org/wiki/Serial_Programming/termios )

 //
 // Output flags - Turn off output processing
 //
 // no CR to NL translation, no NL to CR-NL translation,
 // no NL to CR translation, no column 0 CR suppression,
 // no Ctrl-D suppression, no fill characters, no case mapping,
 // no local output processing
 //
 // config.c_oflag &= ~(OCRNL | ONLCR | ONLRET |
 //                     ONOCR | ONOEOT| OFILL | OLCUC | OPOST);
 config.c_oflag = 0;

It was apparently initialized on one platform but not the other. Thanks to @KamilCuk and @EugeneSh. for finding the issue.

Upvotes: 1

Related Questions