This arduino library simplifies serial communications between arduinos, or between an arduino board and a python script (https://github.com/Tahitibob35/pySerialComm).
- 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 );
....
}
void loop() {
s.check_reception();
}
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 );
}
void loop() {
s.check_reception();
}
arduino = SerialComm('/dev/ttyUSB0', baudrate=115200)
pin = 5
resp = arduino.sendmessage(2, (i,), ack=True)
values = arduino.parsedata("i", resp)
pin_value = values[0]
SerialComm s ( Serial );
char myname[] = "Arduino receiver";
void sendMyName( void ) {
s.sendAck( "s" , myname );
}
void setup( ) {
...
s.attach( 2, sendMyName );
...
}
void loop() {
s.check_reception();
}
SerialComm s ( Serial );
void loop( ) {
...
s.sendMessage( 2 , true ) ) {
char a_string[20] = "";
if ( s.sendMessage( 2 , true ) ) {
s.getData( "s" , &a_string , sizeof( a_string ) );
}
...
}
This sample communicates with the python script sample1.py (https://github.com/Tahitibob35/pySerialComm).
These samples communicate together using the SoftwareSerial library.
SerialComm s(Serial); // Use hardware serial port
int rDigitalRead( uint8_t pin );
int rAnalogRead( uint8_t pin );
void rAnalogWrite( uint8_t pin , int value);
void rDigitalWrite( uint8_t pin , int value);
void rdigitalPinState( int pin , int * rw , int * pwm_cap , int * pwm_enabled , int * value);
rw : Writable or not pwm_cap : PWM capable pin pwm_enabled : PWM enabled or not value : Current value (LOW, HIG, 0-255)
bool attach(int command, void (*ptrfonction)(void));
s.sendMessage( 2 , false );
The second argument indicates if an ack is awaited. The method returns true if an ck has been received.
s.sendMessage( 2 , false, "i", 5 );
"i" means for an integer.
s.sendMessage( 2 , false, "ii", 5, 2000 );
s.sendMessage( 2 , false, "s", a_string );
"s" means for an string.
s.sendMessage( 2 , false, "ss", a_stringA, a_stringB );
s.sendMessage( 2 , false, "is", an_integer, a_string );
s.sendMessage( 2 , false, "iiisss", an_integerA, an_integerB, an_integerC, a_stringA, a_stringB, a_stringC );
if (mySerial.available()) {
s.check_reception();
}
s.getData( "i" , &an_integer );
This method can be used in the callback function of after sending a message if an ack has been specified.
s.getData( "ii" , &an_integerA, &an_integerB );
s.getData( "s" , &a_string, sizeof(a_string) );
s.getData( "ss" , &a_stringA, sizeof(a_stringA) , &a_stringB, sizeof(a_stringB) );
s.getData( "si" , &a_string, sizeof(a_string), &an_integer );
s.getData( "sisi" , &a_stringA, sizeof(a_stringA), &an_integerA , &a_stringB, sizeof(a_stringB), &an_integerB );
s.sendAck( );
This method is used in the callback function.
s.sendAck( "i" , 5 );
s.sendAck( "s" , a_string );
s.sendAck( "ss" , a_string , an_integer );