A Python module for continuous communication with multiple serial ports, based on pyserial module. PyMultiSerial has an Event-driven architecture with events like Connection, Disconnection, incoming data from port, etc.
If this library makes your life easier and has helped bring your project to reality, I would appreciate a small token of gratitude. Click on the below icon to contribute.
pip3 install pyMultiSerial
Dependencies are automatically installed during the installation of pyMultiSerial.
pyMultiSerial is very easy to use! Below are the important statements & functions of the library. Do check the Examples folder for simple examples to get started with.
Import Module
import pyMultiSerial as p
Create Object
ms = p.MultiSerial()
Set Properties of Object:
ms.baudrate = 9600
ms.timeout = 2
Callback functions are the functions defined by you in your program which are triggered by pyMultiSerial module whenever an event occurs.
The pyMultiSerial module provides 5 callback events for which you can define functions:
Note: All the above callbacks are optional. You need to only program those callbacks which you need.
How to define and register callback function:
def your_func_name(standard_arguments):
Your Statements
End of function
#register callback function
ms.callback_name = your_func_name
Here, your_func_name can be any name as decided by you. Arguments passed to this function should be in accordance to the standard_arguments as defined in below table. These arguments will be passed from pyMultiSerial module to your function. callback_name is the property name of the callback event to which you need to assign your function as shown in above snippet.
Event | callback_name | standard_arguments for callback function |
---|---|---|
New Serial Port Connection Found | port_connection_found_callback | Port Number, Serial Port Object |
Data Received on a Serial Port | port_read_callback | Port Number, Serial Port Object, Data |
Device disconnected from Serial Port | port_disconnection_callback | Port Number |
On Keyboard Interrupt (Ctrl+C) by user | interrupt_callback | - |
Continuous Loop Execution | loop_callback | - |
In the above table, there are mainly three types of arguments:
Note: You must include above paramenters in your function definition. However, it is optional to use those parameters.
Start Monitoring with below statement:
ms.Start()
Caution: Since this module monitors all serial ports simultaneously, ms.Start() is a blocking function. Unless you don't stop monitoring using Stop() method, the execution will be stuck at this line. Start() method should ideally be called at the end of your code. To perform other opertions, you should use the provided callback functions. Caution: Callback functions should be defined before ms.Start() statement. The Callback functions should be registered with the object before monitoring is started, else your callback functions won't be called.
If you need to stop monitoring for any reason, use below statement:
ms.Stop()
Here is an instructable with simple Arduino-Raspberry Pi example
Hope this module helps you to build your Projects. In case of any issues do open an Issue on Github.