[英]sending data from Arduino to raspberry via USB serial
我通過 USB 串行將數據從 Arduino 發送到我的樹莓派 pi3 model B。 我使用 python 代碼從 Arduino 讀取數據,該代碼打印數據。 但是當我打印數據時,這是我的 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'
這是我的 Arduino 代碼:
int a = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.println(a);
delay(500);
a++;
}
這是我的 python 代碼:
import serial
while True:
ser = serial.Serial('/dev/ttyACM0', 9600)
valore = ser.readline()
print(valore)
我怎樣才能只打印數字? 非常感謝:)
您需要將返回值解碼為字符串,並且需要去除行尾標記。 所以:
import serial
while True:
ser = serial.Serial('/dev/ttyACM0', 9600)
valore = ser.readline().decode().strip()
print(valore)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.