1

I have a "while True:" loop in python 3 that is taking some serial data from a com port and processing it. The stream of data needs a serial write to continue sending data if a specific string of bytes is read from the port. Once the stream of bytes is found and the serial write is executed, the "while True:" stops running. I think it has something to do with an input from a keyboard.

To troubleshoot, I inserted some code to manually enter the serial write, it works and then continues to read and process data. Can someone explain why the "while True:" loop ceases to run and how to fix it with a command that does not involve the keyboard?

Here is the code:

 ser = serial.Serial('/dev/ttyUSB1', 115200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0) print(ser.name) # check which port was really used while True: try: the_date = str(datetime.datetime.now()) # Read line from serial port in bytes s = ser.readline() #convert to utf-8 for use in serial operations s_text_in = s.decode('utf_8') if(s_text_in.find(com_strt_string) != -1): print("found the specific string") #all_logs_str = input("What is your name? ") #type(all_logs_str) all_logs_str = 'Send Data\r' all_logs_str_bytes = all_logs_str.encode('utf-8', "ignore") ser.write(all_logs_str.encode()) else: print("skipped") continue info_type = port_interpreter(s_text_in) print(s_text_in) if(info_type == 1): s_text = num_remover(s_text_in) err_fl.write(the_date + ' ' + s_text + '\n') print("wrote E") elif(info_type == 2): s_text = num_remover(s_text_in) war_fl.write(the_date + ' ' + s_text + '\n') print("wrote W") else: s_text = num_remover(s_text_in) info_fl.write(the_date + ' ' + s_text + '\n') print("wrote I") except KeyboardInterrupt: # Stopping flow of infinite loop. print("[CTRL+C detected]") err_fl.close() war_fl.close() info_fl.close() 

Thank you

8
  • Does it throw any errors? Otherwise it sides like it isn't exiting the loop but instead stuck somewhere. Can you paste the output of your print statements when you run it? Commented May 1, 2019 at 20:00
  • No errors are thrown. It just ceases to run. I think there is a thread running that expects some sort of input from the keyboard to continue. If you look at the commented lines where the input is, that will cause it to work Commented May 1, 2019 at 20:40
  • Uncommenting input("What is your name? ") will make it work you said? Commented May 2, 2019 at 12:44
  • Correct, But I need this to work without keyboard inputs. It's running in the background of a GUI. Thanks. Commented May 2, 2019 at 12:49
  • So does it never hit print(s_text_in) without that input? Commented May 2, 2019 at 12:49

1 Answer 1

1

The issue was resolved using pyautogui. The Ubuntu python3.6 idle thread was looking for some (any) keyboard input as the thread was paused. as soon as keyboard input was detected, it resumed. I am not sure why this occurred but that fixed the issue.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.