def connect_comport():
    global w
    if serialport.isportopen():  # if port open close it
        w.led_connect.configure(background="white")
        w.btn_Connect.configure(text="""Connect""")
        print("PM2_support.connect_comport")
        updatetitle("Disconnected")
        serialport.close_serial()
    else:
        serialconfig = comportconfig_support.Readconfig()  # otherwise open it
        print("Open serial settings", serialconfig)
        print("PM2_support.connect_comport")
        sys.stdout.flush()
        updatetitle("Open Comport1 " + "," + serialconfig["baudrate"] + "," + serialconfig["comport"])
        serialport.open_serial()
        w.btn_Connect.configure(text="""Disconnect""")
        w.led_connect.configure(background="green")

    sys.stdout.flush()
def do_update():
    global t
    while 1:
        #        print('Thread form update')
        time.sleep(formrefreshtime)
        if serialport.isportopen():
            w.led_connect.configure(background="green")  # connected indicator
        else:
            w.led_connect.configure(background="white")

        if serialport.pmstatus.batterystatus[0] == 1:  # bat1 indicator
            w.e_bat1.configure(background="red")
        else:
            w.e_bat1.configure(background="white")

        if serialport.pmstatus.batterystatus[1] == 1:  # bat2 indicator
            w.e_bat2.configure(background="red")
        else:
            w.e_bat2.configure(background="white")

        if serialport.pmstatus.outputstatus[0] == 1:  # out1 indicator
            w.e_out1.configure(background="red")
        else:
            w.e_out1.configure(background="white")

        if serialport.pmstatus.outputstatus[1] == 1:  # out2 indicator
            w.e_out2.configure(background="red")
        else:
            w.e_out2.configure(background="white")

        if serialport.pmstatus.outputstatus[2] == 1:  # out3 indicator
            w.e_out3.configure(background="red")
        else:
            w.e_out3.configure(background="white")

        if serialport.pmstatus.outputstatus[3] == 1:  # out4 indicator
            w.e_out4.configure(background="red")
        else:
            w.e_out4.configure(background="white")

        if serialport.pmstatus.outputstatus[4] == 1:  # out5 indicator
            w.e_out5.configure(background="red")
        else:
            w.e_out5.configure(background="white")

        if serialport.pmstatus.outputstatus[5] == 1:  # out6 indicator
            w.e_out6.configure(background="red")
        else:
            w.e_out6.configure(background="white")

        w.text1.delete(1.0, END)  # erase text field for new round
        dline = (
            serialport.pmstatus.version[0] + " " + serialport.pmstatus.version[1] + " " + serialport.pmstatus.version[2]
        )
        w.text1.insert(END, dline + "\r\n\r\n")

        for index in range(16):  # print adc values
            dline = (
                str(index)
                + " {:>10}".format(serialport.pmstatus.adclabels[index])
                + " %6.3f" % (serialport.pmstatus.adc_avg[index])
            )
            dline = dline + " %6.3f" % (serialport.pmstatus.adc_min[index])
            dline = dline + " %6.3f" % (serialport.pmstatus.adc_max[index])
            w.text1.insert(END, dline + "\r\n")