Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can clear buffer #12

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion project/controller/maincontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def update_serial_cb(self, handlePacketCb, connectionCb, disconnectionCb):
self._serialController.connection_callback = connectionCb
self._serialController.disconnection_callback = disconnectionCb

def clear_serial_rx_buffer(self) -> None:
self._serialController.clear_rx_buffer()

def save_project_settings(self, filename:str):
self._mainModel.save_project_file(filename)
Expand All @@ -40,4 +42,5 @@ def get_saved_port_name(self)->str:
return self._mainModel.get_port_name()

def list_serial_ports(self) -> list:
return self._serialController.list_serial_ports()
return self._serialController.list_serial_ports()

20 changes: 14 additions & 6 deletions project/controller/serialcontroller.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from time import sleep
from datetime import datetime
from serial import Serial, SerialException
from serial.threaded import Protocol, ReaderThread
from serial.tools import list_ports
Expand All @@ -17,12 +18,15 @@ def __init__(self, controller):
self.controller = controller
self.transport = None
self.isConnected = False


def clear_buffer(self):
self.buffer = bytearray()

def connection_made(self, transport):
"""Called when reader thread is started"""
super(FixedLengthPacketHandler, self).connection_made(transport)
self.transport = transport
self.buffer = bytearray()
self.clear_buffer()
self.isConnected = True
if self.controller and self.controller.connection_callback:
self.controller.connection_callback()
Expand All @@ -32,13 +36,13 @@ def chunk_and_handle_packets(self, big_buff:bytearray)->bytearray:
big_buff = big_buff[self.PACKET_LENGTH:]
if self.controller and self.controller.handle_packet:
convhex = " ".join(["{:02x}".format(bytes) for bytes in small_buff])
self.controller.handle_packet('[RX]: ' + convhex.upper())
self.controller.handle_packet(f'[RX - {datetime.now()}]: {convhex.upper()}')
while len(big_buff)>=len(small_buff):
small_buff = big_buff[:self.PACKET_LENGTH]
big_buff = big_buff[self.PACKET_LENGTH:]
if self.controller and self.controller.handle_packet:
convhex = " ".join(["{:02x}".format(bytes) for bytes in small_buff])
self.controller.handle_packet('[RX]: ' + convhex.upper())
self.controller.handle_packet(f'[RX - {datetime.now()}]: {convhex.upper()}')
return big_buff

def data_received(self, data):
Expand All @@ -51,7 +55,6 @@ def data_received(self, data):
if len(self.buffer) >= self.PACKET_LENGTH:
self.buffer=self.chunk_and_handle_packets(self.buffer)


def send_data(self, data):
"""\
Called when serialport needs to send data. You may format it
Expand All @@ -65,7 +68,7 @@ def connection_lost(self, exc):
otherwise.
"""
self.transport = None
self.buffer = bytearray()
self.clear_buffer()
self.isConnected = False
if self.controller and self.controller.disconnection_callback:
self.controller.disconnection_callback()
Expand Down Expand Up @@ -137,6 +140,7 @@ def connect(self):
Attempts to connect to the specified serial port
'''
try:
self.clear_rx_buffer()
self._rt.start()
except AttributeError as e:
print(f'Cannot connect to port:{self._selectedPortName}, {e}')
Expand All @@ -146,6 +150,7 @@ def disconnect(self):
Attempts to disconnect to the specified serial port
'''
if self._proto is not None:
self.clear_rx_buffer()
try:
self._rt.close()
except Exception as e:
Expand All @@ -169,6 +174,9 @@ def serial_packet_handler(self):
self._proto = FixedLengthPacketHandler(self)
return self._proto

def clear_rx_buffer(self):
if self._proto is not None:
self._proto.clear_buffer()

@staticmethod
def list_serial_ports() -> list:
Expand Down
5 changes: 4 additions & 1 deletion project/view/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ def enable_menu_item(self, menuElement, enable=True):
# # self.MENU_DEF[idx]='!'+self.MENU_DEF[idx]
# break

def clear_btn_pressed(self):
self._ui[self.KEY_CONSOLE].update(value='')
self._controller.clear_serial_rx_buffer()
def open_project_file(self):
filename = gui.popup_get_file('file to open', file_types=(( 'Porty Project (.prtyprj)','.prtyprj'),), no_window=True)
self._controller.open_project_settings(filename)
Expand Down Expand Up @@ -194,7 +197,7 @@ def launch(self):
else:
self.open_port_connection()
elif event == self.KEY_CLEAR_TERMINAL:
self._ui[self.KEY_CONSOLE].update(value='')
self.clear_btn_pressed()
elif event == self.KEY_ADD_MSG_BTN:
self.update_msg_list(values[self.KEY_SEND_MSG_INPUT], True)
elif event == self.KEY_REMOVE_MSG_BTN:
Expand Down