Serial Port Tips for Linux

Addressing a Serial Port
Built-in serial ports tend to get the same device filename everytime, like /dev/ttyS0 for the first one, /dev/ttyS1 for the second one, and so on.

However, USB virtual serial ports get a different filename each time, depending on the kind of device and the order with which the USB ports are connected to the system. Typically, such device filenames look like /dev/ttyACM0 or /dev/ttyUSB0, but such names are often unpredictable.

You have several options:


 * Manually inspect the tty* files under /dev, in order to find out which one gets created when a particular USB device is connected.
 * Use a filename under /dev/serial/by-id or /dev/serial/by-path.
 * Use a script like FindUsbSerialPort.sh in order to find a USB serial port filename by manufacturer name, serial number, etc.
 * Write a UDEV rules file that assigns a fixed filename to your particular USB device.

Configuring a Serial Port under Linux
Set the default configuration with stty to 9600 bps, 8N1, no flow control:

stty -F /dev/serial_port cs8 -parenb -cstopb -clocal raw speed 9600 # What the arguments mean: #  cs8:     8 data bits #  -parenb: No parity (because of the '-') #  -cstopb: 1 stop bit (because of the '-') #  -clocal: Disable modem control signals (no hardware flow control)

Opening a Serial Port
Say you have the following scenario:
 * Your device reacts to single keypresses. Therefore, you do not want line editing on the local side, because characters need to be sent straight away. An example device would be the Bus Pirate.
 * You want to use a standard text console.
 * You want to terminate the connection when you press Ctrl+C.

You can connect to your device with the following command:

socat -t0 STDIO,raw,echo=0,escape=0x03,nonblock=1  /dev/mydevice,b115200,cs8,parenb=0,cstopb=0,clocal=0,raw,echo=0,setlk,flock-ex-nb,nonblock=1 # # #
 * 1) What the arguments mean:
 * 1) -t0: When terminating socat with Ctrl+C, do not wait for a short time,
 * 2)      but exit immediately.
 * 1) On the STDIO side:
 * 2)   raw: Do not interpret special codes.
 * 3)   echo=0: Do not print the input characters locally. The remote end
 * 4)           usually does that.
 * 5)   escape=0x03: Ctrl+C terminates socat (with an exit code of 0, indicating success).
 * 6)                If you wish to pass Ctrl+C to the remote end, you can switch to Ctrl+O
 * 7)                with escape=0x0F.
 * 8)   nonblock=1: Just in case, specify this on the STDIO side too. See comments on the
 * 9)               same parameter on the /dev/mydevice side below for more information.
 * 1) On the /dev/mydevice side:
 * 2)   b115200: The serial port speed.
 * 3)   cs8,parenb=0,cstopb=0,clocal=0: The usual 8N1 configuration, see the stty example above.
 * 4)   raw,echo=0: matches the configuration on the STDIO side.
 * 5)   setlk: Write-lock the whole file, see below for more information on exclusive locks.
 * 6)   flock-ex-nb: Similar to setlk, see below for more information on exclusive locks.
 * 7)   nonblock=1: I had trouble with an "Aten USB-to-Serial Converter (RS-232)",
 * 8)               Model UC-232A. socat did not work, but picocom and others did, and the fix
 * 9)               was to specify nonblock, which looks like a reasonable option anyway.
 * 10)               Shouldn't all full-duplex, async I/O be non-blocking anyway?
 * 11)               I have seen this with a 'ch341-uart' too, so it may not be a device-specfic
 * 12)               issue after all.

If the backspace key does not work, try using Ctrl+H instead.

Depending on your device, you may need to add "crnl" to either the STDIO or the tty side, in order to convert the line termination characters properly.

Alternatives to socat

 * picocom -b 115200 -p n -d 8 /dev/serial-port-device  Exit with Ctrl+A, Ctrl+X.
 * minicom -b 115200 -8 -D /dev/serial-port-device
 * screen /dev/serial-port-device 115200 Exit with Ctrl+A, '\'.
 * C-Kermit
 * cutecom
 * gtkterm -s 115200 -p /dev/serial-port-device
 * putty -serial COM8 # Only on Windows.
 * plink # Only on Windows.

Printing Timestamps
Here are some alternatives:


 * socat's option -v generates an extra output log to stderr which includes timestamps.


 * Tool ts (see Ubuntu package moreutils) copies stdin to stdout prepending timestams. Usage example:  unbuffer $COMMAND | ts "%F %H:%M:%.S" Unfortunately, if you are using socat, you will have to remove "icanon=0,echo=0" from the STDIO side, which may have unpleasant side-effects.


 * Tool grabserial reads lines from a serial port and writes them to a standard output, optionally prepending timestamps, which allows you to easily time processes, like booting an embedded operating system.

No exclusive lock when accessing serial ports
I was surprised to learn that several Linux processes can open a given serial port at the same time. This usually makes a mess of the data. If you make a mistake and try to use the same port from several concurrent scripts, it may take a while to figure out why the data is getting chopped and mixed up in random ways.

There is no exclusive lock when opening serial port files, like there is under Windows. As far as I know, there is no way to enforce such a protection against concurrent access at system level. If you do know how to achieve that, please drop me a line!

There is a work-around if all processes cooperate. Tool socat implements arguments setlk, which uses "fcntl(fd, F_SETLK, ...)", and argument flock-ex-nb, which uses "flock(fd, LOCK_EX|LOCK_NB)". Interestingly enough, those two file-locking mechanisms seem to be independent from each other, at least under Linux. If a process uses one method, then other processes that use the same method will realise that the file is already locked. However, processes that do not use any locking method will not be affected at all.