SerialLink is a Python library designed to simplify serial communication with microcontrollers
Project description
SerialLink
SerialLink is a Python library designed to simplify serial communication with microcontrollers. It enables efficient and seamless data exchange without blocking code execution. By using configurable start and end markers, SerialLink ensures reliable message framing, making it an ideal tool for developers working with embedded systems or serial communication.
Notes: Need the arduino companion library
Key Features:
- Non-blocking Reads: No need for methods like readUntil("\n") that can hinder code flow.
- Marker-Based Communication: Automatically detects the start and end of data packets, reducing parsing complexity.
- Ease of Use: Simplified API for quick integration into projects.
SerialLink is perfect for anyone needing robust, non-blocking serial communication for real-time applications.
Installation
Installing SerialLink is straightforward. It’s available via pip for easy installation.
Using pip
Run the following command in your terminal or command prompt:
pip install seriallink
Manual Installation
If you prefer to install manually:
- Clone the repository:
git clone https://github.com/your-username/seriallink.git
- Navigate to the project directory:
cd seriallink
- Install the library using setup.py:
python setup.py install
Getting Started
Using SerialLink to communicate with a microcontroller is simple and intuitive. This quick guide will help you set up and send your first message.
Note: Needs the companion arduino library at Arduino SerialLink
Step 1: Import SerialLink
Begin by importing the library in your Python script:
from seriallink import SerialLink
Step 2: Initialize the Serial Connection
Create a SerialLink object by specifying the port and baud rate:
serial = SerialLink("/dev/ttyUSB0", 115200)
serial = SerialLink("/dev/ttyUSB0")
the default buadrate is 115200 and the startMarker is < and the endMarker is >.
Step 3: Send Data
Send a message to your microcontroller:
serial.send("on")
Step 4: Read Data
Receive data from the microcontroller without blocking:
serial.poll()
if serial.new_data:
data = serial.get_data()
print(f"Received {data})
Step 5: Close the Connection
Always close the connection when done:
serial.close()
Get serial ports
from seriallink import get_ports
ports = get_ports("USB") # Gets ports with usb devices connected in a list
print(ports[0])
API Reference
The SerialLink library provides a straightforward API for serial communication. Below is a detailed reference to its primary methods and attributes.
SerialLink Class
Initialization
SerialLink(port: str, baudrate: int = 115200, start_marker: str = "<", end_marker: str = ">")
Parameters:
- port (str): The serial port (e.g., "COM3", "/dev/ttyUSB0").
- baudrate (int): Communication speed (default: 9600).
- start_marker (str): Character marking the start of a message (default: <).
- end_marker (str): Character marking the end of a message (default: >).
Methods
-
send(data: str)
Sends a message through the serial port.
- Parameters:
- data (str): The string message to send.
- Example:
serial.send("on")
- Parameters:
-
poll()
Polls the serial port buffer for new data and buffers the data to be read.
serial.poll()
-
get_data()
Get new data from the buffer
if serial.new_data: data = serial.get_data() print(data)
-
flush_port()
Read and clear all data from the serial port buffer
serial.flush_port()
-
close()
close the serial port
serial.close()
Attributes
- port: The port used for communication.
- baudrate: The baud rate of the connection.
- start_marker: The character marking the start of a message.
- end_marker: The character marking the end of a message.
Examples
-
Basic Communication
Send a message to a microcontroller and read its response:
from seriallink import SerialLink # Initialize SerialLink serial = SerialLink(port="/dev/ttyUSB0", baudrate=115200) # Send a message serial.send("Hello, Microcontroller!") # Poll for a response serial.poll() if serial.new_data: response = serial.get_data() print(f"Received: {response}") # Close the connection serial.close()
-
Custom Start and End Markers
Use custom markers to frame messages:
from seriallink import SerialLink # Initialize with custom markers serial = SerialLink(port="/dev/ttyUSB0", baudrate=115200, start_marker="{", end_marker="}") # Send a message serial.send("temp") # Poll for the response serial.poll() if serial.new_data: data = serial.get_data() print(f"Sensor Data: {data}") # Close the connection serial.close()
-
Continuous Reading
Read messages in a loop without blocking the main program:
from seriallink import SerialLink import time # Initialize SerialLink serial = SerialLink(port="/dev/ttyUSB", baudrate=9600) try: while True: # Check for incoming data serial.poll() if serial.new_data: data = serial.get_data() print(f"Received: {data}") time.sleep(0.1) # Avoid tight looping except KeyboardInterrupt: print("Stopping communication.") finally: serial.close()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file seriallink-1.1.0.tar.gz.
File metadata
- Download URL: seriallink-1.1.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a953d200697eb5ab27b660cad1fee94ffbce2cae39f1c09232358c50ec6f981
|
|
| MD5 |
a3fbd073f619f23559812b5c8b7a0f08
|
|
| BLAKE2b-256 |
6bc2c35d145da3cbdf713725916cce65f008d68a10b2a905b8050d57f82de219
|
File details
Details for the file seriallink-1.1.0-py3-none-any.whl.
File metadata
- Download URL: seriallink-1.1.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
678d25fdee6f2c241285aa282928ac7576ce08fc0e7ddc95c4850efb36365d09
|
|
| MD5 |
ca8a03b95ae8489e86d3461b3695d25e
|
|
| BLAKE2b-256 |
cc22cdb00ee8fac408ac29991bde26b4907cafccad1e8153262d95138a39f430
|