def listen(self): """Read from socket, and decode the messages inside. """ # grab bits off the wire buff, addr = self.conn.recvfrom(2048) timestamp = time.time() if buff is not None: seqn = SEQN.decode(buff[:SEQN.size]) if seqn is None: return yield timestamp, ('SEQN', seqn) buff = buff[SEQN.size:] if self.fh is not None: self.fh.write(HEADER.encode(SEQN, int(timestamp))) self.fh.write(SEQN.encode(seqn)) self.fh.write(buff) self.fh.flush() # decode until we run out of bytes while buff != '': try: bytes_read, data = messages.decode(buff) buff = buff[bytes_read:] yield timestamp, data except messages.MessageSizeError: print("out of sync") return except: print("Reader Broke!") return
def test_decode(self): expect = { 'timestamp': 1, 'VCC': 5.0, 'Gyro_X': 1.0, 'Gyro_Y': 0, 'Gyro_Z': 0, 'Acc_X': 98.0, 'Acc_Y': 0, 'Acc_Z': 0, 'Magn_X': 0, 'Magn_Y': 0, 'Magn_Z': 0, 'Temp': 25, 'Aux_ADC': 0, } raw = b'ADIS\x00\x00\x00\x00\x00\x01\x00\x18\x08\x13\x00\x14\x00\x00\x00\x00\x0b\xbb\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' bytes_read, output = messages.decode(raw) fourcc, data = output self.assertEqual(bytes_read, messages.HEADER.size+ADIS.size) self.assertEqual(fourcc, 'ADIS') self.assertEqual(len(expect), len(data)) for item, value in data.items(): sigfig = expect[item] / 100.0 # 1% self.assertAlmostEqual(expect[item], value, delta=sigfig)
def read(self): """Read the file and return data inside it """ for _fourcc, raw in self.scan(): _bytes_read, data = messages.decode(raw) yield data