def read_block(self, interface, command, size, max_retries=MAX_RETRY_COUNT, check_multi_flag=True): ''' Read serial data given command, and data size. ''' with node_io_rlock_obj: # only allow one greenlet at a time self.inc_read_block_count(interface) success = False retry_count = 0 data = None while success is False and retry_count <= max_retries: if check_multi_flag: if self.multi_node_index >= 0: if not self.check_set_multi_node_index(interface): break try: self.io_request = monotonic() self.serial.flushInput() self.serial.write(bytearray([command])) data = bytearray(self.serial.read(size + 1)) self.io_response = monotonic() if validate_checksum(data): if len(data) == size + 1: success = True data = data[:-1] else: retry_count = retry_count + 1 if check_multi_flag: # log and count if regular query if retry_count <= max_retries: self.node_log(interface, 'Retry (bad length) in read_block: port={0} cmd={1} size={2} retry={3}'.format(self.serial.port, command, size, retry_count)) else: self.node_log(interface, 'Retry (bad length) limit reached in read_block: port={0} cmd={1} size={2} retry={3}'.format(self.serial.port, command, size, retry_count)) self.inc_read_error_count(interface) gevent.sleep(0.025) else: # self.log('Invalid Checksum ({0}): {1}'.format(retry_count, data)) retry_count = retry_count + 1 if check_multi_flag: # log and count if regular query if data and len(data) > 0: if retry_count <= max_retries: self.node_log(interface, 'Retry (checksum) in read_block: port={0} cmd={1} size={2} retry={3}'.format(self.serial.port, command, size, retry_count)) else: self.node_log(interface, 'Retry (checksum) limit reached in read_block: port={0} cmd={1} size={2} retry={3}'.format(self.serial.port, command, size, retry_count)) else: if retry_count <= max_retries: self.node_log(interface, 'Retry (no data) in read_block: port={0} cmd={1} size={2} retry={3}'.format(self.serial.port, command, size, retry_count)) else: self.node_log(interface, 'Retry (no data) limit reached in read_block: port={0} cmd={1} size={2} retry={3}'.format(self.serial.port, command, size, retry_count)) self.inc_read_error_count(interface) gevent.sleep(0.025) except IOError as err: self.node_log(interface, 'Read Error: ' + str(err)) retry_count = retry_count + 1 if check_multi_flag: # log and count if regular query if retry_count <= max_retries: self.node_log(interface, 'Retry (IOError) in read_block: port={0} cmd={1} size={2} retry={3}'.format(self.serial.port, command, size, retry_count)) else: self.node_log(interface, 'Retry (IOError) limit reached in read_block: port={0} cmd={1} size={2} retry={3}'.format(self.serial.port, command, size, retry_count)) self.inc_read_error_count(interface) gevent.sleep(0.025) return data if success else None
def read_block(self, interface, command, size): ''' Read serial data given command, and data size. ''' success = False retry_count = 0 data = None while success is False and retry_count < RETRY_COUNT: try: self.io_request = monotonic() self.serial.write(bytearray([command])) data = bytearray(self.serial.read(size + 1)) self.io_response = monotonic() if validate_checksum(data): success = True data = data[:-1] else: # self.log('Invalid Checksum ({0}): {1}'.format(retry_count, data)) gevent.sleep(0.1) retry_count = retry_count + 1 if retry_count < RETRY_COUNT: if retry_count > 1: # don't log the occasional single retry interface.log('Retry (checksum) in read_block: port={0} cmd={1} size={2} retry={3}'.format(self.serial.port, command, size, retry_count)) else: interface.log('Retry (checksum) limit reached in read_block: port={0} cmd={1} size={2} retry={3}'.format(self.serial.port, command, size, retry_count)) except IOError as err: interface.log('Read Error: ' + str(err)) gevent.sleep(0.1) retry_count = retry_count + 1 if retry_count < RETRY_COUNT: if retry_count > 1: # don't log the occasional single retry interface.log('Retry (IOError) in read_block: port={0} cmd={1} size={2} retry={3}'.format(self.serial.port, command, size, retry_count)) else: interface.log('Retry (IOError) limit reached in read_block: port={0} cmd={1} size={2} retry={3}'.format(self.serial.port, command, size, retry_count)) return data
def _read(): self.io_request = monotonic() _data = self.i2c_helper.i2c.read_i2c_block_data(self.i2c_addr, command, size + 1) self.io_response = monotonic() if validate_checksum(_data): return _data else: return None