What causes C to not increase the validity of a variable in Arduino?

I am trying to establish a connection between Raspberry PI and Ardunio, where Python runs on PI. the goal is to check that the Arduino is connected and sending the correct values, the connection seems to be established as I receive on the Python terminal, but the value adoes not fit correctly, it increases by 2, sometimes more than two, Could this be a delay problem connections? serial (USB) problem?

serial_text.ino (works 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 (works on 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

EXIT (on the PI screen):

enter image description here

UPDATE: I made some changes, but this is still the same problem, here are the changes:

serial_text.ino (works 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 just changed this line:

s[0] = str(int (ser.readline(),16))

TO:

s[0] = str(int (ser.readline(),10))

//print read_serial .

:

enter image description here

+4
1

, , : - :

Serial.println(a);

, ASCII, , 12, "12". .

? , , while ser.readline(), .

, HEX. , :

>>> int("49", 16)
73
>>> int("51", 16)
81

, - .

+2

Source: https://habr.com/ru/post/1666964/


All Articles