de-vri-es / serial2-rs

Cross platform serial ports for Rust
Other
41 stars 10 forks source link

Support stable serial device IDs on linux and maybe netbsd? #12

Open DanielJoyce opened 1 year ago

DanielJoyce commented 1 year ago

Many linux distros support /dev/serial/by-id which is stable on device plug/unplug. It would be nice to support this as well when enumerating devices.

de-vri-es commented 10 months ago

Hey! It could be nice indeed. The only thing I'm considering is how to structure the API.

Should the current API simply be adjusted to add stable devices? Or should it be a separate new API? Should that one also include devices that don't have a stable device path?

And I think that directory is populated by udev. What about systems that don't have that directory? Should we fall back to non-stable device paths?

bofh69 commented 7 months ago

For USB serial ports it is also possible to get stable names on macOS. A former colleague wrote this code in a script:

    ioreg -r -c IOUSBHostDevice -l -n "USB Device Name Here" | \
    grep -w 'IODialinDevice' | \
    awk -F'=' '{print $2}'| \
    sed -E -e 's/[ "]//g' | \
    sort | \
    head -n 1 | \
    tr -d '\n'

I don't have a modern mac so I can't really help with that though.

Anyway, I would go for a new API for this, returning a Result<Vec<(PathBuf, Option<String>)>>.

With the first being a normal "/dev/ttyACMx"/"COM0"/"/dev/ttyusb.123" etc and the second being the device name when available or None.

On my machine I have:

/dev/serial/by-id/usb-SEGGER_J-Link_emulator_001050286728-if00 -> ../../ttyACM1
...

I would expect something like Ok(["/dev/ttyACM1", Some("usb-SEGGER_J-Link_emulator_001050286728-if00"), ...]) as the result of that function call.

EDIT: Or maybe without the "usb-" prefix. It would be really nice if the device names could be the same on multiple platforms. I've got a program (otii-measurement) that talks to a Otii Arc energy analyzer. Currently I've got its usual linux device name (/dev/serial/by-id/usb-Qoitech_Arc-if00) as the default device parameter in my program, but it would be nice if I could find the right device on all platforms given the device name instead. There I suspect the "usb-" prefix is only available on linux.

de-vri-es commented 6 months ago

I like the idea of providing a path and additional information separately.

Maybe we should immediately use a SerialPortInfo struct for the information, so we can add more information in the future without breaking the API.