Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @classmethod
- def Read(cls, usb, expected_cmds, timeout_ms=None, total_timeout_ms=None):
- """Receive a response from the device."""
- total_timeout_ms = usb.Timeout(total_timeout_ms)
- start = time.time()
- while True:
- msg = usb.BulkRead(24, timeout_ms)
- cmd, arg0, arg1, data_length, data_checksum = cls.Unpack(msg)
- command = cls.constants.get(cmd)
- logger.info('Now inside def Read (line 296)')
- logger.info('msg: %s', str(msg))
- logger.info('cmd: %s', str(cmd))
- logger.info('arg0: %s', str(arg0))
- logger.info('arg1: %s', str(arg1))
- logger.info('data_length: %s', str(data_length))
- logger.info('data_checksum: %s', str(data_checksum))
- logger.info('command: %s', str(command))
- logger.info('expected_cmds: %s', str(expected_cmds))
- if not command:
- logger.info('About to throw InvalidCommandError in def Read (line 306)')
- raise InvalidCommandError(
- 'Unknown command: %x' % cmd, cmd, (arg0, arg1))
- if command in expected_cmds:
- break
- if command == b'WRTE':
- break
- if time.time() - start > total_timeout_ms:
- raise InvalidCommandError(
- 'Never got one of the expected responses (%s)' % expected_cmds,
- cmd, (timeout_ms, total_timeout_ms))
- if command == b'WRTE':
- cls.Close()
- elif data_length > 0:
- data = bytearray()
- while data_length > 0:
- temp = usb.BulkRead(data_length, timeout_ms)
- if len(temp) != data_length:
- print(
- "Data_length {} does not match actual number of bytes read: {}".format(data_length, len(temp)))
- data += temp
- data_length -= len(temp)
- actual_checksum = cls.CalculateChecksum(data)
- if actual_checksum != data_checksum:
- raise InvalidChecksumError(
- 'Received checksum %s != %s', (actual_checksum, data_checksum))
- else:
- data = b''
- return command, arg0, arg1, bytes(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement