diff options
author | Diego Elio Pettenò <flameeyes@flameeyes.eu> | 2013-08-03 23:18:11 +0200 |
---|---|---|
committer | Diego Elio Pettenò <flameeyes@flameeyes.eu> | 2013-08-03 23:18:11 +0200 |
commit | 91fb2d40f286c1f3b0af9146d66d424f4e8c5c6d (patch) | |
tree | edd37c7ac592944064f56330b2b8d6a30c368873 | |
parent | Add support for checksum verification for the otultra2 driver. (diff) | |
download | glucometerutils-91fb2d40f286c1f3b0af9146d66d424f4e8c5c6d.tar glucometerutils-91fb2d40f286c1f3b0af9146d66d424f4e8c5c6d.tar.gz glucometerutils-91fb2d40f286c1f3b0af9146d66d424f4e8c5c6d.tar.bz2 glucometerutils-91fb2d40f286c1f3b0af9146d66d424f4e8c5c6d.tar.lz glucometerutils-91fb2d40f286c1f3b0af9146d66d424f4e8c5c6d.tar.xz glucometerutils-91fb2d40f286c1f3b0af9146d66d424f4e8c5c6d.tar.zst glucometerutils-91fb2d40f286c1f3b0af9146d66d424f4e8c5c6d.zip |
-rw-r--r-- | glucometerutils/drivers/lifescan_common.py | 38 | ||||
-rw-r--r-- | test/test_lifescan.py | 7 | ||||
-rw-r--r-- | test/test_otultra2.py | 2 |
3 files changed, 13 insertions, 34 deletions
diff --git a/glucometerutils/drivers/lifescan_common.py b/glucometerutils/drivers/lifescan_common.py index ac25d22..5b42c33 100644 --- a/glucometerutils/drivers/lifescan_common.py +++ b/glucometerutils/drivers/lifescan_common.py @@ -6,8 +6,6 @@ __email__ = 'flameeyes@flameeyes.eu' __copyright__ = 'Copyright © 2013, Diego Elio Pettenò' __license__ = 'GPL v3 or later' -import ctypes - from glucometerutils import exceptions @@ -31,40 +29,20 @@ class InvalidSerialNumber(exceptions.Error): def calculate_checksum(bytestring): - """Calculate the "CRC16 Sick" style checksum for LifeScan protocols. + """Calculate the checksum used by OneTouch Ultra and Ultra2 devices Args: bytestring: the string of which the checksum has to be calculated. Returns: - A 16-bit integer that is the checksum for the input. + A string with the hexdecimal representation of the checksum for the input. - Credits for this code go to Christian Navalici, who implemented it in his - library at https://github.com/cristianav/PyCRC/ . + The checksum is a very stupid one: it just sums all the bytes, + modulo 16-bit, without any parity. """ - crcValue = 0x0000 - prev_c = 0x0000 - - for idx, c in enumerate(bytestring): - short_c = 0x00ff & c - - idx_previous = idx - 1 - short_p = ( 0x00ff & prev_c) << 8; - - if ( crcValue & 0x8000 ): - crcValue = ctypes.c_ushort(crcValue << 1).value ^ 0x8005 - else: - crcValue = ctypes.c_ushort(crcValue << 1).value - - crcValue &= 0xffff - crcValue ^= ( short_c | short_p ) - - prev_c = short_c + checksum = 0 - # After processing, the one's complement of the CRC is calcluated and the - # two bytes of the CRC are swapped. - low_byte = (crcValue & 0xff00) >> 8 - high_byte = (crcValue & 0x00ff) << 8 - crcValue = low_byte | high_byte; + for byte in bytestring: + checksum = (checksum + byte) & 0xffff - return crcValue + return checksum diff --git a/test/test_lifescan.py b/test/test_lifescan.py index 774567d..a7446e0 100644 --- a/test/test_lifescan.py +++ b/test/test_lifescan.py @@ -20,10 +20,11 @@ from glucometerutils import exceptions class TestOTUltra2(unittest.TestCase): def testChecksum(self): checksum = lifescan_common.calculate_checksum(bytes('T', 'ascii')) - self.assertEqual(0x5400, checksum) + self.assertEqual(0x0054, checksum) - checksum = lifescan_common.calculate_checksum(bytes('TestString', 'ascii')) - self.assertEqual(0x0643, checksum) + checksum = lifescan_common.calculate_checksum( + bytes('T "SAT","08/03/13","22:12:00 "', 'ascii')) + self.assertEqual(0x0608, checksum) if __name__ == '__main__': unittest.main() diff --git a/test/test_otultra2.py b/test/test_otultra2.py index 6ffea83..98fffc8 100644 --- a/test/test_otultra2.py +++ b/test/test_otultra2.py @@ -48,7 +48,7 @@ class TestOTUltra2(unittest.TestCase): def testInvalidSerialNumber(self): self.mock_readline.return_value = bytes( - '@ "12345678O" E105\r', 'ascii') + '@ "12345678O" 0297\r', 'ascii') self.assertRaises(lifescan_common.InvalidSerialNumber, self.device.get_serial_number) |