Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
.. image:: https://github.com/Lei-k/async-pyserial/actions/workflows/python-publish.yml/badge.svg :target: https://pypi.org/project/async-pyserial/
.. image:: https://github.com/Lei-k/async-pyserial/actions/workflows/docs-publish.yml/badge.svg :target: https://lei-k.github.io/async-pyserial/
.. image:: https://codecov.io/gh/Lei-k/async-pyserial/branch/main/graph/badge.svg :target: https://codecov.io/gh/Lei-k/async-pyserial
Python bindings for a C++ serial port library providing asynchronous serial communication support.
gevent
, eventlet
, asyncio
, callback
, and synchronous operations.epoll
, iocp
, and kqueue
for efficient, scalable I/O operations based on the underlying operating system.Documentation <https://lei-k.github.io/async-pyserial/>
_PyPI page <https://pypi.org/project/async-pyserial/>
_You can install the async-pyserial
package using either poetry
or pip
.
Install poetry
if you haven't already:
.. code-block:: shell
curl -sSL https://install.python-poetry.org | python3 -
Add async-pyserial
to your project:
.. code-block:: shell
poetry add async-pyserial
Install the package from PyPI:
.. code-block:: shell
pip install async-pyserial
For FreeBSD, you need to build the package manually:
Clone the repository:
.. code-block:: shell
git clone https://github.com/Lei-k/async-pyserial.git
Navigate to the project directory:
.. code-block:: shell
cd async-pyserial
Install the dependencies using poetry
:
.. code-block:: shell
poetry install
Build the package:
.. code-block:: shell
python -m build
Install the package:
.. code-block:: shell
pip install dist/*.whl
Here's a simple example of how to use async-pyserial
:
.. code-block:: python
from async_pyserial import SerialPort, SerialPortOptions, SerialPortEvent, SerialPortParity
def on_data(data):
print(f"Received: {data}")
options = SerialPortOptions()
options.baudrate = 9600
options.bytesize = 8
options.stopbits = 1
options.parity = SerialPortParity.NONE # NONE, ODD, EVEN
serial_port = SerialPort('/dev/ttyUSB0', options)
serial_port.on(SerialPortEvent.ON_DATA, on_data)
serial_port.open()
try:
while True:
data_to_send = input("Enter data to send (or 'exit' to quit): ")
if data_to_send.lower() == 'exit':
break
serial_port.write(data_to_send.encode('utf-8'))
finally:
serial_port.close()
A class for serial communication.
__init__(self, port: str, options: SerialPortOptions)
: Initializes the serial port with the specified parameters.def write(self, data: bytes, callback: Callable | None = None)
: Writes data
to the serial port. Can be blocking or non-blocking. If a callback is provided, the write will be asynchronous. Supports gevent
, eventlet
, asyncio
, callback
, and synchronous operations.def read(self, bufsize: int = 512, callback: Callable | None = None)
: Reads data from the serial port. Can be blocking or non-blocking. If a callback is provided, the read will be asynchronous. Supports gevent
, eventlet
, asyncio
, callback
, and synchronous operations.def open(self)
: Opens the serial port.def close(self)
: Closes the serial port.def on(self, event: SerialPortEvent, callback: Callable[[bytes], None])
: Registers a callback for the specified event.def emit(self, evt: str, *args, **kwargs)
: Emits an event, triggering all registered callbacks for that event.def remove_all_listeners(self, evt: str)
: Removes all listeners for the specified event.def remove_listener(self, evt: str, listener: Callable)
: Removes a specific listener for the specified event.def off(self, evt: str, listener: Callable)
: Alias for remove_listener
.A class for specifying serial port options.
baudrate: int
: The baud rate for the serial port.bytesize: int
: The number of data bits.stopbits: int
: The number of stop bits.parity: int
: The parity checking (0: None, 1: Odd, 2: Even).read_timeout: int
: The read timeout in milliseconds.write_timeout: int
: The write timeout in milliseconds.read_bufsize: int
: The read buffer size. Default is 0. When read_bufsize
is 0, the internal buffer is not used, and only data received after the read call will be returned. If read_bufsize
is not 0, both buffered and new data will be returned.An enumeration for serial port events.
ON_DATA
: Event triggered when data is received.An exception class for handling serial port errors.
__init__(self, *args: object)
: Initializes the SerialPortError with the specified arguments.An exception class for handling unsupported platforms.
__init__(self, *args: object)
: Initializes the PlatformNotSupported exception with the specified arguments.A function for setting the asynchronous worker.
def set_async_worker(w: str, loop = None)
: Sets the asynchronous worker to gevent
, eventlet
, or asyncio
. Optionally, an event loop can be provided for asyncio
.The examples
directory contains sample scripts demonstrating how to use async-pyserial
for various operations. Below are a few examples to help you get started.
Example scripts included in the examples
directory:
interval_write.py
: Demonstrates periodic writing to the serial port.serialport_read.py
: Demonstrates reading from the serial port with different async workers.serialport_terminal.py
: A terminal interface for interacting with the serial port.Supports Windows, Linux, macOS, and FreeBSD.
To contribute to the project, follow these steps:
Clone the repository:
.. code-block:: shell
git clone https://github.com/Lei-k/async-pyserial.git
Navigate to the project directory:
.. code-block:: shell
cd async-pyserial
Install the dependencies using poetry
:
.. code-block:: shell
poetry install
Run the tests:
.. code-block:: shell
poetry run pytest
This project is licensed under the MIT License. See the LICENSE
file for more details.
FAQs
Python bindings for a C++ serial port library
We found that async-pyserial demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.