This python module simplifies serial communications with an arduino board (https://github.com/Tahitibob35/SerialComm).
- Attach actions to functions
- Simply send and receive strings or integers
- Manage acknowledgments
Arduino receiver code
SerialComm s ( Serial );
void remoteAnalogWrite( void ) {
int pin = 0;
int value = 0;
s.getData( "ii", &pin , &value );
analogWrite( pin , value );
}
void setup( ) {
s.attach( 2 , remoteAnalogWrite );
}
Python sender script
arduino = SerialComm('/dev/ttyUSB0', baudrate=115200)
pin = 9
value = 120
resp = arduino.sendmessage(2, (pin, value), ack=False)
SerialComm s( Serial );
void remoteAnalogRead( void ) {
int pin;
s.getData( "i" , &pin );
int value = analogRead( pin );
s.sendAck( "i" , value );
}
void setup( ) {
s.attach( 2, remoteAnalogRead );
}
arduino = SerialComm('/dev/ttyUSB0', baudrate=115200)
pin = 5
resp = arduino.sendmessage(2, (i,), ack=True)
values = arduino.parsedata("i", resp)
pin_value = values[0]