darran
darran

Reputation: 143

Python - pySerials inWaiting() always return 0

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.

Upvotes: 5

Views: 6877

Answers (3)

Itay Gal
Itay Gal

Reputation: 322

Calling the flushInput flushOutput immediately after opening the port gave this problem also. Introducing 1 sec. sleep before flushing solved it for me.

Raspberrypi4 with arduino via usb is the serial port.

Upvotes: 0

Haowen Wang
Haowen Wang

Reputation: 51

try this

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

should be working now as expected.

Upvotes: 0

darran
darran

Reputation: 143

This was embarrassing. I had another older version running in the background (on autostart so I didn't remember it was running) that was taking all the received bytes and leaving none for the newer script. So, anyone know how to remove questions?

Upvotes: 4

Related Questions