4

I'm trying to make a small program that receives messages from the serial port, and doing it periodically. Right now I'm not saving anything, I'm just trying to see if I get anything at all, so I tried this code:

def ReceiveRS():
   global ser
   while ser.inWaiting() > 0:
      print(ser.read(1))

ser is the serial port, which is correctly initialized, as it has worked before and I can send stuff. After trying some different things I have found out that inWaiting() never seems to return anything but 0. Anyone have any ideas as to why and how to fix it?

Oh, and I'm using Python 3.2.3, with pySerial on a Raspberry PI.

4

2 回答 2

4

这很尴尬。我在后台运行了另一个旧版本(在自动启动时,所以我不记得它正在运行),它占用了所有接收到的字节,而没有为新脚本留下任何字节。那么,有人知道如何删除问题吗?

于 2012-11-28T10:54:16.683 回答
0

尝试这个

while (True):
      if ser.inWaiting() > 0:
          print(ser.read(1))

现在应该按预期工作。

于 2018-12-25T03:46:41.830 回答