How do I read binary data (struct) from serial version using Python?

I have a problem reading serial data (which is packages in structures) using Python.

In my Arduino, the firmware is sending me data (this is from the Arduino 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.
};
*/
/**********************************************************/

      

The variables look like this:

//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

      

And putting data into these variables looks like this:

//Read the 6 ADC inputs and store current values in Packet
for(CurrentCh=0;CurrentCh<6;CurrentCh++){
  ADC_Value = analogRead(CurrentCh);
  TXBuf[((2*CurrentCh) + HEADERLEN)] = ((unsigned char)((ADC_Value & 0xFF00) >> 8));
  // Write High Byte
  TXBuf[((2*CurrentCh) + HEADERLEN + 1)] = ((unsigned char)(ADC_Value & 0x00FF));
  // Write Low Byte
}
// Send Packet
for(TXIndex=0;TXIndex<17;TXIndex++){
  Serial.write(TXBuf[TXIndex]);
}
// Increment the packet counter
TXBuf[3]++;     

      

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:

#!/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)

      

And the result will be like this:

228
1
165
90
2
98
2
3
1
252
1
245
1
240
1
238
1
232
1
165
90
2
99

      

Now I figured out that there is something like structure in Python and method unboxing, but I don't know how. Please help me.

+3


source to share





All Articles