Go Down

Topic: Indicating the end of a data stream (Read 702 times) previous topic - next topic


Apr 24, 2018, 11:02 pm Last Edit: Apr 24, 2018, 11:04 pm by pc_magas
I have this piece of wiring code that reads a data stream from lora wireless network and passes it through USB Interface:

Code: [Select]

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

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

void indicateError(){

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 https://arduino.stackexchange.com/questions/52151/how-to-indicate-an-end-of-a-stream-on-usb without any answer.


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

Two or three hours spent thinking and reading documentation solves most programming problems.


May 01, 2018, 06:24 pm Last Edit: May 01, 2018, 06:26 pm by sterretje
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 ;)

What is at the receiving end of the USB?
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.


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

Go Up