How to identify multiple USB-serial adapters under Ubuntu 10.1

后端 未结 7 1335
无人共我
无人共我 2020-12-29 13:54

I am reading data from multiple identical USB-serial adapters under Ubuntu 10.1.

On occasion, their /dev/tty path changes (eg if other USB devices are connected on s

相关标签:
7条回答
  • 2020-12-29 14:07

    I too was searching this topic for a way to find which physical USB device was assigned/connected to a logical /dev device name. So, after some trial and error, this is what worked best for me:

    See what logical ttyUSBx devices exist (where x is 0, 1, 2...):

    $ ls /dev
    

    Show bus and device numbers for all usb-serial adapters:

    $ lsusb 
    

    Finally, use:

    $ udevadm info --name=ttyUSBx --attribute-walk | grep num  
    

    Now inspect the udevadm output to match the logical device name to the actual physical device. Here my listing when I did it:

    $ lsusb
    Bus 002 Device 005: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial     (UART) IC
    Bus 002 Device 004: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
    Bus 002 Device 002: ID 80ee:0021  
    Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    
    $ udevadm info --name=ttyUSB0 --attribute-walk | grep num
        ATTRS{port_number}=="0"
        ATTRS{urbnum}=="812"
        ATTRS{busnum}=="2"
        ATTRS{devnum}=="5"
        ATTRS{urbnum}=="115"
        ATTRS{busnum}=="2"
        ATTRS{devnum}=="1"
        ATTRS{numa_node}=="-1"
    
    $ udevadm info --name=ttyUSB1 --attribute-walk | grep num
        ATTRS{port_number}=="0"
        ATTRS{urbnum}=="465"
        ATTRS{busnum}=="2"
        ATTRS{devnum}=="4"
        ATTRS{urbnum}=="115"
        ATTRS{busnum}=="2"
        ATTRS{devnum}=="1"
        ATTRS{numa_node}=="-1"
    

    So, in my case, ttyUSB0 is associated with the device on bus2, device5, which is the Future Technology Devices International USB to Serial Adapter; and likewise, ttyUSB1 is associated with the device on bus2, device4, which is the Prolific Technology, Inc. USB to Serial adapter.

    And as has been pointed out, the command:

    $ usb-devices
    

    Will get you the same info in a one-line manner. I thought I'd post the details that helped me learn how the stuff worked behind the scenes...

    Hope that was helpful :)

    0 讨论(0)
  • 2020-12-29 14:12

    I have many USB to Serial devices with each many ports and the solutions mentioned above did not quite did it for me.

    The USB "KERNEL" was not enough in my case, but I found the port number.

    I am aware that what I'm proposing now might be considered an insane hack, but it works for me..
    for now..

    I would actually be pleased to see a more elegant suggestion that accomplish something similar..

    So... Based on the previous answer form Ilya Matveychikov

    File: /etc/udev/rules.d/usb-parse-devpath.sh

    #!/bin/bash
    
    DEVNUM=$(echo "${1}" | rev | cut -d'/' -f4 | rev | tr -d '-' | tr -d '.' | tr -d ':')
    PORTNUM=$(/sbin/udevadm info -a --path=${1} | grep "ATTRS{port_number}" | head -1 | cut -d'"' -f2)
    PRODUCT=$(/sbin/udevadm info -a --path=${1} | grep "ATTRS{product}" | head -1 | cut -d'"' -f2 | tr -d '/' | tr ' ' '_')
    
    NEWID="ttyUSB_${PRODUCT}_${DEVNUM}${PORTNUM}"
    
    #echo "${NEWID} :: $1" >> /tmp/DEBUG_udev.txt
    
    echo "$NEWID"
    

    And File: /etc/udev/rules.d/99-usb-serial.rules

    ACTION=="add", KERNEL=="ttyUSB[0-9]*", PROGRAM="/etc/udev/rules.d/usb-parse-devpath.sh %p", SYMLINK+="TTYUSBs/%c"
    

    The result look something like this:

    # ls -l /dev/TTYUSBs
    
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_435100 -> ../ttyUSB20
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_435101 -> ../ttyUSB21
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_435102 -> ../ttyUSB22
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_435103 -> ../ttyUSB23
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_435104 -> ../ttyUSB24
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_435105 -> ../ttyUSB25
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_435106 -> ../ttyUSB26
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_435107 -> ../ttyUSB27
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_436100 -> ../ttyUSB28
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_436101 -> ../ttyUSB29
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_436102 -> ../ttyUSB30
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_436103 -> ../ttyUSB31
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_436104 -> ../ttyUSB32
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_436105 -> ../ttyUSB33
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_436106 -> ../ttyUSB34
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport416_436107 -> ../ttyUSB35
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport4_4341100 -> ../ttyUSB38
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport4_4341101 -> ../ttyUSB39
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport4_4342100 -> ../ttyUSB36
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport4_4342101 -> ../ttyUSB37
    lrwxrwxrwx. 1 root root 10 Jan  5 18:46 ttyUSB_Edgeport8_421100 -> ../ttyUSB2
    lrwxrwxrwx. 1 root root 10 Jan  5 18:46 ttyUSB_Edgeport8_421101 -> ../ttyUSB3
    lrwxrwxrwx. 1 root root 10 Jan  5 18:46 ttyUSB_Edgeport8_422100 -> ../ttyUSB4
    lrwxrwxrwx. 1 root root 10 Jan  5 18:46 ttyUSB_Edgeport8_422101 -> ../ttyUSB5
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport8_423100 -> ../ttyUSB18
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Edgeport8_423101 -> ../ttyUSB19
    lrwxrwxrwx. 1 root root 10 Jan  5 18:46 ttyUSB_Edgeport8_424100 -> ../ttyUSB0
    lrwxrwxrwx. 1 root root 10 Jan  5 18:46 ttyUSB_Edgeport8_424101 -> ../ttyUSB1
    lrwxrwxrwx. 1 root root 10 Jan  5 18:46 ttyUSB_Keyspan_USA-49WG_431100 -> ../ttyUSB6
    lrwxrwxrwx. 1 root root 10 Jan  5 18:46 ttyUSB_Keyspan_USA-49WG_431101 -> ../ttyUSB7
    lrwxrwxrwx. 1 root root 10 Jan  5 18:46 ttyUSB_Keyspan_USA-49WG_431102 -> ../ttyUSB8
    lrwxrwxrwx. 1 root root 10 Jan  5 18:46 ttyUSB_Keyspan_USA-49WG_431103 -> ../ttyUSB9
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Keyspan_USA-49WG_432100 -> ../ttyUSB10
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Keyspan_USA-49WG_432101 -> ../ttyUSB11
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Keyspan_USA-49WG_432102 -> ../ttyUSB12
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Keyspan_USA-49WG_432103 -> ../ttyUSB13
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Keyspan_USA-49WG_433100 -> ../ttyUSB14
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Keyspan_USA-49WG_433101 -> ../ttyUSB15
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Keyspan_USA-49WG_433102 -> ../ttyUSB16
    lrwxrwxrwx. 1 root root 11 Jan  5 18:46 ttyUSB_Keyspan_USA-49WG_433103 -> ../ttyUSB17
    
    0 讨论(0)
  • 2020-12-29 14:14

    Much like Ilya Matvejchikov's answer, a good solution is to add udev rules to do what you want with the device. Like you, I was having a similar problem. I had a UPS on a USB-to-multi-serial adapter and occasionally the system would switch around the /dev/tty numbers.

    My solution was to create a rule to match the type of device by driver and port, then create a symbolic link to the port to which my UPS was attached. I used NUT to monitor the UPS, which was always plugged into the same physical port.

    # File contents of /etc/udev/rules.d/75-nut-ups.rules
    # Create /dev/nut-ups0 to use as a persistent serial device that can be used
    # reliably by nut to communicate with a UPS attached to the system.
    # The UPS is attached to the first port of a MosSemiconductor dual USB
    # serial adapter.
    KERNELS=="ttyUSB*", SUBSYSTEMS=="usb-serial", DRIVERS=="moschip7720", ATTRS{port_number}=="0", SYMLINK+="nut-ups0"
    

    Now I configure NUT to always use a constant /dev/nut-ups0, as the serial port and the rule takes care of mapping properly when the usb-serial device is recognized.

    You can use the lsusb command to find out the actual device name to use in the rule when it's plugged in.

    0 讨论(0)
  • 2020-12-29 14:26

    The devices all have the same serial numbers [..]

    The usb-parse-devpath.pm addresses this by using the bus and port id of the adapter.

    0 讨论(0)
  • 2020-12-29 14:27

    Look with $ udevadm info -n /dev/ttyUSB0 -a which port your USB device is plugged in. The variable KERNELS of one of the parent devices should be something like KERNELS=="1-1.2:1.0".

    Create a udev rule:

    SUBSYSTEM=="tty", KERNELS=="1-1.2:1.0", SYMLINK+="ttyUSB42"
    SUBSYSTEM=="tty", KERNELS=="1-1.3:1.0", SYMLINK+="usb-serial"
    

    and trigger udev

    $ udevadm trigger
    
    0 讨论(0)
  • 2020-12-29 14:28

    usb-devices can get you the port number and the bus/dev enumeration.

    0 讨论(0)
提交回复
热议问题