SHARE
TWEET

Untitled

a guest Jul 18th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import serial
  2. import threading
  3. from queue import Queue
  4.  
  5. class SerialPort(object):
  6.     def ___init__(self, timeout=None):
  7.         self.ser = serial.Serial(baud=_, stopbits=_, ... timeout=timeout)
  8.         self.out_q = Queue()
  9.         self.in_q = Queue()
  10.         self.THREAD = None
  11.  
  12.     def setup(self, com):
  13.         self.ser.port = com
  14.         self.ser.open()        
  15.  
  16.     def run(self):
  17.         self.THREAD = threading.Thread(target=self.listen, args=(self.out_q, self.in_q,))
  18.         self.THREAD.start()
  19.  
  20.     def listen(self, in_q, out_q):
  21.         while True:
  22.             if not in_q.empty():
  23.                 # This code is never reached, even though it should be
  24.                 message = in_q.get()
  25.                 if message == 'DIE':
  26.                     break
  27.                 else:
  28.                     self.ser.write(message)
  29.  
  30.     def send_command(self, command):
  31.         self.in_q.put(command)
  32.  
  33.  
  34. class GUI(object):
  35.     def __init__(self):
  36.         self.POWER = False
  37.         self.server_port = SerialPort(timeout=0.1)
  38.         self.client_port = SerialPort(timeout=0.1)
  39.         #etc etc Tkinter stuff and things
  40.  
  41.     def on_power_button_click(self):
  42.         # Tkinter Button already made, know the button works as expected
  43.         self.POWER = not self.POWER
  44.         if self.POWER:
  45.             self.server_port.setup('COM5')
  46.             self.client_port.setup('COM6')
  47.             self.server_port.run()
  48.             self.client_port.run()
  49.         else:
  50.             self.server_port.send_command('DIE')
  51.             self.client_port.send_command('DIE')
  52.             time.sleep(0.3)
  53.             self.server_port.ser.close()
  54.             self.client_port.ser.close()
  55.  
  56. my_example_problem = GUI()
  57. # Creates threads and 'turns on' the application
  58. my_example_problem.on_power_button_click()
  59. # Should Turn off the application but doesn't
  60. my_example_problem.on_power_button_click()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top