Asked
— Edited
Hi @Athena,
I'm using the following code snippet on an Arduino Mega that is communicating via UART with an EZB4:
if (Serial3.available() >= 1) {
int incomingByte = Serial3.peek(); // Do not remove it yet
// Check if this byte is our STOP command (assuming 'x' is the stop command)
if (incomingByte == 'm') {
Serial3.read();
miss = true;
actualCourse = desiredCourse;
}
if (incomingByte == 'x') {
// Consume the byte and stop motors
Serial3.read();
Serial.print("Received STOP command: ");
Serial.println('x');
stopMotors();
goto Main; // Jump to the Main section
}
if (incomingByte == 'c') {
Serial3.read();
Serial.print("(Received obs scan complete) command ");
Serial.println('c');
}
}
if (Serial3.available() >= 1) {
// Read and check the header byte (expected to be 64)
int headerByte = Serial3.read();
if (headerByte == 64) {
int32_t b0 = Serial3.read();
int32_t scaledValue = (b0);
actualCourse = scaledValue;
Serial.print(" ..............---------------------------- rec'd actualCourse: ");
Serial.println(actualCourse, 4);
lastActualCourse = actualCourse;
initial = true;
miss = false;
loopct = 0;
}
}
The lower half of the code sometimes returns gibberish for the "actualCourse". I suspect the issue may be related to the Serial3.peek() and Serial3.available() statements before the actual Serial3.read(). Could you please review the structure of my code and suggest a solution for reliable data transfer?
Thank you,
Jack
Related Hardware (view all EZB hardware)
Arduino Due/Mega
by Arduino
Synthiam ARC enables Arduino Mega 2560 control with 8 ADC, 13 PWM, 21 digital ports and 24 servos; includes firmware, shield and tutorials
USB
Servos
24
Camera
Audio
UART
3
I2C
ADC
8
Digital
21
Related Robot Skills (view all robot skills)
EZB Connection
by Synthiam
Connects EZ-B I/O controllers to ARC via COM or IP (5 connections). Supports init scripts, battery monitoring, TCP/EZ-Script server and serial/I2C.
Serial Terminal
by Synthiam
PC and EZB serial terminals for ARC: connect, monitor and send text/ASCII to serial devices. Supports COM, flow control, baud, DTR/RTS, hex
Upgrade to ARC Pro
ARC Pro will give you immediate updates and new features needed to unleash your robot's potential!
📚 This thread has been preserved as a helpful resource!
While new comments are no longer being accepted, the information here remains available for learning and reference. Check the most recent comment for additional details.

Good catch - the symptoms and your code both point to framing and timing issues in how you read the serial stream. A few problems to fix:
Serial3.read()returns a single byte (or -1), but you assign it to an int32 and treat it like the full 32-bit value.available()in separate places and usingpeek()can leave you attempting to read a header/payload before the whole packet has arrived (race condition).peek()doesn't consume a byte; it’s useful but you still must ensure the full packet is present before consuming.Two reliable approaches:
Below are concrete examples for both approaches and extra tips.
Example:
Notes:
Parsing example:
Advantages: easy to inspect with Serial Monitor or the Serial Terminal skill, and less fragile.
Other suggestions and troubleshooting checklist
gotofor flow control if possible - use state machine or flags; it’s harder to reason about when dealing with async serial data.If you want a minimal state-machine parser that handles both single-byte commands and fixed binary packets, I can provide that.
Quick questions to tailor a final solution
If you want, I can:
References:
Tell me the sender packet format (binary vs ASCII, payload length and endianness) and I’ll produce the exact parser code you can drop in.
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Thank you @Athena.
JavaScript snippet that sends data via UART to the Arduino:
C++ snippet that receives data via UART from EZB4:
Of the two options you detailed in your last response, I think option (2) maybe more suitable for me.
(ASCII/line protocol (human-readable, easier debug) If you can change the sender, one of the easiest robust protocols is a newline-terminated ASCII string like: "ACT:1234\n" or just "1234\n")
Could you please provide me a snippet of JavaScript that would reliably send data, and C++ that would reliably receive data based upon option 2? Thank you Jack
Perfect - ASCII line protocol is a great choice for reliability and debugging. Below are drop-in examples you can use right away.
Summary:
JavaScript (ARC) - sender
Notes:
C++ (Arduino Mega) - receiver
Important integration notes and checks
If you’d like, I can:
Quick question: do you need decimals in actualCourse (float) or is integer OK?
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Thank you @Athena. A lot to digest here. It works very well. I'm working hard to understand the why now. More questions soon to follow. Jack
Hi @Athena, I believe a problem has occurred with the UART transfer of 'actualCourse' from EZB4 to the Arduino Mega. I have included the EZB4 JavaScript that sends the 'actualCourse' and also the Arduino C++ that receives it. The var 'actualControl' is a whole number 1-180.
The JavaScript snippet runs on a Loop with sleep(200ms):
The C++ snippet loop is below:
My problem with motor control is due to, what I believe is, "mixing" of old and new 'actualCourse' when it is received by the Arduino. The sending of a new 'actualCourse is reliable but the reception seems to be a problem. When printing, the old 'actualCourse' continues to print about 6 times before the new 'actualCourse' is processed. The result is incorrect motor control PWM.
I am wondering if the 'delayed' processing of 'actualCourse' in the "while loop" while 'actualCourse' buffer fills up is the best way to go. I believe it is interfering with the new 'actualCourse' being sent. Could you please relook at this code in light of my problem and advise a solution? As always, thank you. Jack
Just a thought but i looked at this thread and you have athena creating a text string protocol? That is going to be a really really really really really really really really really really really really
bad idea haha.
We talked about this before in a previous thread - but as a recap - computers talk to each other through bytes. Humans talk to each other with strings. Computers don't do strings very well. They can - but with a LOT of code. Looking above, your string parser is longer than the program!
All your program should do is send the number as an array of bytes. Then, the arduino receives that array of bytes. Simple. You throw a header on, and it makes it fool proof.
Something like..
And then assemble those bytes into your int32.
Now, if you're talking about sending a float (i.e. a number with a decimal place), then just multiply it before send (convert to an integer) and divide in receive on arduino (converts back to a float).
Lastly, you flagged this conversation as completed so Athena won't answer you - no matter how loud you yell haha - she's deaf for closed threads. Use the information i provided you above to ask Athena in a new thread. Only close a thread if the answer works.