#include <PostNeoSWSerial.h>
PostNeoSWSerial zt2Serial( 10, 11 );
    volatile uint32_t newlines = 0UL;
    
    static void handleRxChar( uint8_t c )
    {
      if (c == '\n')
        newlines++;
    }

//const int chipSelect = 10;
int gotPacket = 0;
uint8_t packet[17];

void setup() {
    // Open serial communications and wait for port to open:
  Serial.begin(9600);
  zt2Serial.begin(9600);
  //Serial1.setTimeout(600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

}


void getZeitronixPacket(char* packetpointer){
gotPacket = 0;
char buffer[32] = {0xF}; // buffer 32 bytes to make sure we find the 3 byte start sequence.
  for (int i=0; i<32; i++){
    zt2Serial.readBytes(&buffer[i],1);
    if (i > 1 && buffer[i] == 2 && buffer[i-1] == 1 && buffer[i-2] == 0) {
          gotPacket = 1;
          zt2Serial.readBytes(packetpointer,11);
          return;
    }
  }
}


void loop() {
char dataString[80];

while (zt2Serial.available() > 32){ // wait til there's a nice chunk in the serial buffer
    getZeitronixPacket(packet);
    // this would be our logtosd() below:
    if (gotPacket == 1) {
      sprintf(dataString, "%lu,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u", millis(),packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7],packet[8],packet[9],packet[10],packet[11],packet[12],packet[13],packet[14]);
      Serial.println(dataString);
    }
}


}