Linux binding USB device port

Posted by Stanley90 on Fri, 12 Nov 2021 21:45:56 +0100

1 bind according to device ID

Method features: if you bind according to the ID, you do not need to fix the USB interface. It is bound according to the ID of the device itself. However, if the serial ports of multiple devices use the same serial port chip, there will be disorder

Check idVendor and idpProduct of the equipment to be bound. For example, I want to bind an rplidar lidar

$ lsusb


Create a new rule file:

$ sudo gedit /etc/udev/rules.d/com_rplidar.rules

Custom open file write custom rule. You'd better create a new file here. Don't overwrite the previous file. If the same equipment requires additional discrimination parameters, additional parameters can also be added. We only take the previous values 10c4, ea60, and don't use the following description.

KERNEL=="ttyUSB*" , ATTRS{idVendor}=="10c4",ATTRS{idProduct}=="ea60",MODE:="0777" ,SYMLINK+="ttyRplidar"

Then make it work:

$ sudo udevadm trigger

Check for success

$ ls -l /dev/ttyRplidar 
lrwxrwxrwx 1 root root 7 Nov 12 22:22 /dev/ttyRplidar -> ttyUSB0

perhaps

ls /dev/tty*


As shown in the figure, you can see that a new device is added, which is the alias we set in the rule file.

If not, first check whether the format of the rule file is wrong, and then unplug the USB and then plug it in again.

If there are other additional devices, add a line in the same way.

2. According to the computer USB hardware port binding, the device name is set as long as the USB port is inserted

Method features: if bound according to the USB hardware ID, the device corresponding to the interface must be fixed, because it is only determined according to the USB hardware address of the computer itself, but it can solve the problem that the device uses the same serial port chip, resulting in the same ID

Sometimes our two serial devices use painful USB chips. For example, lidar and manipulator use the same USB chip to input data in the terminal

$ lsusb

It will be found that the VID and PID of the two USB ports are the same (10c4:ea60), so after the manipulator is turned on, the lidar will point to the port of the manipulator when it is started.

Bind the name of the port to which the USB device is connected. After unplugging or closing the USB device, enter it in the port

$ ls /dev

Then insert or open the USB device and enter

$ ls /dev

Find the name of the USB device (determine the serial port name of each current device, such as ttyUSB1 for the manipulator and ttyUSB0 for the lidar), and then enter it

$ udevadm info --attribute-walk --name=/dev/ttyUSB1

Can see

...
looking at device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2.4/1-2.4.4/1-2.4.4:1.0/ttyUSB1/tty/ttyUSB1':
    KERNEL=="ttyUSB1"
    SUBSYSTEM=="tty"
    DRIVER==""
  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/ 1-2/1-2.4/1-2.4.4/1-2.4.4:1.0/ttyUSB1':
    KERNELS=="ttyUSB1"
    SUBSYSTEMS=="usb-serial"
    DRIVERS=="cp210x"
    ATTRS{port_number}=="0"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/ 1-2/1-2.4/1-2.4.4/1-2.4.4:1.0':
    KERNELS=="1-2.4.4:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="cp210x"
    ATTRS{authorized}=="1"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{bInterfaceClass}=="ff"
    ATTRS{bInterfaceNumber}=="00"
    ATTRS{bInterfaceProtocol}=="00"
    ATTRS{bInterfaceSubClass}=="00"
    ATTRS{bNumEndpoints}=="02"
    ATTRS{interface}=="CP2102 USB to UART Bridge Controller"
    ATTRS{supports_autosuspend}=="1"
    ...
You can know that the port number used by the device is "1"-2.4.4: 1.0",Next, start writing binding rules
$ sudo gedit /etc/udev/rules.d/ttyUSBLink.rules
 Add to file
$ ACTION=="add",KERNELS=="1-2.4.4: 1.0",SUBSYSTEMS=="usb",MODE:="0777", SYMLINK+="rplidar"

Save the file and restart the PC, and input it in the terminal

$ ls -l /dev |grep ttyUSB

Check whether the modification was successful

Note: because it is bound to a fixed port, the usb interface cannot be changed at will. It is considered to mark the devices inserted in each position on the HUB in the future.

Topics: Linux