title: on-the-fly parsing of binary serial data in python
body: <p>I'm new to using Python3 for data acquisition. I'm trying to find a way to parse binary data from a serial port on Linux.</p>
  
  <pre><code>import serial
  
  ser = serial.Serial(   
      port='/dev/ttyS0',
      baudrate = 9600,
      parity=serial.PARITY_NONE,
      stopbits=serial.STOPBITS_ONE,
      bytesize=serial.EIGHTBITS,
      timeout=1)
  counter = 0
  
  while 1:
      x = ser.read(31)
      print (x)
  </code></pre>
  
  <p>This gives me a string which I'm not sure about the format of:</p>
  
  <pre><code>x='\x00\x00\x91\x00\x02\x88BM\x00\x1c\x00\x00\x00\x01\x00\x01\x00\x00\x00\x01\x00\x01\x00\xe1\x00K\x00\x1a\x00\x02\x00\x00'
  </code></pre>
  
  <p>using </p>
  
  <pre><code>x.encode('hex')
  </code></pre>
  
  <p>gives a string of hex values </p>
  
  <pre><code>x='000091000288**424d**001c00000001000100000001000100e1004b001a00020000'
  </code></pre>
  
  <p>where <code>0x42</code> is the end of message and <code>0x4d</code> is start of message.</p>
  
  <p>I can convert it into a base 10 list using </p>
  
  <pre><code>y = map(ord,x)
  print(y)
  </code></pre>
  
  <p>Then I have a way to re-order the message using the indexes but surely there is a neater way? How do I create a list which starts at <code>0x4d</code> to parse with?</p>