Skip to content

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:

bash
cat /sys/bus/usb-serial/devices/ttyUSB0/latency_timer

This 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:

  1. Open a terminal and enter the following command to create or edit the udev rules file:

    bash
    sudo gedit /etc/udev/rules.d/70-ttyusb.rules

    If you do not have the gedit editor installed, you can use vim or any other text editor.

  2. Add the following line to the file:

    udev
    ACTION=="add", SUBSYSTEM=="usb-serial", DRIVER=="ftdi_sio", ATTR{latency_timer}="1"

    The purpose of this line is that when the system detects a matching USB serial device (that is, a usb-serial device using the ftdi_sio driver), it changes its latency setting to 1.

  3. 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:

bash
sudo udevadm control --reload-rules
sudo udevadm trigger

4. Verify the Change ​

After restarting the system or reloading the udev rules, check the latency setting again:

bash
cat /sys/bus/usb-serial/devices/ttyUSB0/latency_timer

If 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:

  1. Open a terminal and enter the following command to create or edit the udev rules file:
bash
sudo vim /etc/udev/rules.d/70-ttyusb.rules
  1. Add the following content:

    bash
    KERNEL=="ttyUSB[0-9]*",MODE="0666"
    #:wq 退出
  2. Re-insert the USB-to-serial device.