From a13bb7f8651cb2da94295e0836b71089fd7ab42d Mon Sep 17 00:00:00 2001 From: Andrew Kay Date: Tue, 17 Dec 2019 22:54:45 -0600 Subject: [PATCH] Do not check parity when unpacking data words --- pycoax/coax/protocol.py | 14 +++++++------- pycoax/tests/test_protocol.py | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pycoax/coax/protocol.py b/pycoax/coax/protocol.py index 0463e45..5705830 100644 --- a/pycoax/coax/protocol.py +++ b/pycoax/coax/protocol.py @@ -237,19 +237,19 @@ def _pack_command_word(command, address=0): """Pack a command and address into a 10-bit command word for the interface.""" return (address << 7) | (command.value << 2) | 0x1 -def _unpack_data_words(words): - """Unpack the data bytes from 10-bit data words, performs parity checking.""" - return bytes([_unpack_data_word(word) for word in words]) +def _unpack_data_words(words, check_parity=False): + """Unpack the data bytes from 10-bit data words.""" + return bytes([_unpack_data_word(word, check_parity=check_parity) for word in words]) -def _unpack_data_word(word): - """Unpack the data byte from a 10-bit data word, performs parity checking.""" - if not (word & 0x1) == 0x0: +def _unpack_data_word(word, check_parity=False): + """Unpack the data byte from a 10-bit data word.""" + if (word & 0x1) != 0: raise ProtocolError('Word does not have data bit set') byte = (word >> 2) & 0xff parity = (word >> 1) & 0x1 - if not odd_parity(byte) == parity: + if check_parity and parity != odd_parity(byte): raise ProtocolError('Parity error') return byte diff --git a/pycoax/tests/test_protocol.py b/pycoax/tests/test_protocol.py index f8c6b9e..ecb0c36 100644 --- a/pycoax/tests/test_protocol.py +++ b/pycoax/tests/test_protocol.py @@ -156,7 +156,7 @@ class UnpackDataWordTestCase(unittest.TestCase): def test_parity_error(self): with self.assertRaisesRegex(ProtocolError, 'Parity error'): - _unpack_data_word(0b0000000000) + _unpack_data_word(0b0000000000, check_parity=True) if __name__ == '__main__': unittest.main()