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.1.0.tar.gz (5.5 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.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ct_serialcommands-0.1.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for ct_serialcommands-0.1.0.tar.gz
Algorithm Hash digest
SHA256 234beddb75fd71052b84fdd390074151b55ab57a1fe84b3da9201117fc1b2be9
MD5 fab15ce307a2e82c21a59573d099544c
BLAKE2b-256 a322922259973d5184a46269118e286494e442e5f1924457bebbfb271b139403

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ct_serialcommands-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c0d37b93a1caba980c591784a6112b1221fe509ab5625572dcd5313397503a9a
MD5 7881697f2343d3a13196843cbe96e546
BLAKE2b-256 55b7839d267e811d10e9e9a1896c2617f2d0b190e2104a36a8fbec01ac324318

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