William Dussault
William Dussault

Reputation: 375

Why is "if "statement is causing "While True:" Loop to cease operation

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

Upvotes: 1

Views: 60

Answers (1)

William Dussault
William Dussault

Reputation: 375

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.

Upvotes: 1

Related Questions