i need a method for sending a struct from an esp to an arduino over serial protocol. what is the preferred way to do this please. i prefer not to use any other extra libraries. i'm out of search results i really need some help thanks
Use Serial.write - the variant where you pass an array and its size. Pass it a pointer to your struct variable.
Note that there may be an issue if the processor architectures vary and for example, have a different size for double.
wildbill:
Use Serial.write - the variant where you pass an array and its size. Pass it a pointer to your struct variable.Note that there may be an issue if the processor architectures vary and for example, have a different size for double.
i was wondering about that. will i face any issues doing this between an esp and an arduino mega2560? I also need an idea of how to read the byte array back into a structure? i understand your idea of sending it
Just Serial.read it a byte at a time. Create a pointer that takes the address of the target struct and write on it a byte at a time, incrementing the pointer as you do so.
wildbill:
Just Serial.read it a byte at a time. Create a pointer that takes the address of the target struct and write on it a byte at a time, incrementing the pointer as you do so.
that sounds simple enough. i will try it. from what i have read the esp8266 is bi-endian, and defaulted to "little-endian? thee only thing i know about endianess is that it has somthing to do with byte order. do you think this will be an issue for me? I'm using Arduino IDE to compile and load the sketches with board NodeMCUv1.0
I'd suggest that you define some simple test structs and try it out. You can always adjust the byte order once you have them if necessary.
If it gets too gnarly, you can always send the struct components as comma separated text and parse the result at the destination.
Okay forgive me i thought i understood the first post. here is some code ive come up with so far,
struct testStruct{
int var0 =123;
float var1 = 9.50;
unsigned long var2 = 123321;
};
testStruct ts;
void setup() {
Serial.begin(115200);
}
void loop() {
}
void testFunction(){
byte testArray[(sizeof(testStruct))];
for (byte i = 0; i < sizeof(testStruct); i++) {
testArray[i] = ?
}
}
i got stuck in the for loop. my logic is missing. Am i in the right direction. how do i load the bytes into the array?
struct testStruct{
int var0 =123;
float var1 = 9.50;
unsigned long var2 = 123321;
};
testStruct ts;
byte testArray[(sizeof(ts))];
void setup() {
Serial.begin(115200);
}
void loop() {
}
void testFunction(){
//byte testArray[(sizeof(testStruct))];
for (byte i = 0; i < sizeof(testStruct); i++) {
testArray[i] = ((byte*)&ts)[i];
}
}
Okay iv'e created a testWrite() function to Serial.write the testArray[]. I'm nnot sure weather to use the (const bytes) or (const char) cast. Should i be ready to move on to the receiving code after setting up loop to run functions?
struct testStruct{
int var0 =123;
float var1 = 9.50;
unsigned long var2 = 123321;
};
testStruct ts;
byte testArray[(sizeof(ts))];
void setup() {
Serial.begin(115200);
}
void loop() {
}
void testFunction(){
for (byte i = 0; i < sizeof(testStruct); i++) {
testArray[i] = ((byte*)&ts)[i];
}
}
void testWrite(){
Serial.write((const byte*)&testArray,sizeof(testArray));
}
When i run this code,
struct testStruct{
int var0 =123;
float var1 = 9.50;
unsigned long var2 = 123321;
};
testStruct ts;
byte testArray[(sizeof(ts))];
void setup() {
Serial.begin(115200);
}
void loop() {
testFunction();
}
void testFunction(){
for (byte i = 0; i < sizeof(testStruct); i++) {
testArray[i] = ((byte*)&ts)[i];
}
Serial.println("writing");
testWrite();
delay (2000);
}
void testWrite(){
Serial.write((const byte*)&testArray,sizeof(testArray));
}
the serial output prints "writing" and some symbols
notsolowki:
When i run this code,struct testStruct{
int var0 =123;
float var1 = 9.50;
unsigned long var2 = 123321;
};
testStruct ts;
byte testArray[(sizeof(ts))];
void setup() {
Serial.begin(115200);
}
void loop() {
testFunction();
}
void testFunction(){
for (byte i = 0; i < sizeof(testStruct); i++) {
testArray[i] = ((byte*)&ts)[i];
}
Serial.println("writing");
testWrite();
delay (2000);
}
void testWrite(){
Serial.write((const byte*)&testArray,sizeof(testArray));
}
the serial output prints "writing" and some symbols
What did you expect?
TheMemberFormerlyKnownAsAWOL:
What did you expect?
I'm not sure exactly.. i expect the bytes of the testArray. Am i ready to receive the bytes on the receiving end?
notsolowki:
i expect the bytes of the testArray.
Maybe those are the symbols. You're picking apart a binary number - who is to say those bytes are printable ASCII codes?
TheMemberFormerlyKnownAsAWOL:
Maybe those are the symbols. You're picking apart a binary number - who is to say those bytes are printable ASCII codes?
i sort of thought the serial console was expecting ASCII encoded characters and these bytes are in binary format. I think im going to need a little more guidance on the receiving end. Ultimately the receiver will be receiving 8 different structs from 8 different devices and all the structures contain different data all of the same endianess i guess. i dont want to start off in a bad direction. the receiver is running an ic2 lcd screen that updates frequently. im not looking for a smooth refresh rate but i need an efficient way to check if data is ready at the serial and know what struct to update without locking up the loop for too long.
I have created a receiver code im not exactly sure what i need to do to use the receivedArray[] to populate the struct. Am i in the right direction?
struct testStruct {
int var0 = 123;
float var1 = 9.50;
unsigned long var2 = 123321;
};
testStruct ts;
byte receivedArray[(sizeof(ts))];
void setup() {
Serial.begin(115200);
}
void loop() {
testReadData();
}
void testReadData() {
if (Serial.available() < sizeof(receivedArray)) {
Serial.println("wrong size");
return;
}
for (byte i = 0; i < sizeof(receivedArray); i++) {
receivedArray[i] = Serial.read();
}
}
EDIT: I modified the code in the last post
Well ideally, you lose the array and use a byte* pointer, point it to the ts structure, read the bytes one by one and write them where the pointer points and then increment it.
If you want to buffer in the array for some reason, memcpy the data there into the struct.
wildbill:
Well ideally, you lose the array and use a byte* pointer, point it to the ts structure, read the bytes one by one and write them where the pointer points and then increment it.If you want to buffer in the array for some reason, memcpy the data there into the struct.
Well i tried coming up with some receiver code and its not working at all. it just prints 255 in the serial console.
struct testStruct {
int var0 = 123;
float var1 = 9.50;
unsigned long var2 = 123321;
};
testStruct ts;
byte receivedArray[(sizeof(ts))];
void setup() {
Serial.begin(115200);
}
void loop() {
testReadData();
}
void testReadData() {
if (Serial.available() > 0 && Serial.available() < sizeof(999)) {
Serial.println("wrong size");
return;
}
for (byte i = 0; i < sizeof(receivedArray); i++) {
receivedArray[i] = Serial.read();
}
for (byte i = 0; i < sizeof(receivedArray); i++) {
Serial.println(receivedArray[i]);
if (i >= sizeof(receivedArray)) {
Serial.println("done");
}
}
}
i understand create a byte point and hand is the struct pointer as.
((byte*)&ts);
but you say drop the array. Forgive me knowledge but do i receive the data into the array first? or do you mean
((byte*)&ts) = Serial.read(i);
Serial.available() < sizeof(999)
What's that for?
TheMemberFormerlyKnownAsAWOL:
Serial.available() < sizeof(999)
What's that for?
i changed it to " if (Serial.available() > 0 && Serial.available() < sizeof(999)) {"
but it was not working the way i though. i thought it would help not lockup the code while serial data was greater than 0 or less than 999. 999 is an extreme number i should have chose the size of my largest transfer. i didnt think this would be so complicated for me to do
For the receiving code this is where im at. i need some strong guidance from here to get the data back into a struct.
struct testStruct {
int var0 = 123;
float var1 = 9.50;
unsigned long var2 = 123321;
};
testStruct ts;
byte receivedArray[(sizeof(ts))];
void setup() {
Serial.begin(115200);
}
void loop() {
testReadData();
}
void testReadData() {
while ((Serial.available() > 0) && (Serial.available() < 64)) {
}
}