This repository contains tools for identifying and managing USB devices on your main system.
Clone this repository to your system where you need to connect and identify specific USB devices:
git clone https://github.com/your-username/tools.git
cd toolsDescription
This script creates a persistent udev rule for a specified USB device, allowing the system to recognize and assign a fixed name to it.
Usage
sudo ./create_usb_rule.sh /dev/ttyUSB0 usb_custom_name
Replace /dev/ttyUSB0 with your device path and usb_custom_name with the desired symlink name.
Quickly extracts and lists key attributes of specific USB devices.
./extract_usb_info.sh /dev/ttyUSB0Replace /dev/ttyUSB0 with the device you want to query.
idVendor: Device's vendor IDidProduct: Device's product IDmanufacturer: Device manufacturer nameserial: Device's serial number
Generates specific udev rules based on USB device characteristics, allowing the main system to recognize and assign fixed names to these USB devices.
- Modify the
device_rule_generate.shscript:- Add USB device characteristics (obtained from
extract_usb_info.sh) to the designated area.
- Add USB device characteristics (obtained from
- Execute the script with sudo:
This will assign fixed names to the USB devices and take effect immediately.
sudo ./device_rule_generate.sh
# LiDAR device rule
SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", ATTRS{manufacturer}=="Silicon Labs", SYMLINK+="usb_ydlidar", MODE="0666"
# Arduino device rule
SUBSYSTEM=="tty", ATTRS{idVendor}=="1d6b", ATTRS{idProduct}=="0002", SYMLINK+="usb_rear_wheel", MODE="0666"This Python script automates the process of generating udev rules for USB devices. It communicates with connected devices, retrieves their custom IDs, and creates appropriate udev rules.
- Python 3.x
pyseriallibrary (install withpip install pyserial)- Arduino or ESP32 devices with proper firmware (see Arduino/ESP32 Code Requirements below)
For this script to work correctly, the Arduino or ESP32 devices must include their own CUSTOM_ID and be able to respond to JSON commands. Add the following to your Arduino/ESP32 code:
// Replace with your device's unique identifier
#define CUSTOM_ID "usb_rear_wheel"
// In your main loop or command processing function:
if (doc.containsKey("command") && doc["command"] == "I") {
Serial.println(CUSTOM_ID);
}This code allows the device to respond with its CUSTOM_ID when queried by the rule_generator.py script. The CUSTOM_ID will be used as the device's rule name in the generated udev rules.
- Ensure you have the necessary permissions to write to
/etc/udev/rules.d/and execute udev commands. - Make sure your Arduino/ESP32 devices are programmed with the required code (as described above).
- Connect your devices to the system.
- Run the script with sudo:
sudo python3 rule_generator.py- Automatically detects all
/dev/ttyUSB*and/dev/ttyACM*devices. - Communicates with each device to retrieve its custom ID.
- Generates udev rules based on the device's kernel information and custom ID.
- Reloads udev rules to apply changes immediately.
- Displays the custom ID received from each device.
- Shows the matching KERNELS information.
- Confirms the creation of udev rules for each device.
- Reports any errors encountered during the process.
Detects the specific KERNELS value associated with a given USB device path (e.g., /dev/ttyUSB0). The KERNELS value represents the physical port path in the system's kernel device tree. This value is useful for creating udev rules that target a specific physical USB port regardless of the device plugged into it.
./find_usb_kernels.sh /dev/ttyUSB0Replace /dev/ttyUSB0 with the actual device path you want to inspect.
- The script outputs the
KERNELSvalue found for the specified device (e.g.,1-1.2,2-3.4.1).
Creates a udev rule that assigns a fixed symbolic link name (e.g., /dev/my_specific_usb) to any compatible device plugged into a specific physical USB port. It identifies the port using the KERNELS value obtained from find_usb_kernels.sh. This is useful when you want a specific port to always map to the same device name, regardless of which identical device is plugged in or the order they are plugged in.
- Find the KERNELS value: Use
./find_usb_kernels.sh <DEVICE_PATH>for a device currently plugged into the desired physical port to get itsKERNELSvalue. - Run the script: Execute the script with
sudo, providing theKERNELSvalue and the desired symbolic link name.
# Example: Assign /dev/front_sensor to the port with KERNELS="1-1.2"
sudo ./set_usb_name_by_kernels.sh "1-1.2" front_sensorReplace "1-1.2" with the actual KERNELS value and front_sensor with your desired fixed name.
- Creates a udev rule file in
/etc/udev/rules.d/(e.g.,99-front_sensor.rules). - The rule matches the specified
KERNELSvalue andSUBSYSTEM=="tty". - Assigns the specified
SYMLINK+name. - Sets
MODE="0666"for general read/write access (modify if needed). - Reloads udev rules to apply the change. You might need to replug the device.


