Advertisement
caffeinatedmike

Untitled

Nov 14th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. @classmethod
  2. def Read(cls, usb, expected_cmds, timeout_ms=None, total_timeout_ms=None):
  3. """Receive a response from the device."""
  4. total_timeout_ms = usb.Timeout(total_timeout_ms)
  5. start = time.time()
  6. while True:
  7. msg = usb.BulkRead(24, timeout_ms)
  8. cmd, arg0, arg1, data_length, data_checksum = cls.Unpack(msg)
  9. command = cls.constants.get(cmd)
  10. logger.info('Now inside def Read (line 296)')
  11. logger.info('msg: %s', str(msg))
  12. logger.info('cmd: %s', str(cmd))
  13. logger.info('arg0: %s', str(arg0))
  14. logger.info('arg1: %s', str(arg1))
  15. logger.info('data_length: %s', str(data_length))
  16. logger.info('data_checksum: %s', str(data_checksum))
  17. logger.info('command: %s', str(command))
  18. logger.info('expected_cmds: %s', str(expected_cmds))
  19. if not command:
  20. logger.info('About to throw InvalidCommandError in def Read (line 306)')
  21. raise InvalidCommandError(
  22. 'Unknown command: %x' % cmd, cmd, (arg0, arg1))
  23. if command in expected_cmds:
  24. break
  25. if command == b'WRTE':
  26. break
  27.  
  28. if time.time() - start > total_timeout_ms:
  29. raise InvalidCommandError(
  30. 'Never got one of the expected responses (%s)' % expected_cmds,
  31. cmd, (timeout_ms, total_timeout_ms))
  32.  
  33. if command == b'WRTE':
  34. cls.Close()
  35. elif data_length > 0:
  36. data = bytearray()
  37. while data_length > 0:
  38. temp = usb.BulkRead(data_length, timeout_ms)
  39. if len(temp) != data_length:
  40. print(
  41. "Data_length {} does not match actual number of bytes read: {}".format(data_length, len(temp)))
  42. data += temp
  43.  
  44. data_length -= len(temp)
  45.  
  46. actual_checksum = cls.CalculateChecksum(data)
  47. if actual_checksum != data_checksum:
  48. raise InvalidChecksumError(
  49. 'Received checksum %s != %s', (actual_checksum, data_checksum))
  50. else:
  51. data = b''
  52. return command, arg0, arg1, bytes(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement