Indicating the end of a data stream

Hello,
I have this piece of wiring code that reads a data stream from lora wireless network and passes it through USB Interface:

void onLoRaReceive(int packetSize) {
  if (packetSize == 0) {
    indicateError();
    return;          // if there's no packet, return
  }

  byte[packetSize] buffer=[];
  
  while (LoRa.available()) {
    site_t recievedData=LoRa.readBytes(buffer,packetSize);
    Serial.write(buffer,recievedData);
  }
}

void indicateError(){
  digitalWrite(ERROR_LED_INDICATOR,HIGH);
  sleep(1000);
  digitalWrite(ERROR_LED_INDICATOR,LOW);
}

My question is on how to indicate the end of the stream/packet to the usb interface? My idea is to use either EOF or '\0' character but I fear is that some LoRa Packet may have Rogue EOF or '\0' characters that may indicate prematurely the End of the serial stream.

I have also seen the arduino uno - How to indicate an End of a stream on USB - Arduino Stack Exchange without any answer.

The second and third examples in Serial Input Basics may give you some ideas.

...R

Is your data text or binary? If the former, see Robin's reply.

If it's binary, you need to design a complete protocol that will contain a start marker, a length indication, the data and a crc so you can check that the packet is indeed a packet; a timeout might also be useful.

Regarding the start marker, it depends on the type of data that you want to transfer; if it's e.g. values obtained with analogRead(), 0xFFFF can be used. If it's status of switches, you can transfer 7 switches in a byte and the 8th bit can indicate start of packet; in which case the other seven bits contain either the status (start marker 0) or can contain the length of the message (start marker 1). Your creativity is the limit :wink:

What is at the receiving end of the USB?

to overcome the problem of frame flags in a data stream you can use bit or byte stuffing