user17319247
user17319247

Reputation:

sending data from Arduino to raspberry via USB serial

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

Upvotes: 0

Views: 130

Answers (1)

larsks
larsks

Reputation: 311606

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)

Upvotes: 1

Related Questions