Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while (Serial.available()
#1
Hi. I'm receiving serial data from an Arduino over USB the data comes at anytime, could be 30 minutes between messages. I have the following while loop that does the job but it prints an empty line ever loop.  On the Arduino I can do "while (Serial.available()" How could this be done in Python?

Thanks.

while 1:
        try:
            msg = ser.readline()
            msg=msg.decode('utf-8')
            print(msg)
            time.sleep(1)
        except ser.SerialTimeoutException:
            print("Data could not be read")
        time.sleep(1)
Reply
#2
I'm doing something similar with a PicAXE over bluetooth, and found the ser.inWaiting() of use

while 1:
      while ser.inWaiting() > 20:
Reply
#3
(Feb-09-2017, 05:20 PM)DougArndt Wrote: I'm doing something similar with a PicAXE over bluetooth, and found the ser.inWaiting() of use

while 1:
      while ser.inWaiting() > 20:

Hi thanks for that I did this.

while 1:
        try:
            msg = ser.readline()
            msg=msg.decode('utf-8')
            if len(msg) > 0:
                print(msg)
 
Now that only goes to print if msg is > than 0.
That stopped the code printing empty lines.
I will use your code next time.
holt24.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,907 Aug-24-2023, 07:56 AM
Last Post: gowb0w

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020