class Model(Thread): def __init__(self, **kwargs): Thread.__init__(self) # Serial thread self.serialthread = SerialPort() # Controller Read/Write variables over serial self.controller = DistantIO() # Serial protocol self.protocol = Protocol() # Variable manager self.variable_manager = VariableManager(self) self.variable_manager.start() self.running = True; # Data logger self.logger = DataLogger() def get_ports(self): return self.serialthread.get_ports() def connect_com(self,COM_port): # Start serial thread (can run without COM port connected) if not self.serialthread.isAlive(): self.serialthread.start() self.serialthread.connect(COM_port,115200) #Model update running in a thread def run(self): while self.running: if self.serialthread.char_available(): c = self.serialthread.get_char() if not c is None: self.protocol.process_rx(c) if self.protocol.available(): p = self.protocol.get() # Dump payload if controller is in heavy load ? pub.sendMessage('new_rx_payload',rxpayload=p)#USED ? if not p is None: self.controller.decode(p) def disconnect_com(self): self.serialthread.disconnect() def stop(self): self.running = False self.serialthread.stop() self.variable_manager.stop() if self.serialthread.isAlive(): self.serialthread.join(0.1) if self.serialthread.isAlive(): self.serialthread.join(1) if self.serialthread.isAlive(): print("--- Serial thread not properly joined.") if self.variable_manager.isAlive(): self.variable_manager.join(0.1) if self.variable_manager.isAlive(): self.variable_manager.join(1) self.stop_controller() def start_controller(self): #Get command for querying variable table MCU side cmd = self.controller.encode(cmd='table') #Feed command to serial protocol payload processor frame = self.protocol.process_tx(cmd) #Send command self.serialthread.write(frame) def stop_controller(self): #TODO : Tell MCU to stop sending all data pass def start_log(self): self.logger.start() def stop_log(self): self.logger.record_all() def read_var(self, varid): # Get command cmd = self.controller.encode(cmd='read',var_id=varid) # Feed command to serial protocol payload processor frame = self.protocol.process_tx(cmd) # Send command self.serialthread.write(frame) def write_var(self,varid,value): # Get command cmd = self.controller.encode(cmd='write',var_id=varid,value=value) if cmd == None: return # Feed command to serial protocol payload processor frame = self.protocol.process_tx(cmd) # Send command self.serialthread.write(frame) def stop_read_var(self,varid): # Get command cmd = self.controller.encode(cmd='stop',var_id=varid) if cmd == None: return # Feed command to serial protocol payload processor frame = self.protocol.process_tx(cmd) # Send command self.serialthread.write(frame) def stop_read_all_vars(): # Get command cmd = self.controller.encode(cmd='stopall') if cmd == None: return # Feed command to serial protocol payload processor frame = self.protocol.process_tx(cmd) # Send command self.serialthread.write(frame) def get_var_info(self,varid): return self.controller.get_var_info(varid)
port.connect("COM5",115200) synced = False count = 0 errorcount = 0 index = 0 sequence = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] t = Timer(10.0, stop) t.start() start = time.time() t2 = Timer(1.0, printout) t2.start() while port.isAlive(): if port.char_available(): ch = port.get_char() c = struct.unpack('=B',ch)[0] if not c is None : # Sync with sequence if not synced if not synced : if c == sequence[0]: synced = True index = 1 # Look for errors else: if c == sequence[index]: index = (index + 1) % 10 count += 1 else: