MCPcopy Index your code
hub / github.com/de-vri-es/serial2-rs

github.com/de-vri-es/serial2-rs @v0.2.37

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.2.37 ↗ · + Follow
253 symbols 441 edges 22 files 71 documented · 28%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

serial2

Serial port communication for Rust.

The serial2 crate provides a cross-platform interface to serial ports. It aims to provide a simpler interface than other alternatives.

Currently supported features: * Simple interface: one [SerialPort] struct for all supported platforms. * List available ports. * Custom baud rates on all supported platforms except Solaris and Illumos. * Concurrent reads and writes from multiple threads, even on Windows. * Purge the OS buffers (useful to discard read noise when the line should have been silent, for example). * Read and control individual modem status lines to use them as general purpose I/O. * Cross platform configuration of serial port settings: * Baud rate * Character size * Stop bits * Parity checks * Flow control * Read/write timeouts * Full access to platform specific serial port settings using target specific feature flags ("unix" or "windows").

You can open and configure a serial port in one go with [SerialPort::open()]. The second argument to open() must be a type that implements [IntoSettings]. In the simplest case, it is enough to pass a u32 for the baud rate. Doing that will also configure a character size of 8 bits with 1 stop bit and disables parity checks and flow control. For full control over the applied settings, pass a closure that receives the current [Settings] and return the desired settings. If you do, you will almost always want to call [Settings::set_raw()] before changing any other settings.

The standard [std::io::Read] and [std::io::Write] traits are implemented for [SerialPort] and [&SerialPort][SerialPort]. This allows you to use the serial port concurrently from multiple threads through a non-mutable reference.

There are also non-trait read() and write() functions, so you can use the serial port without importing any traits. These take &self, so they can also be used from multiple threads concurrently.

The [SerialPort::available_ports()] function can be used to get a list of available serial ports on supported platforms.

Example

This example opens a serial port and echoes back everything that is read.

use serial2::SerialPort;

// On Windows, use something like "COM1" or "COM15".
let port = SerialPort::open("/dev/ttyUSB0", 115200)?;
let mut buffer = [0; 256];
loop {
    let read = port.read(&mut buffer)?;
    port.write_all(&buffer[..read])?;
}

Extension points exported contracts — how you extend this code

IntoSettings (Interface)
Trait for objects that can configure a serial port. The simplest option is to pass a `u32`, which is used to set the ba [3 …
src/into_settings.rs
DCBBitField (Interface)
(no doc) [1 implementers]
src/sys/windows/mod.rs

Core symbols most depended-on inside this repo

as_raw_handle
called by 24
src/sys/windows/mod.rs
check_bool
called by 21
src/sys/windows/mod.rs
as_raw_fd
called by 18
src/serial_port.rs
check
called by 14
src/sys/unix/mod.rs
as_u8
called by 4
src/settings.rs
as_str
called by 4
src/settings.rs
set_raw
called by 4
src/settings.rs
poll
called by 4
src/sys/unix/mod.rs

Shape

Method 187
Function 44
Class 15
Enum 5
Interface 2

Languages

Rust100%

Modules by API surface

src/sys/windows/mod.rs74 symbols
src/sys/unix/mod.rs46 symbols
src/serial_port.rs44 symbols
src/settings.rs34 symbols
src/rs4xx.rs14 symbols
src/sys/unix/linux/rs4xx.rs8 symbols
tests/serde.rs4 symbols
tests/conversions.rs4 symbols
tests/assert_traits.rs4 symbols
examples/serial-cat.rs4 symbols
src/sys/unix/apple.rs3 symbols
src/os.rs3 symbols

For agents

$ claude mcp add serial2-rs \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page