我们从Python开源项目中,提取了以下33个代码示例,用于说明如何使用serial.iterbytes()。
def sixteen(data): """\ yield tuples of hex and ASCII display in multiples of 16. Includes a space after 8 bytes and (None, None) after 16 bytes and at the end. """ n = 0 for b in serial.iterbytes(data): yield ('{:02X} '.format(ord(b)), b.decode('ascii') if b' ' <= b < b'\x7f' else '.') n += 1 if n == 8: yield (' ', '') elif n >= 16: yield (None, None) n = 0 if n > 0: while n < 16: n += 1 if n == 8: yield (' ', '') yield (' ', ' ') yield (None, None)
def data_received(self, data): """Find data enclosed in START/STOP, call handle_packet""" for byte in serial.iterbytes(data): if byte == self.START: self.in_packet = True elif byte == self.STOP: self.in_packet = False self.handle_packet(bytes(self.packet)) # make read-only copy del self.packet[:] elif self.in_packet: self.packet.extend(byte) else: self.handle_out_of_packet_data(byte)
def hex_decode(data, errors='strict'): """b'@ab' -> '40 41 42'""" return (unicode(''.join('{:02X} '.format(ord(b)) for b in serial.iterbytes(data))), len(data))
def decode(self, data, errors='strict'): """b'@ab' -> '40 41 42'""" return unicode(''.join('{:02X} '.format(ord(b)) for b in serial.iterbytes(data)))
def decode(self, data, final=False): return unicode(''.join('{:02X} '.format(ord(b)) for b in serial.iterbytes(data)))
def data_received(self, data): """Find data enclosed in START/STOP, call handle_packet""" for byte in serial.iterbytes(data): if byte == self.START: self.in_packet = True elif byte == self.STOP: self.in_packet = False self.handle_packet(self.packet) del self.packet[:] elif self.in_packet: self.packet.append(byte) else: self.handle_out_of_packet_data(byte)
def test_iterbytes(self): self.assertEqual(list(serial.iterbytes(b'\x01\x02\x03')), [b'\x01', b'\x02', b'\x03'])