Code: Select all
def On_Connect_Click(Gui):
if(Gui.connect_btn.text() == "Disconnect"):
Gui.connect_btn.setText("Connect")
Gui.connect_btn.setStyleSheet("background-color: #FFD700; ")
Gui.write_thread.stop()
Gui.read_thread.stop()
time.sleep(0.1) # Small delay to ensure buffers are cleared
Gui.ser.close()
del Gui.ser # Remove the serial object completely
else: # (self.connect_bt.text() == "Connect")
try:
# Set up serial connection
Gui.ser = serial.Serial(Gui.port_combo.currentText(),
Gui.baud_combo.currentText(),
timeout=1,
bytesize=serial.EIGHTBITS,
stopbits=serial.STOPBITS_ONE,
parity=serial.PARITY_NONE)
Gui.ser.read_buffer_size = 4096
except Exception as e :
print("Failed")
print(e)
Gui.show_message("Connection failed. Check the Port number.", title="Connection Failed")
return
# write_thread includes Writing myltiple Regs command and Read_req command
Start_read_write_threads(Gui)
Gui.connect_btn.setText("Disconnect")
Gui.connect_btn.setStyleSheet("background-color: Red; color: white;")
time.sleep(1) #If you delete this, UI will be closed.