Skip to content

Commit

Permalink
Hart protocol - unpacker asynch adjustments (#6)
Browse files Browse the repository at this point in the history
* universal/common/parsing

updates 8/1

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* parsing/readme fixes

fixed mypy errors in parsing. switched  readme unpacker object. added spacing to common

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* read one byte adjustment

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
will-driessen and pre-commit-ci[bot] authored Aug 29, 2022
1 parent daba759 commit d584e82
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions hart_protocol/_unpacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,24 @@ def _decoding_error(self, message="Error decoding message from buffer."):
# Discard first byte of buffer, it might decode better now...
self.buf = self.buf[1:]

def _read_one_byte_if_possible(self):
if self._file.in_waiting > 0:
return self._file.read(1)
else:
raise StopIteration

def __next__(self):
# must work with at least two bytes to start with
while len(self.buf) < 2:
self.buf += self._file.read(1)
self.buf += self._read_one_byte_if_possible()
# keep reading until we find a minimum preamble
while self.buf[:2] != b"\xFF\xFF":
self.buf += self._file.read(1)
self.buf += self._read_one_byte_if_possible()
self._decoding_error("Head of buffer not recognized as valid preamble")
# now we are within the preamble, seek forward for start charachter
while True:
if len(self.buf) < 2:
self.buf += self._file.read(1)
self.buf += self._read_one_byte_if_possible()
if self.buf[0] == 0xFF:
self.buf = self.buf[1:]
elif self.buf[0] in [0x06, 0x86]:
Expand All @@ -78,12 +84,12 @@ def __next__(self):
else:
l = 6
while len(self.buf) < l:
self.buf += self._file.read(1)
self.buf += self._read_one_byte_if_possible()
# now we can use the bytecount to read through the data and checksum
bytecount = self.buf[l - 3]
response_length = l + bytecount - 1
while len(self.buf) < response_length:
self.buf += self._file.read(1)
self.buf += self._read_one_byte_if_possible()
# checksum
checksum = int.from_bytes(tools.calculate_checksum(self.buf[: response_length - 1]), "big")
if checksum != self.buf[response_length - 1]:
Expand Down

0 comments on commit d584e82

Please sign in to comment.