pythonlinuxserial-portpyserial

Persistent name of USB serial device with Python pyserial


I have a Python application that chose serial device using pyserial serial.tools.list_ports

I have two USB to RS485 serial adapter connected. They won't be disconnected from the server and the server has never been restarted (it's a Linux Ubuntu 22.04.3 LTS)

For some reason every time I chose to use one of the two serial, it randomly pick one even if I always choose, for istance, /dev/ttyUSB0

To me, it looks strange because, the server is never restarted and the USB device is never removed from it so, technically, if I use the serial port mapped to /dev/ttyUSB0 it should always be the same

Is there a way to name serial ports in a persistent way?


Solution

  • The solution was to check the actual serial number of the USBs attached with

    sudo lsusb -v | grep 'idVendor\|idProduct\|iSerial'
    

    Then you need to create the file with

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

    And add the USB you need with

    SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="FTZ5X8NG", SYMLINK+="USB-0"
    

    where ATTRS{serial}=="FTZ5X8NG" is the unique identifier of the USB and SYMLINK+="USB-0" is the unique name you would like to give to it