1

I am very new to Arduino.

Now I am trying to send the sensor data to Computer.

The sensor data will be stored as bytes into arrays. To build a robust transmission I'll pack the data with own defined protocol. e.g. has Header, length, Checksum ...

My question is, how to combine the sensor data with protocol frames and send it with serial in a better way.

Now I have written something like below, it works, but very simple and dirty :)

#define SOF_1 0x55
#define SOF_2 0xAA

uint8_t SensorDataByteArrA[4] = {0xc1, 0xc2, 0xc3, 0xC4};
uint8_t SensorDataByteArrB[4] = {0xe1, 0xe2, 0xe3, 0xe4};

void send_sensor_data()
{
  Serial.write(SOF_1);
  Serial.write(SOF_2);

  Serial.write(SensorDataByteArrA, 4);
  Serial.write(SensorDataByteArrB, 4);
  // ...

}

So I'd like to know how will you handle my case?

6
  • The primary question is text or binary. I'd only use binary if the speed benefit really matters. You seem to prefer binary, so just do it, and keep your computer code for the protocol in sync. ( Maybe even add a version info to the protocol ) – DataFiddler Sep 13 '20 at 20:55
  • 1
    What you have is a good start. You might like to look how I do it in ICSC: github.com/MajenkoLibraries/ICSC/blob/master/src/ICSC.cpp#L115 – Majenko Sep 13 '20 at 21:02
  • 1
    Of course you can add your own higher level functions or methods, like sendHeader ... Perhaps that helps developing the reading side in sync. Somewhere, more or less deep down, there will be a series of write calls. – DataFiddler Sep 13 '20 at 21:28
  • 1
    For more robust binary frame serial communication, you might want to take a look at PacketSerial which supports both SLIP and COBS to ensure reliable, unambiguous packet framing. – hcheung Sep 14 '20 at 5:54
  • 1
    @sun0727 there is nothing "dirty" about multiple serial wires. They just feed into the internal tx buffer. There is no benefit to implementing your own higher level tx buffer. All you do is waste memory. – Majenko Sep 14 '20 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.