importPySimpleGUIassgimportserialimportserial.tools.list_portsimporttimeimportthreadingcoms=serial.tools.list_ports.comports()comlist=[]forcomincoms:comlist.append(str(com.device))sg.theme('DarkBlack')layout=[[sg.Text('COMポート'),sg.Combo(comlist,size=(10,1),key='selectCOM',enable_events=True),sg.Text('',size=(16,1),key='com_state'),sg.InputText('',size=(18,1),key='uplink'),sg.Button('送信',key='send')],[sg.Output(size=(97,10))]]window=sg.Window('Serial Monitor',layout,enable_close_attempted_event=True)ser=serial.Serial()ser.baudrate=115200ser.timeout=0.1defGUIloop():whileTrue:event,values=window.read()ifevent=="-WINDOW CLOSE ATTEMPTED-":if(sg.popup_ok_cancel("終了しますか?")=='OK'):ser.close()window.close()breakifevent==sg.WIN_CLOSED:#ウィンドウのXボタンを押したときの処理breakifevent=='selectCOM':ifser.is_open:ser.close()ser.port=values['selectCOM']ser.open()ifevent=='send':try:ser.write(values['uplink'].encode())window['uplink'].update('')except:print('err')defCOMloop():whileTrue:ifser.is_open:window['com_state'].update('connected')try:text=ser.readline().decode('UTF-8').rstrip()print(text)except:time.sleep(0.1)else:window['com_state'].update('disconnected')time.sleep(0.1)thread_GUI=threading.Thread(target=GUIloop)thread_COM=threading.Thread(target=COMloop,daemon=True)thread_GUI.start()time.sleep(0.5)thread_COM.start()time.sleep(1)