serialport / serialport-rs

A cross-platform serial port library in Rust. Provides a blocking I/O interface and port enumeration including USB device information.
Other
515 stars 124 forks source link

Support low latency mode #3

Open jessebraham opened 2 years ago

jessebraham commented 2 years ago

This issue was migrated from GitLab. The original issue can be found here: https://gitlab.com/susurrus/serialport-rs/-/issues/55

USB serial ports can have a latency problem when you're trying to receive small packets of data, because they try to buffer data to put into one USB packet for efficiency. For example FTDI serial ports have a timeout of 16 milliseconds by default, and if you're only receiving a few bytes at a time, you will only receive data every 16ms.

Linux supports an ASYNC_LOW_LATENCY which asks drivers to have less latency (perhaps at the efficiency of transferring large amounts of data) which alleviates this problem. Windows also seems to have a similar thing using the COMMTIMEOUTS struct (not tested). I couldn't immediately find a similar setting for macOS.

Having a set_low_latency method on platforms that support it would be useful.

michaellass commented 1 year ago

We needed this functionality as well and created a separate small crate for it: https://github.com/michaellass/serialport_low_latency. It uses the TIOCGSERIAL and TIOCSSERIAL ioctls to change the serial line information and enable or disable low latency mode. This is basically the same as a setserial /dev/tty... low_latency would do.

It would be nice to incorporate this functionality directly into this crate. The reason why we haven't patched this into serialport-rs is portability and the necessity to run bindgen during build:

Note that, if you have root permissions, it should also be possible to just write 1 into /sys/bus/usb-serial/devices/tty.../latency_timer. However, that file is typically writable only for root while the ioctls work for whoever can write to the tty device.