title: How to read binary data (struct) from serial using Python?
body: <p>I have a problem with reading serial data (which are packets in structs) using Python.</p>
  
  <p>In my Arduino, the firmware is sending me data (this is from the Arduino code):</p>
  
  
  
  <pre class="lang-c prettyprint-override"><code>***********************************************************
  For proper communication packet format given below have to be supported:
  ///////////////////////////////////////////////
  ////////// Packet Format Version 2 ////////////
  ///////////////////////////////////////////////
  // 17-byte packets are transmitted from Olimexino328 at 256Hz,
  // using 1 start bit, 8 data bits, 1 stop bit, no parity, 57600 bits per second.
  
  // Minimial transmission speed is 256Hz * sizeof(Olimexino328_packet) * 10 = 43520 bps.
  
  struct Olimexino328_packet
  {
    uint8_t   sync0;      // = 0xa5
    uint8_t   sync1;      // = 0x5a
    uint8_t   version;    // = 2 (packet version)
    uint8_t   count;      // packet counter. Increases by 1 each packet.
    uint16_t  data[6];    // 10-bit sample (= 0 - 1023) in big endian (Motorola) format.
    uint8_t   switches;   // State of PD5 to PD2, in bits 3 to 0.
  };
  */
  /**********************************************************/
  </code></pre>
  
  <p>Variables looks like this:</p>
  
  <pre class="lang-c prettyprint-override"><code>//Write packet header and footer
   TXBuf[0] = 0xa5;    //Sync 0
   TXBuf[1] = 0x5a;    //Sync 1
   TXBuf[2] = 2;       //Protocol version
   TXBuf[3] = 0;       //Packet counter
   TXBuf[4] = 0x02;    //CH1 High Byte
   TXBuf[5] = 0x00;    //CH1 Low Byte
   TXBuf[6] = 0x02;    //CH2 High Byte
   TXBuf[7] = 0x00;    //CH2 Low Byte
   TXBuf[8] = 0x02;    //CH3 High Byte
   TXBuf[9] = 0x00;    //CH3 Low Byte
   TXBuf[10] = 0x02;   //CH4 High Byte
   TXBuf[11] = 0x00;   //CH4 Low Byte
   TXBuf[12] = 0x02;   //CH5 High Byte
   TXBuf[13] = 0x00;   //CH5 Low Byte
   TXBuf[14] = 0x02;   //CH6 High Byte
   TXBuf[15] = 0x00;   //CH6 Low Byte 
   TXBuf[2 * NUMCHANNELS + HEADERLEN] =  0x01;    // Switches state
  </code></pre>
  
  <p>And putting data to these variables looks like this:</p>
  
  <pre class="lang-c prettyprint-override"><code>//Read the 6 ADC inputs and store current values in Packet
  for(CurrentCh=0;CurrentCh&lt;6;CurrentCh++){
    ADC_Value = analogRead(CurrentCh);
    TXBuf[((2*CurrentCh) + HEADERLEN)] = ((unsigned char)((ADC_Value &amp; 0xFF00) &gt;&gt; 8));
    // Write High Byte
    TXBuf[((2*CurrentCh) + HEADERLEN + 1)] = ((unsigned char)(ADC_Value &amp; 0x00FF));
    // Write Low Byte
  }
  // Send Packet
  for(TXIndex=0;TXIndex&lt;17;TXIndex++){
    Serial.write(TXBuf[TXIndex]);
  }
  // Increment the packet counter
  TXBuf[3]++;     
  </code></pre>
  
  <p>I'm new to Python and I don't know how to read it. For example, how to put these variables into a list. I wrote something like this:</p>
  
  <pre class="lang-py prettyprint-override"><code>#!/usr/bin/env python
  import serial
  
  ser = serial.Serial('/dev/tty.usbmodem1411', 57600)
  read_byte = ser.read()
  
  while read_byte is not None:
      read_byte = ser.read()
      print int(read_byte.encode('hex'), 16)
  </code></pre>
  
  <p>And the result is like this:</p>
  
  <pre class="lang-c prettyprint-override"><code>228
  1
  165
  90
  2
  98
  2
  3
  1
  252
  1
  245
  1
  240
  1
  238
  1
  232
  1
  165
  90
  2
  99
  </code></pre>
  
  <p>I now that there is something like a struct in Python and method unpack but I don't know how to do this. Please help me.</p>