Linux – Wrong newline character over serial port (CR instead of LF)

linuxnewlinesserial port

I'm trying to talk to a device using a usb-to-serial converter on linux. Drivers are working, the device exists at /dev/ttyUSB0.

There is a strange problem (checked with a null-modem cable to another computer): Whatever program I use to connect to the device (putty, minicom, screen), they all send a CR (\r) on enter. I (and my device) expect a LF (\n) on enter.

When I use the console to send a command – it sends a LF:

# echo Hello World > /dev/ttyUSB0

Results in "Hello World\n".
I also wrote the classic c hello world program (printf("Hello, world!\n");) and redirected the output to /dev/ttyUSB0 – also gives me a "Hello world\n" on the other end of the line.

But all other terminal programs send a \r on enter.

What's going on here?

Best Answer

Please ensure you have set the ttyUSBx port in raw mode. Otherwise there can be some character replacements e.g. \r with \n and vice versa on receiver side.