Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import serial
- import threading
- from queue import Queue
- class SerialPort(object):
- def ___init__(self, timeout=None):
- self.ser = serial.Serial(baud=_, stopbits=_, ... timeout=timeout)
- self.out_q = Queue()
- self.in_q = Queue()
- self.THREAD = None
- def setup(self, com):
- self.ser.port = com
- self.ser.open()
- def run(self):
- self.THREAD = threading.Thread(target=self.listen, args=(self.out_q, self.in_q,))
- self.THREAD.start()
- def listen(self, in_q, out_q):
- while True:
- if not in_q.empty():
- # This code is never reached, even though it should be
- message = in_q.get()
- if message == 'DIE':
- break
- else:
- self.ser.write(message)
- def send_command(self, command):
- self.in_q.put(command)
- class GUI(object):
- def __init__(self):
- self.POWER = False
- self.server_port = SerialPort(timeout=0.1)
- self.client_port = SerialPort(timeout=0.1)
- #etc etc Tkinter stuff and things
- def on_power_button_click(self):
- # Tkinter Button already made, know the button works as expected
- self.POWER = not self.POWER
- if self.POWER:
- self.server_port.setup('COM5')
- self.client_port.setup('COM6')
- self.server_port.run()
- self.client_port.run()
- else:
- self.server_port.send_command('DIE')
- self.client_port.send_command('DIE')
- time.sleep(0.3)
- self.server_port.ser.close()
- self.client_port.ser.close()
- my_example_problem = GUI()
- # Creates threads and 'turns on' the application
- my_example_problem.on_power_button_click()
- # Should Turn off the application but doesn't
- my_example_problem.on_power_button_click()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement