https://github.com/rust-embedded-community/usbd-serial
Work-in progress minimal CDC-ACM (USB serial port) class for usb-device
https://github.com/rust-embedded-community/usbd-serial
Last synced: 6 months ago
JSON representation
Work-in progress minimal CDC-ACM (USB serial port) class for usb-device
- Host: GitHub
- URL: https://github.com/rust-embedded-community/usbd-serial
- Owner: rust-embedded-community
- License: mit
- Created: 2019-05-23T15:14:29.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2025-10-07T11:42:13.000Z (8 months ago)
- Last Synced: 2025-10-25T20:11:04.449Z (8 months ago)
- Language: Rust
- Size: 54.7 KB
- Stars: 129
- Watchers: 8
- Forks: 46
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
usbd-serial
===========
CDC-ACM USB serial port implementation for [usb-device](https://crates.io/crates/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:
```rust
let mut serial = SerialPort::new(&usb_bus);
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
.strings(&[StringDescriptors::new(LangID::EN).product("Serial port")])
.expect("Failed to set strings")
.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
};
}
```