I am trying to establish a connection between Raspberry PI and an Ardunio, where Python is running on the PI. the goal is to verify that Arduino is connected and sending correct values, the connection seems to be established as I am receiving on the Python terminal, but the a value is not coming correctly, it is incrementing by 2, sometime more than two.
Can this be a connection delay issue? a serial (USB) issue?
serial_text.ino (running on Arduino) :
char dataString[50] = {0};
int a =0;
void setup() {
Serial.begin(9600); //Starting serial communication
}
void loop() {
// a++; // a value increase every loop
//sprintf(dataString,"%02X",a); // convert a value to hexa
Serial.println(++a); // send the data
delay(1000); // give the loop some break
}
Serial_test.py (Running on the PI) :
import serial
ser = serial.Serial('/dev/ttyACM0',9600)
s = [0,1]
while True:
read_serial=ser.readline()
s[0] = str(int (ser.readline(),16))
print "a value from controller = ", s[0]
//print read_serial
OUTPUT (on PI screen) :
UPDATE: i made some changes, but it's still the same problem , here are the changes:
serial_text.ino (running on Arduino) :
int a =0;
void setup() {
Serial.begin(9600); //Starting serial communication
}
void loop() {
a++;
Serial.println(a); // send the data
delay(1000); // give the loop some break
}
For Python, I only changed this line:
s[0] = str(int (ser.readline(),16))
TO:
s[0] = str(int (ser.readline(),10))
I also removed //print read_serial just in case.
OUTPUT:


printin your program.int(..., 16)? Are you sure you don't run code witha++;?10then you receive it as10butint(10, 16)gives16so it skips some numbers.a++;andSerial.println(++a);at the same time - because it could give incrementing by 2.