Skip to content

Commit

Permalink
Merge pull request #3 from sahilkhanna/feat/display-incoming-serial-data
Browse files Browse the repository at this point in the history
-implemented display of data
  • Loading branch information
sahilkhanna authored Jul 11, 2022
2 parents 62474d0 + b58c527 commit e63b5c4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
32 changes: 21 additions & 11 deletions project/controller/serialcontroller.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
'''\
Expand All @@ -95,23 +98,30 @@ 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')

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):
'''\
Expand Down
7 changes: 6 additions & 1 deletion project/view/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit e63b5c4

Please sign in to comment.