Skip to main content

Module serial

Module serial 

Source
Expand description

USB serial adapters: CDC-ACM devices (Arduino-style boards, modems, most microcontroller USB stacks) and FTDI chips, behind one SerialPort type.

This module is behind the serial cargo feature.

use rawusb::serial::{LineConfig, SerialPort};
use std::io::{Read, Write};
use std::time::Duration;

let ctx = rawusb::Context::new()?;
let dev = ctx.find_device(0x0403, 0x6001)?.expect("adapter not plugged in");
let mut port = SerialPort::open(&dev)?;
port.set_line_config(&LineConfig::new(115_200))?;
port.set_dtr(true)?;
port.set_read_timeout(Duration::from_millis(500));
port.write_all(b"AT\r")?;
let mut reply = [0u8; 64];
let n = port.read(&mut reply)?;

Opening a port does not change its line settings or modem lines; set what you need. Many CDC-ACM devices only start talking once DTR is asserted.

§Platform notes

The operating system’s serial driver normally owns these interfaces. On Linux it is detached while the port is open (the /dev/ttyUSB* or /dev/ttyACM* node disappears) and re-attached when it is dropped. On macOS the Apple CDC and FTDI drivers cannot be displaced, and on Windows the device must be bound to WinUSB; prefer the OS serial port there.

Re-exports§

pub use ftdi::FtdiChip;

Modules§

ftdi
FTDI USB-to-serial chips: chip identification, baud-rate divisors and the vendor request set, following the Linux ftdi_sio driver and FTDI’s application note AN232B-05.

Structs§

LineConfig
Line settings: baud rate and character framing.
ModemStatus
Modem input lines and line errors.
PortInfo
A serial port a device offers, as listed by ports.
SerialPort
A USB serial port. See the module documentation.

Enums§

DataBits
Number of data bits per character.
FlowControl
Hardware or software flow control.
Parity
Parity bit.
PortKind
The protocol of a PortInfo.
SerialKind
Which kind of adapter a SerialPort drives.
StopBits
Number of stop bits.

Functions§

ports
The serial ports of a device, in descriptor order: every CDC-ACM function, and on FTDI devices (vendor ID 0x0403) every port. Composite devices often have several (a debug probe with two UARTs, a modem with AT and diagnostic ports).