1

I am working on a project using Tkinter and threads. Th project is kind of a machine to test an electronic product.

The product is inside a box with an open/close sensor. the product is powered by 220 volts

I am using a thread to monitor the state of the sensor so then when the enclosure is opened (if the value = 0 ) I should shutdown the power supply to protect the user, and this is properly working.

However when the box is opened all the test sequence should stop and the result of the test should be FAIL, but this is no happening and the thread which is managing the tests of the product keeps running (the power supply is OFF but tests are still executed)

I have to find a solution so that when the box is opened, the test sequence stops and return to the initial state.

Any help will be greatly appreciated

Thanks

import tkinter as tk
import threading
from pyModbusTCP.client import ModbusClient
import configparser
import serial


read_config = configparser.ConfigParser()
read_config.read("config.ini")
SERVER_HOST = read_config.get("IOLOGIK", "IP")
SERVER_PORT = read_config.get("IOLOGIK", "Port")
c = ModbusClient()
c.host(SERVER_HOST)
c.port(SERVER_PORT)

port = read_config.get("Configuration MOXA", "port")

address_c = int(read_config.get("I_O_U1", "adresse_cloche"))
adresse_alim = int(read_config.get("I_O_U1", "adresse_alim"))

class InputThread(threading.Thread):
    def __init__(self, threadID):
        super(InputThread, self).__init__()
        self.daemon = False
        self.c = []
        self.running = None
        self.id = threadID

    def run(self):
        if self.id == 1:
            while True:
                try:
                   self.c = c.read_discrete_inputs(address_c, 1)  # reading my variable from the iologik
                   if not self.c[0]:
                       x = c.write_single_coil(adresse_alim, 0)  # alim off
                       ser.close()
                       self.running = False
                       finish.configure(bg="red")
                       break
                   else:
                       self.running = True
                except:
                    break
        elif self.id == 2:
            start_test()


def task_serial(ser, cmd):
    if ser.isOpen:
        try:
            ser.write(cmd)
            x = ser.read()
            line = ser.readline()
            return line
        except:
            return 0


def connection_Product():
    global ser
    ser = serial.Serial()
    ser.port = port
    ser.baudrate = 19200
    ser_open = False
    while not ser_open:
        try:
            ser.open()
            ser_open = True
        except:
            ser_open = False
    res = task_serial(ser, "hh ")
    return ser


def Product_test():
    res = task_serial(ser, "aa")
    res = task_serial(ser, "bb")
    res = task_serial(ser, "cc")
    res = task_serial(ser, "dd")
    res = task_serial(ser, "ee")
    res = task_serial(ser, "ff")
    res = task_serial(ser, "hh")
    res = task_serial(ser, "ii")
    res = task_serial(ser, "jj")
    res = task_serial(ser, "kk")
    res = task_serial(ser, "ll")
    res = task_serial(ser, "mm")
    res = task_serial(ser, "nn")
    res = task_serial(ser, "oo")
    res = task_serial(ser, "pp")
    res = task_serial(ser, "qq")


def test():
    thread = InputThread(2)
    thread.start()


def start_test():
    it = InputThread()
    it.start()
    x = c.write_single_coil(adresse_alim, 1) # alim on
    while it.running:
        ser = connection_Product()
        Product_test()


window = tk.Tk()
window.geometry("2000x700")
window.title("Test")
departure = tk.Button(window, text="Depart Test", fg="#0F8DCB",font="serif 13 bold", width=15, command=test)
departure.place(x=1100, y=160)
finish = tk.Button(window, text="Fin Test", fg="#0F8DCB", font="serif 13 bold", width=15)
finish.place(x=1100, y=210)
window.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 2
    Does this answer your question? [Is there any way to kill a Thread?](https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread) – quamrana Jun 07 '21 at 13:32
  • try this https://stackoverflow.com/questions/43033280/python-tkinter-stop-a-threading-function – Code Cracker Jun 07 '21 at 13:36
  • @CodeCracker this method won't really help I will just keep verifying the opening of the box(if it.running == False) in every line of the Product_test function. which is not an efficient solution. – Marry Jones Jun 08 '21 at 21:38

0 Answers0