4

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) :

enter image description here

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:

enter image description here

9
  • 1
    This is not your real code. The screenshot doesn't match the print in your program. Commented Jan 17, 2017 at 0:02
  • 2
    you sent it as decimal value but after receiving you convert as hex value int(..., 16) ? Are you sure you don't run code with a++; ? Commented Jan 17, 2017 at 0:07
  • I know, i wrote the print statement here, but it's the same, the last print statement is commented (in C comment), too i know, but that's the code running basically Commented Jan 17, 2017 at 0:09
  • 1
    when you send decimal 10 then you receive it as 10 but int(10, 16) gives 16 so it skips some numbers. Commented Jan 17, 2017 at 0:11
  • 1
    I was asking if you use a++; and Serial.println(++a); at the same time - because it could give incrementing by 2. Commented Jan 17, 2017 at 0:13

1 Answer 1

2

Ok, basically what happens here: when you do something like that:

Serial.println(a);

It sends the number as an ASCII encoded decimal, so basically if you send the integer value 12 it will send the string "12". The incrementing part works just fine here.

Why does the skipping happen? If you look closely at your code you can see, that in each iteration of the while loop there are two calls to ser.readline() this makes the skipping of every second number happen.

The reason for the larger gaps is that you receive a decimal string and treat it as an HEX string. See what happens here:

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

In decimal it's just a difference of two, but in hex it's a difference of eight - this explains the bigger gaps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot, the problem was with this line: s[0] = str(int (ser.readline(),16)) , i changed it to s[0] = str(int (read_serial,16))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.