Skip to main content

A Python module for simultaneous communication with multiple serial ports, based on pyserial module

Project description

pyMultiSerial

GitHub release license DOI

A Python module for continuous communication with multiple serial ports, based on pyserial module

Features:

  • Monitor multiple serial ports simultaneously.
  • Detect connections to port automatically and starts monitoring them.
  • Raises a trigger whenever data is received from the port. You can attach callback function to process this data on-demand.
  • Detect disconnections from port automatically.
  • You can add your own processing logic to the above events using callback functions

How to Install:

pip3 install pyMultiSerial

Dependencies:

Dependencies are automatically installed during installation of pyMultiSerial.

  • PySerial
  • Threading

How to use

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.

Step 1: Initialize

Import Module

import pyMultiSerial as p

Create Object

ms = p.MultiSerial()

Set Properties of Object:

ms.baudrate = 9600    
ms.timeout = 2

Step 2: Define Callback Functions

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:

  1. New Serial Port Connection Found - Allows you to write a function which triggers when a new serial port connection is found. With this feature, you can perform many operations like authenticating, performing handshake or simply creating a list of newly connected devices.
  2. Data Received on a Serial Port - Allows you to write a function which triggers when any data is received on the serial port. With this feature, you can process incoming data from serial ports.
  3. Device disconnected from Serial Port - Allows you to write a function which triggers when any device is disconnected. With this feature, you can keep track of serial ports that have been disconnected.
  4. On Keyboard Interrupt (Ctrl+C) by user - Allows you to write a function which triggers when you force stop the python script with keyborard interrupt or through your Python IDE. This feature allows you to perform any clean up activities necessary before exiting your application.
  5. Continuous Loop Execution - Allows you to write a function which triggers continuously. With this feature, you can perform repitive / periodic tasks. Don't forget to add appropriate delay in this callback since this event occurs continuously. The user gets to decide what frequency / delay is needed.

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:

  1. Port Number - The Port Number of the port on which the event has occured.
  2. Serial Port Object - pySerial Object of the Port on which event has occured. This object can be used to read, write to the port from your callback function.
  3. Data - Data received from Serial Port in String format.

Note: You must include above paramenters in your function definition. However, it is optional to use those parameters.

Step 3: Start Monitoring

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyMultiSerial-1.1.1.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyMultiSerial-1.1.1-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file pyMultiSerial-1.1.1.tar.gz.

File metadata

  • Download URL: pyMultiSerial-1.1.1.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.12

File hashes

Hashes for pyMultiSerial-1.1.1.tar.gz
Algorithm Hash digest
SHA256 26f9a9716aea0023af64164dbe62f113cd27501213395fecaf455170918031c3
MD5 bde01ca6eda3d80240e805c0e8461e63
BLAKE2b-256 6c041e17260bfea8a9d4bf74b924e5adcc2b5cfd02b618fab9f0a939b132dd07

See more details on using hashes here.

File details

Details for the file pyMultiSerial-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyMultiSerial-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.12

File hashes

Hashes for pyMultiSerial-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 77e271f143e067666ded39c0333750df47d2f567c6ebbceddd01d4ee93e353a2
MD5 6bd8e7c8174f3a8d0b8f9ff945958e8d
BLAKE2b-256 4c2c5b13f6ca1711a2c46f7610cdc1adf474f6008bcba6367129ce2cf94bdd2d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page