mavcrc.py 858 B

123456789101112131415161718192021222324252627282930
  1. '''MAVLink X25 CRC code'''
  2. from builtins import object
  3. class x25crc(object):
  4. '''x25 CRC - based on checksum.h from mavlink library'''
  5. def __init__(self, buf=None):
  6. self.crc = 0xffff
  7. if buf is not None:
  8. if isinstance(buf, str):
  9. self.accumulate_str(buf)
  10. else:
  11. self.accumulate(buf)
  12. def accumulate(self, buf):
  13. '''add in some more bytes'''
  14. accum = self.crc
  15. for b in buf:
  16. tmp = b ^ (accum & 0xff)
  17. tmp = (tmp ^ (tmp<<4)) & 0xFF
  18. accum = (accum>>8) ^ (tmp<<8) ^ (tmp<<3) ^ (tmp>>4)
  19. self.crc = accum
  20. def accumulate_str(self, buf):
  21. '''add in some more bytes'''
  22. accum = self.crc
  23. import array
  24. bytes = array.array('B')
  25. bytes.fromstring(buf)
  26. self.accumulate(bytes)