【问题标题】:Random character at the beginning of serial.readline() in python and arduinopython和arduino中serial.readline()开头的随机字符
【发布时间】: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


【解决方案1】:

你是否刷新了串行缓冲区

ser.flushInput()

【讨论】:

  • 我尝试将 ser.flushInput() 放在 ser.open() 之后,得到相同的输出
【解决方案2】:

在 pyserial 和 Arduino 之间进行接口时,我遇到了同样的问题。这可能是一个旧帖子,但如果其他人遇到同样的问题,我通过插入来解决我的问题:

ser.flushInput()

...就在我的ser.readline() 通话之前。

【讨论】:

    【解决方案3】:

    How to Read serial data from an Arduino in Linux

    这里没什么特别的。我正在获取当前端口配置,将输入/输出速度设置为 9600 波特,将数据预期设置为每字 8 位,没有奇偶校验位和停止位,设置规范模式,然后将这些更改提交回串口。

    如果我没记错的话,你必须在通过串口连接时更改上述设置。

    你没有提到它,但我猜你正在使用 pySerial 库。无论如何,您都可以使用正确的设置来通过串行连接。 API 的构造函数允许下面提到的所有参数: Pyserial Library

    我没有测试过这种方法,因为我在 C 而不是 Python 中遇到过类似的问题。

    【讨论】:

      【解决方案4】:

      如果你把它分解成更简单的东西会发生什么?

      在 Arduino 上:

      void setup()
      {
        Serial.begin(9600);
      }
      
      int i = 0;
      void loop()
      {
        delay(1000);
        Serial.print("Hello World ");
        Serial.println(i);
        i++;
      }
      

      在 Python REPL 中...

      import serial
      ser = serial.Serial('/dev/ttyUSB0', 9600)
      while(1):
          ser.readline()
      

      您应该观察到的第一个行为是,当您启动串行连接时,Arduino 会重新启动。这需要几秒钟。

      您应该观察到的第二个行为是(如果您在 REPL 中缓慢地键入此内容)当 while(1) 循环开始时,您将获得自启动串行连接以来累积的所有串行数据。我需要几秒钟来输入所有内容,所以当我在 ser.readline() 之后按 Enter 时,我看到:

      'Hello World 1\r\n'
      'Hello World 2\r\n'
      'Hello World 3\r\n'
      

      我提到这一点只是为了确保您熟悉上次我弄乱与 Arduino 的串行通信时让我感到有些痛苦的两件事:连接后需要时间重新启动,以及串行数据缓冲区。 readline() 只会在缓冲区中为您提供一行 - 而不是缓冲区中的所有内容。

      您是否有可能在重新启动之前尝试发送/接收数据?按钮弹跳怎么样 - 十几个高/低状态检测会搞砸一些事情吗?

      【讨论】:

        猜你喜欢
        • 2017-05-06
        • 1970-01-01
        • 1970-01-01
        • 2023-02-08
        • 2011-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-26
        相关资源
        最近更新 更多