Kenny's Blog

23 Mar 2021

Fixed serial port paths by id

Sometimes, it’s useful to have a fixed serial port path, in case you have a script running on a machine that uses a serial port that is connected by usb. Often times if you’re using a usb-serial converter (like those from FTDI), the path can change when you physically reconnect the device. This can be the case even if you’re reconnecting it to the same USB port.

For example, /dev/ttyUSB0 can turn into /dev/ttyUSB1, even with the same device and usb port.

You can get a fixed path by using the /dev/serial/by-id path:

$ ls -lah /dev/serial/by-id
total 0
drwxr-xr-x 2 root root 80 Mar 21 18:42 .
drwxr-xr-x 4 root root 80 Mar 21 18:20 ..
lrwxrwxrwx 1 root root 13 Mar 21 18:20 usb-FTDI_FT231X_USB_UART_XXXXXXXX-if00-port0 -> ../../ttyUSB0
lrwxrwxrwx 1 root root 13 Mar 21 18:42 usb-FTDI_FT232R_USB_UART_XXXXXXX2-if00-port0 -> ../../ttyUSB1

The linux kernel automatically adds these symlinks when you connect these devices. The paths should be unique, although I’ve heard that chinese clones of certain devices may not flash unique serial numbers at the factory. If you’re using genuine ftdi / other devices, this shouldn’t be a problem.