Hi,
I'm developing ESP32 to ESP32 serial communication module for switching 32 relays(receiver end).
the DataPacket format is as follows
struct DataPacket
{
int arr[];
int arrSize;
}
I want to transmit this struct to receiver side over serial
I'm a beginner and can someone help me to solve this Data packet transmission over serial problem?
The struct you have defined has no specification for the number of array-elements.
There are a lot of options to do this.
If you really want to transmit it over a serial interface you would iterate through all array-elements creating a comma-separated string.
printing the string to serial receive the whole string on the other side and then using strtok to separate the numbers into the same struct on the receiverside.
As you are using an ESP32 how about using the wireless ESP-NOW-protocol?
ESP-NOW allows to send up to 250 bytes per message.
And you can directly send the struct as is. You just need to do a memcopy from the received data to a variable of type DataPacket
There is some effort to setup the ESP-NOW but there are a lot of examples online how to do that.
I would recommend to use this tutorial
as a starting point and then adapt it in small steps to your needs.
actually that's a common approach for defining a generic struct for a variable length packets (e.g. ethernet). however, the size must precede the data.
a packet of fixed size would be cast as that type of struct and passed to some output function. the output function would read the size and transmit the corresponding # of by byte (4 * (size+1)). you would use serial.write (buf, len)
there are benefits to sending structured data in binary in the form suggested. the data[] could be a structure that can be copied directly into the struct on the receiving side and avoid parsing a string
I agree, this is a good approach, especially serial.write (buf, len) if a person knows how structures are located in memory and how to get data later.
However, beginners can use any approach they understand.
consider the code (run on my laptop) below and producing the following output
the code defines two structures for data, A_s and B_s, to be transmitted using a common routine passed a argument of type Generic_s. the send() routine doesn't care about the format of the data, just the # of integers.
in both A_s and B_s, the data is not just composed of integers. and when using mixed types, the structure must be "packed", to avoid "unused" byte between fields. the field within a structure used for this purpose are often organized to line up on integer boundaries
gcjr
thank you for posting. Aha .... --- OK -- ... hm ....
The code makes intensive use of pointers.
It uses main. So it seems not an arduino-sketch but a classical C(c++-code.
That's all I understand.
I'm a somehow a little bit advanced programmer but this is above my head.
Again thank you for posting an example to my request.
maybe I should stop hijacking the thread and concentrate on suggestions for the thread-opener.
@Vijan Vijan,
what do you think about transferring the data wireless?
It is just another option.
Both options wired-serial and wireless ESP-NOW have their pro's and con's
best regards Stefan
#include <SoftwareSerial.h>
SoftwareSerial softOut(11, 12); //Rx, Tx
struct dataLayout //struct to hold the data
{
char x[20];
byte y;
} dataOut; //working copy of the struct
const byte structSize = sizeof(dataOut);
union unionLayout //union to hold data in parallel
{
dataLayout dataOut;
char buffer[structSize];
};
unionLayout dataToSend; //working copy of the union
void setup()
{
Serial.begin(115200);
while (!Serial);
softOut.begin(9600);
strcpy(dataToSend.dataOut.x, "Hello"); //put some values in the struct
dataToSend.dataOut.y = 65;
}
void loop()
{
for (int bufferIndex = 0; bufferIndex < structSize; bufferIndex++)
{
softOut.write(dataToSend.buffer[bufferIndex]);
Serial.print(dataToSend.buffer[bufferIndex]);
}
delay(1000);
}
Receiver
#include <SoftwareSerial.h>
SoftwareSerial softIn(11, 12); //Rx, Tx
struct dataLayout //struct to hold the data
{
char x[20];
byte y;
} dataIn; //working copy of the struct
const byte structSize = sizeof(dataIn);
union unionLayout //union to hold data in parallel
{
dataLayout dataIn;
char buffer[structSize];
};
unionLayout dataToReceive; //working copy of the union
void setup()
{
Serial.begin(115200);
while (!Serial);
softIn.begin(9600);
}
void loop()
{
if (softIn.available() == structSize)
{
for (int bufferIndex = 0; bufferIndex < structSize; bufferIndex++)
{
dataToReceive.buffer[bufferIndex] = softIn.read();
}
Serial.println(dataToReceive.dataIn.x);
Serial.println(dataToReceive.dataIn.y);
}
}
It may be full of holes and certainly needs careful management to ensure that both ends remain in step should the data layout change