【发布时间】:2014-12-04 06:13:49
【问题描述】:
我在执行 serial.readline() 时得到随机字符,有时是其他数字,有时是整个消息。输出应该是“1,2”或“2,2”
以下是 serial.readline() 输出的几个屏幕截图。 我试图在 serial.readline() 之前延迟,但这并没有什么不同。 开头通常有一个奇怪的字符:
我还收到了奇怪的消息:
程序稍后出现问题导致程序提交,因为有时我只收到一个空行。
有没有办法从串口获得一致的输出?
这里是arduino代码:
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
Serial.println("1,2");
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
Serial.println("2,2");
}
}
这里是python代码:
ser = serial.Serial('/dev/ttyUSB0', 9600)
line=ser.readline()
coord= line.strip()
print coord
编辑: 我尝试将 ser.flushInput() 放在 ser.open() 之后,得到相同的输出。
【问题讨论】:
-
时间延迟不起作用。我将如何实现 while (!Serial);在 python 中?
-
我认为这个实现不是在 Python 上,而是在 Arduino 上。或许能帮到你arduino.cc/en/Serial/IfSerial
-
当我将它添加到 arduino 代码时它仍然不起作用
-
你试过用 print 代替 println 吗?
标签: python serialization serial-port arduino raspberry-pi