From b58c5275d5b2196ff65c4b162edc2ceaa741961c Mon Sep 17 00:00:00 2001 From: Sahil Date: Mon, 11 Jul 2022 10:39:20 +1000 Subject: [PATCH] -implemented display of data --- project/controller/serialcontroller.py | 32 +++++++++++++++++--------- project/view/main.py | 7 +++++- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/project/controller/serialcontroller.py b/project/controller/serialcontroller.py index 796b2a7..a5fe797 100644 --- a/project/controller/serialcontroller.py +++ b/project/controller/serialcontroller.py @@ -1,5 +1,5 @@ from time import sleep -from serial import Serial +from serial import Serial, SerialException from serial.threaded import Protocol, ReaderThread from serial.tools import list_ports @@ -33,6 +33,8 @@ def data_received(self, data): self.buffer = self.buffer[self.PACKET_LENGTH:] print(f'data:{temp_buff}') # TODO: Handle this + if self.controller and self.controller.handle_packet: + self.controller.handle_packet(temp_buff) def send_data(self, data): @@ -84,6 +86,7 @@ def __init__(self, comPortName:str = None, conf:dict = None) -> None: self.connection_callback = lambda : print('connection_callback') self.disconnection_callback = lambda : print('disconnection_callback') + self.handle_packet = lambda : print('handle_packet') def set_comport(self, comPortName): '''\ @@ -95,14 +98,19 @@ def set_comport(self, comPortName): self.disconnect() self._sp = None self._selectedPortName = comPortName - self._sp = Serial(self._selectedPortName) - self._sp.baudrate = self._conf['baudrate'] - self._sp.bytesize=self._conf['bytesize'] - self._sp.parity=self._conf['parity'] - self._sp.stopbits=self._conf['stopbits'] - self._sp.timeout=self._conf['timeout'] - self._sp.xonxoff=self._conf['xonxoff'] - self._sp.rtscts=self._conf['rtscts'] + try: + self._sp = Serial(self._selectedPortName) + self._sp.baudrate = self._conf['baudrate'] + self._sp.bytesize=self._conf['bytesize'] + self._sp.parity=self._conf['parity'] + self._sp.stopbits=self._conf['stopbits'] + self._sp.timeout=self._conf['timeout'] + self._sp.xonxoff=self._conf['xonxoff'] + self._sp.rtscts=self._conf['rtscts'] + self._rt = ReaderThread(self._sp, self.serial_packet_handler) + except SerialException as e: + print(f'Cannot open COM Port:{self._selectedPortName}, {e}') + else: raise ValueError('Provided port doesn\'t exist') @@ -110,8 +118,10 @@ def connect(self): '''\ Attempts to connect to the specified serial port ''' - self._rt = ReaderThread(self._sp, self.serial_packet_handler) - self._rt.start() + try: + self._rt.start() + except AttributeError as e: + print(f'Cannot connect to port:{self._selectedPortName}, {e}') def disconnect(self): '''\ diff --git a/project/view/main.py b/project/view/main.py index 1964825..63277f0 100644 --- a/project/view/main.py +++ b/project/view/main.py @@ -59,11 +59,16 @@ def close_port_connection(self): self._isConnected = False self.refresh_port_list() + def update_console(self, msg:bytearray): + self._consoleBuffer = msg.decode("utf-8") + self._ui[self.KEY_CONSOLE].print(msg) def launch(self): + self._controller.handle_packet=self.update_console + event, values = self._ui.read(timeout=10) + self.refresh_port_list() while True: event, values = self._ui.read(timeout=10) - # print(f'event = {event}, values = {values}') if event == gui.WIN_CLOSED: break elif event == self.KEY_REFRESH_PORT_LIST: