Binding of serial device and serial number under linux system

3.7 Binding of serial device and serial number under linux system

3.7.1 Origin of the problem

​In the development process of ROS car, you need to use ROS communication serial port module, lidar serial port module, IMU serial port module, etc. In order to prevent the serial port number of these devices from changing every time you turn on the device, you need to bind the serial port number to the serial device Fixed; In order to better identify the serial port, we can also set a static name for the serial port number; in order to better use the serial port, we can also set the permissions of the serial port.

Once set up, will last a lifetime.

3.7.2 Principle

In the Linux system, although each fixed port may be assigned different ttyUSB* due to the start-up time difference of the external device, for each fixed port, there are fixed KERNELS, idProduct, idVendor, and the KERNELS of each port is different. Not the same.

Therefore, as long as we fix the installation position of the serial device, we can still solve this problem from the physical nature of the port.

3.7.3 Method

Step 1: How to query KERNELS, idProduct, idVendor of each port?

Here you need to use the following command

udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB0)

And when a serial port device is inserted independently every time , the serial port number is always /dev/ttyUSB0, if you are not assured, you can use the following command to query

ls /dev | grep ttyUSB

Use the first command, the window will print a bunch of device information, we only need KERNELS, idProduct, idVendor, we slide down to find the part similar to the following figure to find the information of the port.

image-20200920220927095

Note : When querying port information, please insert the serial device separately.

Step 2: Create your own 99-*.rules file in the /etc/udev/rules.d/ directory

For example, to create the 99-robot-usb.rules file here, use the following command

sudo vim /etc/udev/rules.d/99-robot-usb.rules

Fill in the following content, save, fill in according to the information of your own device

KERNELS=="3-2", ATTRS{
    
    idVendor}=="1a86", ATTRS{
    
    idProduct}=="7523", SYMLINK+="robot",MODE:="0777"

Explanation of the above

==”:比较键、值,若等于,则该条件满足;
“+=”:为一个表示多个条目的键赋值。
“:=”:对一个键赋值,并拒绝之后所有对该键的改动。目的是防止后面的规则文件对该键赋值。
Step 3: Restart and check whether the configuration takes effect

Reboot

sudo reboot

Wait for boot, check port

ll /dev | grep ttyUSB

The following information is printed out, and the setting is successful

lrwxrwxrwx   1 root root           7 Sep 20 22:30 robot -> ttyUSB0
crwxrwxrwx   1 root dialout 188,   0 Sep 20 22:30 ttyUSB0

Later, when we use the port at that physical location, its serial port number is fixed to /dev/robot , and it has read, write, and executable permissions.

Precautions:

The above is only the process of setting a port, and in most cases, there are multiple devices. Smart you, you must know how to infer other things.

Each time you insert a serial device independently, get its KERNELS, idProduct, idVendor value, and record it; create your own 99-*.rules file in the /etc/udev/rules.d/ directory, and fill in the device’s Information and the name of the mapping you need; then restart.

Previous link, click to visit.

Previous article : Building a ROS car and writing a starter function package
series of articles : Building a ROS robot from scratch

If you feel that my article is more suitable for you, pay attention to me, like it, and give you a different surprise.

Draft design export-20200810-205521
Insert picture description here

Guess you like

Origin blog.csdn.net/zhao_ke_xue/article/details/108700080