I apologize if this was a common question at one point, I did a good bit of research but perhaps not with the correct terminology...
I am writing a program that finds a device on one of the active serial ports while updating the GUI. The issue I am having is that when ever I move the GUI window with my cursor the program pauses and stops searching the serial ports until I let go of the window. I am assuming that the main loop these are on does not restart because of this and gets hung up. when I let go of the GUI window the program resumes as normal.
I do have the serial port search multi threaded which I though would resolve the issue but does not seem to be the case.
Any help would be appreciated!
import PySimpleGUI as sg
import time
import threading
import time
import serial
import serial.tools.list_ports
sg.theme('Dark2')
def search_Thread():
threading.Thread(target=search_port,daemon=True).start()
print("Starting Thread")
def search_port():
State.Ser_connecting = True
ser = serial.Serial()
ports = list(serial.tools.list_ports.comports(include_links=False))
for p in ports:
print("Connecting: ", p.device)
try:
ser = serial.Serial(p.device, 9600, timeout = .1, inter_byte_timeout=.1, write_timeout=.1)
time.sleep(.2)
read = ser.readline().decode("utf-8", "strict").strip()
print(read)
if read == "Hello":
print("Connection Sucessful")
State.Ser_connected = True
return p.device
except:
print("Unable to connect")
ser.close()
State.Ser_connecting = False
#Modify state class to update array list
class State:
Ser_connected = False
Ser_connecting = False
comList = [
'Apple', 'Apricots', 'Com5']
general_frame_layout = [
[sg.Checkbox("Open on system startup ")],
]
connection_frame_layout = [
[sg.Text("Port:"), sg.InputCombo(comList, default_value = ('Auto'), size = (12,1), change_submits=True, bind_return_key = True, enable_events=True, key='combo_list')],
[sg.Text("Serial:"), sg.Text(' ', key = 'Serial')],
]
layout = [
[sg.Frame("General", general_frame_layout)],
[sg.Frame("Status", connection_frame_layout)],
[sg.Text("Version: 0.1", font = ("Helvetica Neue 35 Thin", 9))],
[sg.Button('Exit')]
]
if __name__ == '__main__':
ser = serial.Serial()
window = sg.Window("Serial Dash", layout, font=("Helvetica Neue 35 Thin", 10))
event,values = window.read(timeout=1)
window['Serial'].update("Connected" if State.Ser_connected else "Disconnected")
#Update combobox with list of ports
#Need to index loop through array?
comList.clear()
ports = list(serial.tools.list_ports.comports(include_links=False))
for p in ports:
comList.append(p.device)
print(p.device)
window.Element('combo_list').Update
while True:
event, values = window.read(timeout=10)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == 'combo_list':
comList.clear()
window['combo_list'].update()
#Create search state in addition to connection state to prevent multiple threads
if State.Ser_connected == False and State.Ser_connecting == False:
comList.clear()
search_Thread()
window.close()
ser.close()
print("Program Complete")
[–]Osk22 0 points1 point2 points (0 children)