Skip to content
Snippets Groups Projects
Commit 580ac29b authored by Roberto-R's avatar Roberto-R
Browse files

Added block processing of incoming bytes - Problem now is that frames will be...

Added block processing of incoming bytes - Problem now is that frames will be skipped if multiple come in at once, since the processor only returns once
parent 2f5bf739
No related branches found
No related tags found
No related merge requests found
......@@ -42,54 +42,75 @@ class PlotDecoder:
if new_mode == Mode.DATA:
self.data = []
def receive_byte(self, byte: bytearray) -> bool:
"""Precess new incoming byte.
def receive_bytes(self, new_bytes: bytearray) -> bool:
"""Precess a set of incoming bytes.
Return true when a complete package was received
Return true when a complete package was received.
"""
if self.mode == Mode.HEADER: # If header not passed
i = 0 # Index inside set of bytes
# Increment header
if byte == self.header_bytes[self.bytes_count]:
self.bytes_count += 1
else:
self.bytes_count = 0 # Reset, header failed
new_size = len(new_bytes)
if self.bytes_count >= 3: # Header completed
self.set_state(Mode.SIZE)
new_packet = False
return False
while i < new_size:
if self.mode == Mode.SIZE:
self.channel_size = int.from_bytes(byte, byteorder='big', signed=False)
self.set_state(Mode.TIME)
if self.mode == Mode.HEADER: # If header not passed
return False
# Increment header
if new_bytes[i] == self.header_bytes[self.bytes_count]:
self.bytes_count += 1
else:
self.bytes_count = 0 # Reset, header failed
if self.mode == Mode.TIME:
self.buffer.append(byte[0])
if self.bytes_count >= 3: # Header completed
self.set_state(Mode.SIZE)
if len(self.buffer) == 4:
# print(''.join('{:02x}'.format(x) for x in self.buffer))
i += 1
continue
self.time = int.from_bytes(self.buffer, byteorder='big', signed=True)
self.set_state(Mode.DATA)
if self.mode == Mode.SIZE:
self.channel_size = new_bytes[i]
self.set_state(Mode.TIME)
return False
i += 1
continue
if self.mode == Mode.DATA:
self.buffer.append(byte[0])
if self.mode == Mode.TIME:
if len(self.buffer) == 4:
value = struct.unpack('f', self.buffer)
self.buffer = bytearray()
self.data.append(value)
# Copy until buffer contains 4 bytes or until new segment is depleted
copy_bytes = min(4 - len(self.buffer), new_size - i)
self.buffer.append(new_bytes[i:(i + copy_bytes)])
if len(self.data) == self.channel_size:
self.set_state(Mode.HEADER)
return True
if len(self.buffer) == 4:
self.time = int.from_bytes(self.buffer, byteorder='big', signed=True)
self.set_state(Mode.DATA)
elif len(self.buffer) > 4:
self.set_state(Mode.HEADER) # Something went wrong
return False
i += copy_bytes
continue
return False
if self.mode == Mode.DATA:
# Copy until buffer contains 4 bytes or until new segment is depleted
copy_bytes = min(self.channel_size * 4 - len(self.buffer), new_size - i)
self.buffer.append(new_bytes[i:(i + copy_bytes)])
if len(self.buffer) == self.channel_size * 4:
values = struct.unpack(self.channel_size * 'f', self.buffer)
self.data.append(list(values))
self.set_state(Mode.HEADER)
new_packet = True
elif len(self.buffer) > self.channel_size * 4:
self.set_state(Mode.HEADER) # Something went wrong
i += copy_bytes
continue
return new_packet
......@@ -208,10 +208,9 @@ class MainWindow(QMainWindow):
new_bytes = self.serial.readAll()
for byte in new_bytes:
if self.decoder.receive_byte(byte):
self.update_data(self.decoder.channel_size, self.decoder.time,
self.decoder.data)
if self.decoder.receive_bytes(bytearray(new_bytes)):
self.update_data(self.decoder.channel_size, self.decoder.time,
self.decoder.data)
@pyqtSlot(QAction)
def on_save(self, action: QAction):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment