Code Monkey home page Code Monkey logo

serial2-rs's Introduction

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 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. 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(&buffer[..read])?;
}

serial2-rs's People

Contributors

de-vri-es avatar avocadochicken avatar hacknus avatar kinire98 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.