#serial-port #serial #tty #uart

bin+lib serial_enumerator

A serial port enumreator library writen in rust

6 releases

0.2.12 Mar 24, 2023
0.2.10 Mar 24, 2023
0.2.5 Jun 1, 2022
0.2.4 Jan 5, 2022

#1008 in Hardware support

28 downloads per month
Used in 2 crates

MIT license

33KB
670 lines

serial_enumerator

A serial port enumreator library writen in rust, which can help you to get serial ports and informations of you devices.

  • Support Linux, Windows, MacOS
  • Support arm and x86 devices of linux

Simple usage

  • print all serial port with table
  • The sample cli-tool to list serial port: lser
use cli_table::{format::Justify, print_stdout, Table, WithTitle};
use serial_enumerator::{get_serial_list, SerialInfo};

#[derive(Table)]
struct SerialItem {
    #[table(title = "Name")]
    name: String,
    #[table(title = "Vendor", justify = "Justify::Center")]
    vendor: String,
    #[table(title = "Product", justify = "Justify::Center")]
    product: String,
    #[table(title = "USB", justify = "Justify::Center")]
    usb: String,
}

impl SerialItem {
    pub fn from_serial_info(serial_info: SerialInfo) -> SerialItem {
        let field_or_else = || Some(String::from("--"));
        return SerialItem {
            name: serial_info.name,
            vendor: serial_info.vendor.or_else(field_or_else).unwrap(),
            product: serial_info.product.or_else(field_or_else).unwrap(),
            usb: serial_info
                .usb_info
                .and_then(|usbinfo| Some(format!("{}:{}", usbinfo.vid, usbinfo.pid)))
                .or_else(field_or_else)
                .unwrap(),
        };
    }
}

fn main() {
    let serials_info = get_serial_list();
    let mut serials_table = Vec::new();
    for serial_info in serials_info {
        serials_table.push(SerialItem::from_serial_info(serial_info));
    }
    print_stdout(serials_table.with_title()).unwrap();
}

  • Output on Debian
+--------------+------------+------------------+-----------+
| Name         | Vendor     | Product          | USB       |
+--------------+------------+------------------+-----------+
| /dev/ttyS0   |    pnp     |     PNP0501      |    --     |
+--------------+------------+------------------+-----------+
| /dev/ttyUSB0 |    FTDI    | Dual RS232-HS:00 | 0403:6010 |
+--------------+------------+------------------+-----------+
| /dev/ttyUSB1 |    FTDI    | Dual RS232-HS:01 | 0403:6010 |
+--------------+------------+------------------+-----------+
| /dev/ttyUSB2 | ch341-uart | USB2.0-Serial:00 | 1a86:7523 |
+--------------+------------+------------------+-----------+

Dependencies

~0.8–38MB
~545K SLoC