serial_enumerator

Crates.ioserial_enumerator
lib.rsserial_enumerator
version0.2.12
sourcesrc
created_at2022-01-05 14:52:11.516239
updated_at2023-03-24 17:44:35.850399
descriptionA serial port enumreator library writen in rust
homepagehttps://github.com/rede97/serial_enumerator
repositoryhttps://github.com/rede97/serial_enumerator
max_upload_size
id508413
size36,125
eebear (rede97)

documentation

README

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 |
+--------------+------------+------------------+-----------+
Commit count: 29

cargo fmt