5 releases

0.2.2 Apr 22, 2024
0.2.1 Mar 6, 2024
0.2.0 Nov 13, 2023
0.1.1 Oct 3, 2020
0.1.0 Jul 24, 2019

#13 in Hardware support

Download history 2545/week @ 2024-08-02 3272/week @ 2024-08-09 6972/week @ 2024-08-16 3480/week @ 2024-08-23 6854/week @ 2024-08-30 4840/week @ 2024-09-06 2913/week @ 2024-09-13 3041/week @ 2024-09-20 2910/week @ 2024-09-27 2872/week @ 2024-10-04 2714/week @ 2024-10-11 3352/week @ 2024-10-18 3690/week @ 2024-10-25 3625/week @ 2024-11-01 3192/week @ 2024-11-08 2880/week @ 2024-11-15

14,142 downloads per month
Used in 65 crates (60 directly)

MIT license

37KB
755 lines

usbd-serial

CDC-ACM USB serial port implementation for usb-device.

CDC-ACM is a USB class that's supported out of the box by most operating systems and used for implementing modems and generic serial ports. The SerialPort class implements a stream-like buffered serial port that can be used similarly to a normal UART.

The crate also contains CdcAcmClass which is a lower-level implementation that has less overhead, but requires more care to use correctly.

Example

A full example requires the use of a hardware-driver, but the hardware independent part is as follows:

let mut serial = SerialPort::new(&usb_bus);

let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
    .product("Serial port")
    .device_class(USB_CLASS_CDC)
    .build();

loop {
    if !usb_dev.poll(&mut [&mut serial]) {
        continue;
    }

    let mut buf = [0u8; 64];

    match serial.read(&mut buf[..]) {
        Ok(count) => {
            // count bytes were read to &buf[..count]
        },
        Err(UsbError::WouldBlock) => // No data received
        Err(err) => // An error occurred
    };

    match serial.write(&[0x3a, 0x29]) {
        Ok(count) => {
            // count bytes were written
        },
        Err(UsbError::WouldBlock) => // No data could be written (buffers full)
        Err(err) => // An error occurred
    };
}

Dependencies

~1.5MB
~32K SLoC