Menu
Cart

Persistent USB Serial Connections on Linux

If you use USB serial adapter devices regularly you will find that operating systems do not assign a particular adapter to the same device name consistently. This is especially challenging if you use multiple devices. If we are setting up a home automation system using serial devices and we program it to look for /dev/ttyUSB0 but at some point in the future the serial adapter we are using comes up instead as /dev/ttyUSB1 our home automation system will come to a crashing halt.

Fortunately there is a way to have a persistent device name assigned to the adapter we want. The adapters that Engimusing products use all have unique serial numbers.

We first need to identify the serial number of the device we are using. Using the following commands in a terminal will tell us the vendor id, product id, and serial number if we know the devices current name. In this example we use /dev/ttyUSB1 but replace it with the actual name of your device.

udevadm info -a -n /dev/ttyUSB1 | grep '{idVendor}' | head -n1

With the adapter I am using this command reports:

ATTRS{idVendor}=="0403"

Then run:

udevadm info -a -n /dev/ttyUSB1 | grep '{idProduct}' | head -n1

which reports:

ATTRS{idProduct}=="6015"

Then run:

udevadm info -a -n /dev/ttyUSB1 | grep '{serial}' | head -n1

which reports:

ATTRS{serial}=="DA01DCO1"

You will see different values for one or more of these values. 

We need these three values for a file we are going to create. For this we need to use a text editor. For this example I will use nano as it is installed on most Linux systems by default. Start by typing:

sudo nano /etc/udev/rules.d/99-usb-serial.rules

A window like this should open:

Nano editor screen 1

If this didn't work for you use google to find an editor on your system or install nano. You need to type or copy the following line replacing the three values you found for your device. You also need to put the name you want following where it says SYMLINK. This must all be typed on one line or it won't work.

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", ATTRS{serial}=="DA01DCO", SYMLINK+="ttyMQTT0"

So you nano window should look like this now (with your values):

nano2

If you have more than one USB serial device you should add a line for each adapter.

So check it carefully to be sure it is correct and then write out the file by typing <CTRL>+o (hold the Ctrl key down and type 'o').

You should then  see the [ Wrote 1 line ] message above the menu. You can then close nano by typing <CTRL>+x.

If you disconnect and reconnect your USB serial device and type:

ls -l /dev/ttyMQTT0

 you will see something like this:

lrwxrwxrwx 1 root root 7 Feb 2 12:11 /dev/ttyMQTT0 -> ttyUSB0

Of course, you will have had to substitute the name you used if you didn't use ttyMQTT0. So ttyMQTT0 is a symbolic link to ttyUSB0. The system still assigns the adapter to its normal name but we have a new name linked to the normal name.

If you look for ttyMQTT0 in the Arduino IDE it will not show up. If you are using the IDE with multiple boards and want them to always have the same name in the IDE you can use the above method with a change in the way you name the link as shown here.