Skip to main content

A Python module for seamless serial communication with devices like an Arduino or ESP, offering both non-blocking and blocking interfaces with customizable command and value callbacks.

Project description

SerialCommands

A Python module for seamless serial communication with devices like an Arduino or ESP, offering both non-blocking and blocking interfaces with customizable command and value callbacks.

GitHub, PyPi

Installation

Use the package manager pip to install SerialCommands.

pip install pyserial
pip install ct-serialcommands

Simple usage

from ct_serialcommands import SerialCommands

def command_callback():
    print("This is a command callback")

def value_callback(value):
    print(f"This is a value callback with value: {value}")

arduino = SerialCommands()
arduino.set_command_callback("test_command", command_callback)
arduino.set_value_callback("value", value_callback) # Returns everything after the value_separator to the callback function. 
                                                    # Ex: value_separator = ":". If the incoming message = "value: 11", "11" will be passed to the callback function.

arduino.send_command("test") # Adds a prefix to the command. Ex: command: test
arduino.send_message("hallo")

Overview

ct_serialcommands is a robust Python module designed to facilitate seamless communication with serial devices. It offers both non-blocking and blocking interfaces, allowing developers to easily send and receive commands and values through a serial interface. The module includes features such as automatic device port detection, customizable baud rates, and support for setting command and value callbacks. Additionally, it provides comprehensive logging capabilities to aid in debugging and monitoring serial communications. Whether you're developing for embedded systems, IoT devices, or any application requiring serial communication, ct_serialcommands simplifies the process and enhances reliability.

Features

  • Automatically find and connect to the device port.
  • Set custom baud rates.
  • Define command and value callbacks.
  • Send commands and messages.
  • Logging support for debugging and monitoring.

Example usage:

from ct_serialcommands import SerialCommands
import time
import logging


# Create a command callback function
def command_callback(arg1, arg2):
    print(f"Command callback with arg1: {arg1}, arg2: {arg2}")
    
# Create a value callback function
def value_callback(value, arg1, arg2):
    print(f"Value callback with arg1: {arg1}, arg2: {arg2} and value: {value}")


# Set the port name of the connected device. (Optional. Default = "AUTO") (When set to AUTO, make sure only one device is connected. It will use the first port that is found.)
# port = "/dev/cu.usbmodem21211101"
port = "AUTO"

# Set the baud rate. (Optional. Default = 9600)
baud_rate = 9600

# Set the default command prefix when sending a command. (Optional. Default = "command") (Ex.: command: test)
default_command_prefix = "command"

# Set the separator for a received value. Ex.: 'value1: 30'. (Optional. Default = ":")
value_separator = ":"

# Set the logging level. (Optional. Default = logging.INFO)
log_level = logging.INFO

# Set the logger name. (Optional. Default = "SerialCommands")
log_name = "SerialCommands"

# Initialise the class
arduino = SerialCommands(port=port, baud_rate=baud_rate, default_command_prefix=default_command_prefix, value_separator=value_separator, log_level=log_level, log_name=log_name)


# Set a command callback. Option 1
arduino.set_command_callback("command1", command_callback, "test 1", "test 2")

# Set a command callback. Option 2
arduino.set_command_callback(command="command1", callback=command_callback, args=["test 1", "test 2"])

# Set a command callback. Option 3
arduino.set_command_callback(command="command1", callback=command_callback, arg1="test 1", arg2="test 2")

# Set a value callback
arduino.set_value_callback(value="value", callback=value_callback, arg1="test 1", arg2="test 2")


# Start listening for commands
arduino.start()

try:
    while True:
        pass
except KeyboardInterrupt:
    print("Program terminated.")
finally:
    arduino.stop()

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub. You can also suggest a new sensor by opening an issue.

Support my work

If you like my work you can always buy me a coffee!

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

ct_serialcommands-0.0.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

ct_serialcommands-0.0.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file ct_serialcommands-0.0.1.tar.gz.

File metadata

  • Download URL: ct_serialcommands-0.0.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for ct_serialcommands-0.0.1.tar.gz
Algorithm Hash digest
SHA256 17389f3768b86ebde644b392d575ff823fe532b33e81b1315c7cf9be8c74cb96
MD5 0d1a69bf755a69ca32a3a15e3d77250b
BLAKE2b-256 58af45c428ca68ee039da728d5ee0cb1bbf7fba468293487b3e6df2a15ef491f

See more details on using hashes here.

File details

Details for the file ct_serialcommands-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ct_serialcommands-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b8c67701cf5fc3d09dd5ecce3d63e420d1b010f99a5cc5fcf58c2f54187dec7e
MD5 c9efc2365c8d2454ad1bcc20be861323
BLAKE2b-256 b59f8a0c6dcaa24b47e5807287f9adbf7030f083074afc8c23eb042e89a5260b

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