py_serial_rs

The goal of this package is to provide a fast and reliable interface to the serial ports.

Instead of implementing the logic in Python, this package builds around serialport written in Rust. This package supports threading.

Installation

PyO3 (with maturin) will build a pip package for you. For this, make sure that you are able to build rust packages with cargo. Create a virtual environment and install the required pip packages with

pip install -r requirements.txt

The pip package can then easily be build with

maturin build --release

Voilà, a pip package to be used in your code!

Demo Scripts

Demo scripts are located in /python:

  • demo.py shows a simple direct communication via the serial connection

  • demo_threads.py demonstrates how this package can be used with threads

API

class py_serial_rs.PySerial

Sets up a connection with a serial port

Parameters:
  • baud_rate (int) – baud rate of the serial port

  • port (str) – name of the serial port

close()

Close the connection to the serial port (handled internally)

read_line(timeout_in_millis=Ellipsis)

Returns data from the serial port until newline (\n) is read

Parameters:

timeout_in_millis (int) – (Optional) Duration in milliseconds until a timeout exception is thrown

Returns:

A string with the received data

Return type:

str

reset_input_buffer()

Clear the input buffer. Any pending input will be discarded.

reset_output_buffer()

Clear the output buffer. Any pending output will be discarded.

write(data)

Writes data to the serial port

Parameters:

data (bytes) – Byte array of data that will be written

Returns:

the number of bytes written

Return type:

int