def _read_raw_packet(self): """ Reads a raw line of data from the Arduino. """ with self._serial_read_lock: data = (self._serial.readline() or '').strip() # self._serial.reset_input_buffer() if data: self.print('read raw data:', data) if data[0] == c.ID_HASH: # Check for new hash. try: self.expected_hash = int(data[1:].strip()) except (TypeError, ValueError) as e: traceback.print_exc(file=sys.stderr) elif self.expected_hash: # Validate packet. packet = Packet.from_string(data) if packet.hash != self.expected_hash: # if self.verbose: self.print('Invalid packet read due to hash mismatch:', packet) data = '' if data: # Record a successful read. self.last_read = time.time() return data
def _read_packet(self): """ Reads a packet from the Arduino. """ data = self._read_raw_packet() if data: packet = Packet.from_string(data) # Keep a receipt of when we receive acknowledgments so the write thread # knows when to stop waiting. if packet.data == c.OK: with self._acks_lock: self._acks[packet.id] = time.time() # Record pong so we know Arduino is still alive. # if packet.id == c.ID_PONG: # self.last_pong = time.time() self.read_count += 1 if packet.non_get_id in c.ALL_IDS: self.packet_counts[packet.non_get_id] += 2 return packet