Page 1 of 1

Serial Communication: Sending 3 Integers

Posted: Sun Mar 03, 2024 6:44 am
by dwalden
Hello- I'm trying to send 3 Integers from an Esp32 Controller to an Esp32 Receiver. I'm having a hard time understanding how to identify each Integer, so that the Receiver interprets them in the correct order. Do I need to send some kind of identifier with each Integer? I'm sending these 3 Integers every millisecond.

Controller code:
Serial.println(Int1);
Serial.println(Int2);
Serial.println(Int3);

The Receiver code uses Serial.parseInt() to identify a received Integer. Question: How should the Receiver check each Integer, to make sure it is the intended one, and doesn't confuse Int1 with Int2, etc? For example, the Receiver code might start executing at Int2, and therefore reading the first Integer with Serial.parseInt() would return Int2 and not Int1. Therefore it is important to identify the start and end of each message, and receive each Integer in order. Any help with this would be greatly appreciated.

Re: Serial Communication: Sending 3 Integers

Posted: Sun Mar 03, 2024 11:24 am
by DrMickeyLauer
That's what framing protocols are for. For payloads small as these, I'd use json on the serial console.

Re: Serial Communication: Sending 3 Integers

Posted: Sun Mar 03, 2024 5:04 pm
by lbernstone
yep, json, or send them all as a single record with a defined structure.

Code: Select all

Serial.printf("%d\t%d\t%d\n", Int1, Int2, Int3);

Re: Serial Communication: Sending 3 Integers

Posted: Sun Mar 03, 2024 10:54 pm
by dwalden
Thanks for the help. I've never used Json before, but I'm looking into it now. Question: is there a speed limit when sending Integers like this between boards using serial communication? For example, could I send/receive 3 Integers every 10 microseconds if I wanted to? At what point would the communication start to fail, or messages get dropped? Also, does the distance between the boards matter? Ideally I want to place the boards about 15 ft apart.

Re: Serial Communication: Sending 3 Integers

Posted: Mon Mar 04, 2024 9:00 am
by DrMickeyLauer
You can easily compute how long it takes to send your data, you have all the variables like UART bitrate and number of bits to send, etc.