Robin2:
This came up in another Thread recently and this is what I suggested.
...R
Noted this.
What if I want to allow varied payload size? I thinking if the sender sends something like: ><123>
Where '<' and '>' are start/end markers respectively. I wouldn't know if the data ended after the 'C' or '3' since the markers themselves can appear in the data and there is no fixed length to read.
As for the special marker method you posted, I'm concerned with having too much processing done on the sender and receiver side.
I'm thinking of using headers to inform the receiver how many bytes to receive but this will incur some overhead. I guess this is the trade off I have to compromise for.
PS ... its much easier to help if you stay as close as possible to the original version of a piece of code - so the changes you have made are obvious within a body of familiar code
That makes sense, I will keep this in mind when I post in future.
Power_Broker:
You could also use a proven and efficient serial transfer library to send standardized byte packets between Arduinos. The library uses start/end markers plust cyclic redundancy checking, consistent overhead byte stuffing, and dynamic payload length handling. I use it for several of my own projects and should work perfectly for this one, too.
You can install it using the Arduino IDE's Libraries Manager (search "serialTransfer")
Example code:
TX Arduino:
#include "SerialTransfer.h"
SerialTransfer myTransfer;
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
myTransfer.begin(Serial1);
}
void loop()
{
float myFloat = 100.5;
myTransfer.txBuff[0] = 'h';
myTransfer.txBuff[1] = 'i';
myTransfer.txBuff[2] = '\n';
myTransfer.txFloat(myFloat, 3); //insert the float "myFloat" at index 3 since "hi\n" already takes up indicies 0-2
myTransfer.sendData(7); //3 bytes for "hi\n" plus 4 bytes for the float "myFloat"
delay(100);
}
RX Arduino:
#include "SerialTransfer.h"
SerialTransfer myTransfer;
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
myTransfer.begin(Serial1);
}
void loop()
{
float myFloat;
if(myTransfer.available())
{
/////////////////////////////////////////////////////////////// Handle Entire Packet
Serial.println("New Data");
for(byte i = 0; i < myTransfer.bytesRead; i++)
Serial.write(myTransfer.rxBuff[i]);
Serial.println();
/////////////////////////////////////////////////////////////// Parse Out Float From Packet
myTransfer.rxFloat(myFloat, 3);
Serial.print("Received float: "); Serial.println(myFloat);
Serial.println();
}
else if(myTransfer.status < 0)
{
Serial.print("ERROR: ");
Serial.println(myTransfer.status);
}
}
Thanks for sharing that! Although the sender in my use case may not be using the Arduino framework, I will take a look at that library.