0

i'm sending data from Arduino to my raspberry pi3 model B via USB serial. I read the data from Arduino with a python code that prints me the data. But when i print the data this is my output:

b'5\r\n'
b'6\r\n'
b'7\r\n'
b'8\r\n'
b'9\r\n'
b'10\r\n'
b'11\r\n'

this is my Arduino code:

int a = 0;
void setup(){
        Serial.begin(9600);

}
void loop(){
        Serial.println(a);
        delay(500);
        a++;
}

And this is my python code:

import serial

while True:
    ser = serial.Serial('/dev/ttyACM0', 9600)
    valore = ser.readline()
    print(valore)

how can I print just the numbers? Thank you very much :)

0

1 Answer 1

1

You need to decode the return value into a string, and you need to strip the end-of-line marker. So:

import serial

while True:
    ser = serial.Serial('/dev/ttyACM0', 9600)
    valore = ser.readline().decode().strip()
    print(valore)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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