Alan C. Assis

Configuring Linux’s serial port to raw mode

I was facing a strange issue when debugging a serial communication from a microcontroller on a Linux machine.

I was using this command to print modem communication:

$ cat /dev/ttyUSB0 | hexdump -v -e '/1 "%02X "' -e '/1 "%c\n"'

41 A
54 T
5A Z
0A 
0A 

The microcontroller was sending “ATZ\r\n” but in the Linux I saw only “ATZ\n\n”, initially I suspected that RTOS NuttX in the microcontroller was converting the “\r” (0x0D) to “\n” (0x0A).

After some low level debug I verified that NuttX was writing “\r” correctly in the serial port’s buffer.

Then I realized the guilt should be the Linux. In fact Linux handles serial port as a TTY interface. It means that Linux will do some post processing of serial data.

Fortunately we can change this behavior configuring the serial port to RAW mode, this way:

$ sudo stty -F /dev/ttyUSB0 raw
$ sudo stty -F /dev/ttyUSB0 -echo -echoe -echok
$ sudo stty -F /dev/ttyUSB0 115200

Now everything goes fine:

$ cat /dev/ttyUSB0 | hexdump -v -e '/1 "%02X "' -e '/1 "%c\n"'

41 A
54 T
5A Z
0D 
0A 

Source: http://www.armadeus.com/wiki/index.php?title=Serial_ports_usage_on_Linux