Crate serialport5

Source
Expand description

serialport-rs is a cross-platform serial port library.

The goal of this library is to expose a cross-platform and platform-specific API for enumerating and using blocking I/O with serial ports. This library exposes a similar API to that provided by Qt’s QSerialPort library.

§Feature Overview

The library provides a single SerialPort type which works across the supported platforms. Some platform-specific functionality is available through platform-specific extension traits provided in the posix and windows modules, which can be imported when platform-specific functions are needed.

To open a SerialPort, create a builder with SerialPort::builder(). The SerialPortBuilder can be used to customize settings such as baud rate, number of data bits, flow control, parity and timeouts before opening the port. Note that most of these settings can be changed after opening as well, but they are provided on the builder for convenience.

For normal reading and writing, SerialPort implements the standard Read and Write traits.

use std::io::Read;
use serialport5::SerialPort;
let mut port = SerialPort::builder().baud_rate(115200).open("/dev/ttyUSB0")?;
let mut buf = [0u8; 1024];
let bytes_read = port.read(&mut buf[..])?;
println!("Read {} bytes: {:?}", bytes_read, &buf[..bytes_read]);

SerialPort instances are thread-safe, and both read and write are implemeted for both SerialPort and &SerialPort. This allows you to share a single SerialPort instance between threads without locking. This is primarily intended to allow having 1 thread for reading and 1 thread for writing. It is also possible to get separate SerialPort instances which have the same underlying serial device using try_clone.

Modules§

posix
Provides unix-only extensions to the SerialPort type.
windows
Provides windows-only extensions to the SerialPort type.

Structs§

Error
An error type for serial port operations
SerialPort
A Serial Port device.
SerialPortBuilder
A struct containing all serial port settings
SerialPortInfo
A device-independent implementation of serial port information
UsbPortInfo
Contains all possible USB information about a SerialPort

Enums§

ClearBuffer
Specifies which buffer or buffers to purge when calling clear
DataBits
Number of bits per character
ErrorKind
Categories of errors that can occur when interacting with serial ports
FlowControl
Flow control modes
Parity
Parity checking modes
SerialPortType
The physical type of a SerialPort
StopBits
Number of stop bits

Functions§

available_ports
Returns a list of all serial ports on system

Type Aliases§

Result
A type for results generated by interacting with serial ports