Optimizing USB-485 Serial Port Latency on Linux
Problem Details
When using devices based on USB-to-485 serial communication, the Linux system applies a default delay to USB-to-serial devices, which may seriously affect the performance of real-time-sensitive application scenarios.
Solution Steps
1. Check the Current Latency Setting
First, you need to check the current latency setting of the USB-to-serial device. Open a terminal and enter the following command:
cat /sys/bus/usb-serial/devices/ttyUSB0/latency_timerThis will display the current latency value, which is usually around 15ms.
2. Modify the udev Rules to Set the Minimum Latency
To set the latency to the minimum value of 1, you need to edit the system's udev rules file. Perform the following steps:
Open a terminal and enter the following command to create or edit the
udevrules file:bashsudo gedit /etc/udev/rules.d/70-ttyusb.rulesIf you do not have the
gediteditor installed, you can usevimor any other text editor.Add the following line to the file:
udevACTION=="add", SUBSYSTEM=="usb-serial", DRIVER=="ftdi_sio", ATTR{latency_timer}="1"The purpose of this line is that when the system detects a matching
USBserial device (that is, ausb-serialdevice using theftdi_siodriver), it changes its latency setting to1.Save and close the file.
3. Restart the System or Reload the udev Rules
For the changes to take effect, you can restart the system, or reload the udev rules:
sudo udevadm control --reload-rules
sudo udevadm trigger4. Verify the Change
After restarting the system or reloading the udev rules, check the latency setting again:
cat /sys/bus/usb-serial/devices/ttyUSB0/latency_timerIf the output is 1, the change was successful.
5. Modify the Serial Port Permissions (Optional)
In some cases, you may also need to adjust the serial port permissions so that ordinary users (non-root users) can access it as well. This can be done by modifying the group ownership of the serial device or by adding a udev rule. The specific steps are as follows:
- Open a terminal and enter the following command to create or edit the
udevrules file:
sudo vim /etc/udev/rules.d/70-ttyusb.rulesAdd the following content:
bashKERNEL=="ttyUSB[0-9]*",MODE="0666" #:wq 退出Re-insert the
USB-to-serial device.
