Package: arduinopythonserialrpc
ArduinoPythonSerialRpcArduinoPythonSerialRpc 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.ArchitectureTbdfFeaturesBidirectional communicationLow Arduino resources requiredEasy to learn interfaceFlexible naming conventionGetting StartedArduino Side InstallationIt is required to:download the Arduino library from:https://github.com/Mauxilium/ArduinoSerialRpcexpand it into your "library" path of Arduino Ide,
or follows the manual installation section ofhttps://www.arduino.cc/en/guide/librariesPython Side Installationpip install arduinopythonserialrpcArduino Sketch basic example#include<ArduinoSerialRpc.h>ArduinoSerialRpcrpc("Arduino www.mauxilium.it");voidsetup(){Serial.begin(9600);rpc.registerArduinoFunction("go",startTest);rpc.registerArduinoFunction("halt",stopTest);}// ##########################################################################// BE AWARE!!!// DO NOT FORGET TO ADD THE FOLLOWING serialEvent() FUNCTION INTO YOUR SKETCHvoidserialEvent(){rpc.serialEventHandler();}// ##########################################################################boolexecFlag=false;intcounter=0;voidstartTest(){execFlag=true;}voidstopTest(){counter=0;execFlag=false;}voidloop(){delay(200);if(execFlag){rpc.executeRemoteMethod("ping_from_arduino",counter,counter++);}}Python Class basic exampleA 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 runInside the GitHub repository:https://github.com/Mauxilium/ArduinoPythonSerialRpcYou can find another real complete use case in the source paths:ArduinoPythonSerialRpc\tests\integration\sketchArduinoPythonSerialRpc\tests\integration\pythonIn order to execute this integration example test, please follow this steps:Open the sketch ArduinoPythonSerialRpc\tests\integration\sketch\sketch.inoDownload it into your Arduino CardOpen a terminal and go to ArduinoPythonSerialRpc\tests\integration\pythonModify the port name on row 158 of integration_test.py according to your systemExecute the following commandpythonintegration_test.pyNext stepsArduinoPythonSerialRpc 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)