Skip to content
Snippets Groups Projects
Commit 4d5894a9 authored by Daniele Nicolodi's avatar Daniele Nicolodi
Browse files

meerstetterTEC: Fix parsing of error replies

parent 15613e45
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,7 @@ from ptblab import datalogger, terminal, utils ...@@ -14,6 +14,7 @@ from ptblab import datalogger, terminal, utils
EOF = b'\r' EOF = b'\r'
DEVICE = b'!' DEVICE = b'!'
HOST = b'#' HOST = b'#'
ERROR = b'+'
class Errno(int, enum.Enum): class Errno(int, enum.Enum):
...@@ -102,19 +103,27 @@ class MeCom: ...@@ -102,19 +103,27 @@ class MeCom:
def recv(self, crc=None): def recv(self, crc=None):
packet = self.conn.read_until(EOF).rstrip() packet = self.conn.read_until(EOF).rstrip()
crc = crc if crc is not None else self.crc(packet[:-4]) control, address, seqno, data = self.unpack(packet[:-4])
# When the received frame is an ACK to a VS instruction, the
# CRC matches the CRC of the packet containing the VS
# instruction. When the received frame is an error or a reply
# to other instructions, the CRC should match the CRC of the
# reply packet. When the reply is expected in response to a VS
# instruction, the expected CRC is provided as an argument.
crc = crc if crc is not None and data[0:1] != ERROR else self.crc(packet[:-4])
if not packet: if not packet:
raise TimeoutError raise TimeoutError
if crc != packet[-4:]: if crc != packet[-4:]:
raise ValueError(f'CRC error: {packet}') raise ValueError(f'CRC error: {packet}')
control, address, seqno, data = self.unpack(packet[:-4])
if control != DEVICE: if control != DEVICE:
raise ValueError(f'unexpected control character: {packet}') raise ValueError(f'unexpected control character: {packet}')
if address != self.address: if address != self.address:
raise ValueError(f'unexpected return address: {packet}') raise ValueError(f'unexpected return address: {packet}')
if seqno != self.seqno: if seqno != self.seqno:
raise ValueError(f'unexpected return seqno: {packet}') raise ValueError(f'unexpected return seqno: {packet}')
if data[0:1] == b'+': if data[0:1] == ERROR:
raise MeComError(Errno(int(data[1:]))) raise MeComError(Errno(int(data[1:])))
return data return data
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment