Proper method of receiving UART packet data far exceeding RX buffer size?

@Blackfin,

This looks really promising. This is a much more elegant way of going about this. The data format is:

Header - 4 bytes of char, constant
Data1 - unsigned short(2 byte) x 500
Data2 - unsigned short(2 byte) x 176
Data3 - unsigned short(2 byte) x 400
Data4 - unsigned short(2 byte) x 276
Data5 - unsigned short(2 byte) x 208
Data6 - float(4 byte) x 1
Data7 - float(4 byte) x 1
Data8 - float(4 byte) x 1
Data9 - float(4 byte) x 1
Data10 - float(4 byte) x 1
Data11 - float(4 byte) x 1
Data12 - float(4 byte) x 1
Tail - 4 bytes of char, constant

So, I have a total of 3156 bytes and 3148 bytes for the payload. Making the appropriate change to MSG_PCKT_SIZE = 3148 and running the code I get the following output:

20:15:32.508 -> Arduino ready to begin
20:15:32.714 -> DBG: Got header.
20:15:32.986 -> DBG: Got payload.
20:15:33.020 -> DBG: Got bad trailer.
20:15:33.055 -> DBG: Got header.
20:15:33.290 -> DBG: Got payload.
20:15:33.290 -> DBG: Got bad trailer.

I didn't think it would make any difference what the format of the payload bytes were, if we are just packing them away as bytes, however it appears that we aren't aligned to gather a valid tail.

Would it add too much delay to Serial.print(data,HEX) in each state in order to debug what I'm seeing in incoming data?

My previous code which runs with SERIAL_BUFFER_SIZE 6312 in RingBuffer.h (C:\Users<USERNAME>\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.12\cores\arduino) for Arduino Due is as follows:

#define PACKET_SIZE 3156
// ADC_PACKET_HEAD   0x5AA555AA
// ADC_PACKET_TAIL   0xEEFFEFFE 

int idx;
unsigned int adc_data[PACKET_SIZE];

void setup() {
  Serial.begin(9600);
  Serial1.begin(115200);
  Serial.println("Arduino Ready to recieve"); 
}

void loop() {
  while (1){
     if (Serial1.available() >= PACKET_SIZE*2 - 1){
      break;
     }
     else{
      continue;
     }
  }
  for (int i = 0; i < PACKET_SIZE*2 - 1; i++){
    adc_data[i] = Serial1.read();
  }

  for (int j = 0;j < PACKET_SIZE*2 - 1;j++){
    if (adc_data[j] == 0x5A && adc_data[j+1] == 0xA5 && adc_data[j+2] == 0x55 && adc_data[j+3] == 0xAA){ 
      idx = j;  // Find header index
      break;
    }
  } 
  Serial.println(adc_data[idx+PACKET_SIZE-1],HEX); // If this printout number is 0xFE, that means we received one data packet successfully.
}
}

Which generates this output:

US-D1 Readout
22:14:46.108 -> FE
22:14:46.688 -> FE
22:14:47.242 -> FE
22:14:47.830 -> FE
22:14:48.382 -> FE
22:14:48.966 -> FE
22:14:49.513 -> FE
22:14:50.100 -> FE
22:14:50.655 -> FE
22:14:51.241 -> FE

While this does work, albeit ugly, it will only work with the modified RX buffer size which appears to cause problems with the Wire library which I will need for the RTC communications. If I can get your code properly aligned to the packet size It looks like I'll be golden.