Python Forum
Waiting for input from serial port, then move on
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Waiting for input from serial port, then move on
#1
Why doesn't this leave the while loop?


#wait for % and then move on
indata = ""
Counter = 0
while indata != "%":  
  indata = ser.read()
  print(indata)
  Counter +=  1
  if Counter > 5:
   sys.exit("Timed out waiting for Bootloader Active Character. Please close window and try again")
Result:

b''
b''
b'%'
b'%'
b'%'
b'%'
Timed out waiting for Bootloader Active Character. Please close window and try again
Reply
#2
serial port reads bytes, "%" is a str.

I don't think you should loop. If you are using pySerial, there is a read_until() command that should work well for you.
inp_data = ser.read_until(b"%")
This will wait forever unless you set a timeout on the port.
ser = serial.Serial(port, baudrate, timeout=1.0, ...)   # Timeout read if wait > 1 second
From your post it looks like you already set a timeout.
Reply
#3
Thanks, that did the trick
Reply
#4
(Apr-17-2024, 06:45 AM)feistycavities Wrote: The issue with your while loop not closing is because you have an embedded for loop in your code.

The cause was the condition of while. In the code there was the check of equality of
Output:
b"%" == "%"
which never is True. So the loop never break and instead sys.exit() was executed at the end.

Data over Sockets, Serial ports and Pipes are always bytes. Python 2 did not this differentiation, which was a huge problem.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  MCU reboots after opening Serial port when ran from Raspberry PI zazas321 3 452 Mar-19-2024, 09:02 AM
Last Post: zazas321
  pip stops waiting for python walker 6 1,068 Nov-28-2023, 06:55 PM
Last Post: walker
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 4,161 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Serial Port As Global Prasanjith 2 1,517 Mar-23-2023, 08:54 PM
Last Post: deanhystad
  Waiting for heavy functions question philipbergwerf 14 3,386 Apr-29-2022, 07:31 PM
Last Post: philipbergwerf
  serial input/output barryjo 3 2,528 Dec-27-2021, 11:57 PM
Last Post: barryjo
  python serial port barryjo 2 1,662 Dec-27-2021, 11:09 PM
Last Post: barryjo
  is there a way to mention port range or search for port dynamically with qConnection Creepy 0 1,494 Sep-09-2021, 03:15 PM
Last Post: Creepy
  How to create waiting process? samuelbachorik 4 1,986 Sep-02-2021, 05:41 PM
Last Post: bowlofred
  How to Properly Open a Serial Port in a Function bill_z 2 4,493 Jul-22-2021, 12:54 PM
Last Post: bill_z

Forum Jump:

User Panel Messages

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