Python side of a serial communication library with Arduino Card
Project description
ArduinoPythonSerialRpc
ArduinoPythonSerialRpc is the Python side of a serial communication library with Arduino Card.
The Arduino part of this communication is implemented into ArduinoSerialRpc repository.
The communication model is implemented in form of:
- Remote Method Invocation. Where Arduino calls a method declared into a Python class.
- Remote Procedure Call. Where a Python process call a function defined into the Arduino sketch.
The communication is a point to point model, performed through the serial (USB) port.
Architecture
Tbdf
Features
- Bidirectional communication
- Low Arduino resources required
- Easy to learn interface
- Flexible naming convention
Getting Started
Arduino Side Installation
It is required to:
- download the Arduino library from: https://github.com/Mauxilium/ArduinoSerialRpc
- expand it into your "library" path of Arduino Ide, or follows the manual installation section of https://www.arduino.cc/en/guide/libraries
Python Side Installation
pip install arduinopythonserialrpc
Arduino Sketch basic example
#include <ArduinoSerialRpc.h>
ArduinoSerialRpc rpc("Arduino www.mauxilium.it");
void setup() {
Serial.begin(9600);
rpc.registerArduinoFunction("go", startTest);
rpc.registerArduinoFunction("halt", stopTest);
}
// ##########################################################################
// BE AWARE!!!
// DO NOT FORGET TO ADD THE FOLLOWING serialEvent() FUNCTION INTO YOUR SKETCH
void serialEvent() {
rpc.serialEventHandler();
}
// ##########################################################################
bool execFlag = false;
int counter = 0;
void startTest() {
execFlag = true;
}
void stopTest() {
counter = 0;
execFlag = false;
}
void loop() {
delay(200);
if (execFlag) {
rpc.executeRemoteMethod("ping_from_arduino", counter, counter++);
}
}
Python Class basic example
A simplified version of Python test program could be:
from threading import Event
from arduinopythonserialrpc.arduino_python_serial_rpc import ArduinoPythonSerialRpc
class ArduinoRpc(ArduinoPythonSerialRpc):
def __init__(self):
super(ArduinoRpc, self).__init__("COM5", 9600, self)
def ping_from_arduino(self, arg1: int, arg2: int) -> int:
print("Ping: "+str(arg2))
if arg1 == 15:
test_arduino_to_pc_completed.set()
return arg2
test_arduino_to_pc_completed = Event()
if __name__ == "__main__":
rpc = ArduinoRpc()
rpc.connect()
card_name = rpc.get_card_name()
print("Connected to: " + card_name)
print("First cicle of 15 pings")
rpc.execute_remote_function("go")
test_arduino_to_pc_completed.wait()
rpc.execute_remote_function("halt")
print("Second cicle of 15 pings")
test_arduino_to_pc_completed.clear()
rpc.execute_remote_function("go")
test_arduino_to_pc_completed.wait()
rpc.execute_remote_function("halt")
print("Test ends")
rpc.disconnect()
Build and run
Inside the GitHub repository: https://github.com/Mauxilium/ArduinoPythonSerialRpc
You can find another real complete use case in the source paths:
- ArduinoPythonSerialRpc\tests\integration\sketch
- ArduinoPythonSerialRpc\tests\integration\python
In order to execute this integration example test, please follow this steps:
- Open the sketch ArduinoPythonSerialRpc\tests\integration\sketch\sketch.ino
- Download it into your Arduino Card
- Open a terminal and go to ArduinoPythonSerialRpc\tests\integration\python
- Modify the port name on row 158 of integration_test.py according to your system
- Execute the following command
python integration_test.py
Next steps
- ArduinoPythonSerialRpc tutorial - A tutorial to discover a more complex use of library (On Working)
- www.mauxilium.it - The reference site for my other projects (On Working)
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
Hashes for arduinopythonserialrpc-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0667b7ce039ed1f9d8064a2ee67216f5c3400ac3d8ce44128f0e67b55567c1f |
|
MD5 | e8e9b716a07122bbc83f6f2e9ae45ed8 |
|
BLAKE2b-256 | d2b6b38881c3328f0bbd766a77d749d3a9d33989efebf0269bc7a7e33549d1ce |
Hashes for arduinopythonserialrpc-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a003b4bd10af05b7cd2536ef0cd1d821955196f428dca32f9fb842460764e2d |
|
MD5 | 856d05d04615e0554a6a6602f16464bb |
|
BLAKE2b-256 | 8e8368648d07f2fdcc3e7c58009ce5f5c1ef64ae9e4b2a731f92e6fb85606e65 |