[Linux] Fixed paths for multiple serial devices

A case where multiple serial devices are used at the same time on Linux. For example, when the GPS module and LTE communication module are connected at the same time, both are recognized as / dev / ttyUSB0 or / dev / ttyUSB1 by default, but the numbers at the end are assigned in the recognized order, so at startup. Depending on the timing of, it is possible to hit the opposite module. In this case, the program that starts automatically will not be able to access the device well.

To prevent this, write udev rules and define a fixed path so that you can access the modules in any order.

About udev rules

udev is a system for managing device files to access devices. A device file may be created under / dev when a device is connected, or deleted when it is disconnected, but here a symbolic link is created and fixed when a specific serial device is connected. Describes the rules for accessing a specific device with a serial path

About the rule file

udev rules are placed under /etc/udev/rules.d. Filenames start with a two-digit number and - and have a .rules extension.

udev file name example


70-persistent-net.rules

Two-digit numbers are called from the youngest number. Therefore, if the rule is set arbitrarily by the user, it is better to specify a young number such as 10 and apply it preferentially. If the rule for the same device is called first, the rule with the later number will not be applied.

Checking applied rules

Let's check the rules that are applied to a device. Below is an example [^ 1] of checking udev rules for / dev / sda

# udevadm test `udevadm info -q path -n /dev/sda`

Output example

# udevadm test `udevadm info -q path -n /dev/sda`
calling: test
version 3.2.9
This program is for debugging only, it does not run any program
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.

=== trie on-disk ===
tool version:          3
file size:         8270736 bytes
header size             80 bytes
strings            2110312 bytes
nodes              6160344 bytes
Load module index
timestamp of '/etc/udev/rules.d' changed
Reading rules file: /lib/udev/rules.d/01-md-raid-creating.rules
Reading rules file: /lib/udev/rules.d/10-dm.rules
Reading rules file: /lib/udev/rules.d/11-dm-lvm.rules
Reading rules file: /lib/udev/rules.d/13-dm-disk.rules
Reading rules file: /lib/udev/rules.d/40-slackware.rules
Reading rules file: /lib/udev/rules.d/40-usb_modeswitch.rules
Reading rules file: /lib/udev/rules.d/50-compat_firmware.rules
Reading rules file: /lib/udev/rules.d/50-udev-default.rules
Reading rules file: /lib/udev/rules.d/60-block.rules
Reading rules file: /lib/udev/rules.d/60-cdrom_id.rules
Reading rules file: /lib/udev/rules.d/60-drm.rules
Reading rules file: /lib/udev/rules.d/60-evdev.rules
Reading rules file: /lib/udev/rules.d/60-input-id.rules
        :

You can see that we are looking for a rule for the device specified by this command. Various other things are output, but please refer to others for detailed explanations. I'll just make sure that the rules I wrote are loaded correctly.

The rules in /lib/udev/rules.d (system-provided rules) and the rules in /etc/udev/rules.d (user-configured rules) are in 2-digit number order. It will be loaded. If it is read correctly, the file name will appear after Reading rules file:, but if there is an error in the rule, an error message will appear, so be careful.

How to write rules

Please refer to http://www.reactivated.net/writing_udev_rules.html for how to write udev rules.

Once you've mastered udev's rules, you'll be able to do things like execute specific commands when a device is connected.

reference

If you want to use the same device more than once

Since the path allocated by the USB port connected under / dev / serial / by-id / is generated, there was an example of creating a symbolic link to access this.

http://tclip.blog.fc2.com/blog-entry-86.html

However, this method is recognized even when the kernel version is upgraded or when there are multiple USB controllers (not only when an expansion board is attached, but also when the board has a mixture of USB 3.x ports and USB 2.0 ports). Please note that the order may change and the environment in which it can be used is limited.

reference

[^ 1]: Some site may have introduced the command ʻudevadm, which is now replaced by ʻudevadm info.

Recommended Posts

[Linux] Fixed paths for multiple serial devices
pyenv for linux
[For memo] Linux Part 2
What is Linux for?
Linux command for self-collection