def slip_reader(port):
 partial_packet = None
 in_escape = False
 while True:
  waiting = port.inWaiting()
  read_bytes = port.read((1 if (waiting == 0) else waiting))
  if (read_bytes == ''):
   raise FatalError(('Timed out waiting for packet %s' % ('header' if (partial_packet is None) else 'content')))
  for b in read_bytes:
   if (type(b) is int):
    b = bytes([b])
   if (partial_packet is None):
    if (b == '\xc0'):
     partial_packet = ''
    else:
     raise FatalError(('Invalid head of packet (%r)' % b))
   elif in_escape:
    in_escape = False
    if (b == '\xdc'):
     partial_packet += '\xc0'
    elif (b == '\xdd'):
     partial_packet += '\xdb'
    else:
     raise FatalError(('Invalid SLIP escape (%r%r)' % ('\xdb', b)))
   elif (b == '\xdb'):
    in_escape = True
   elif (b == '\xc0'):
    (yield partial_packet)
    partial_packet = None
   else:
    partial_packet += b

def slip_reader ( port ) : partial_packet = None in_escape = False while True : waiting = port . inWaiting ( ) read_bytes = port . read ( ( 1 if ( waiting == 0 ) else waiting ) ) if ( read_bytes == '' ) : raise FatalError ( ( 'Timed out waiting for packet %s' % ( 'header' if ( partial_packet is None ) else 'content' ) ) ) for b in read_bytes : if ( type ( b ) is int ) : b = bytes ( [ b ] ) if ( partial_packet is None ) : if ( b == '\xc0' ) : partial_packet = '' else : raise FatalError ( ( 'Invalid head of packet (%r)' % b ) ) elif in_escape : in_escape = False if ( b == '\xdc' ) : partial_packet += '\xc0' elif ( b == '\xdd' ) : partial_packet += '\xdb' else : raise FatalError ( ( 'Invalid SLIP escape (%r%r)' % ( '\xdb' , b ) ) ) elif ( b == '\xdb' ) : in_escape = True elif ( b == '\xc0' ) : ( yield partial_packet ) partial_packet = None else : partial_packet += b
generator to read slip packets from a serial port .