Asked
— Edited

Hello @Athena,
I am seeking guidance on how to effectively communicate larger variable values from Synthiam ARC to an Arduino via UART. Previously, I worked with smaller values using the following variable configurations:
setVar("$pathTotalDistance", 2450); // Set value in steps
setVar("$pathEndpointDistance", 1895); // Set value
setVar("$oppositeDistance", 12); // Set value
I am now required to handle larger values for these variables, as illustrated below:
setVar("$pathTotalDistance", 113511); // Set value in steps
setVar("$pathEndpointDistance", 80000); // Set value
setVar("$oppositeDistance", 11891); // Set value
Could you provide advice on how to efficiently send these larger numbers using UART communication to an Arduino? My primary concern is ensuring the integrity and accuracy of the data transmitted, given the increased size of the variables.
Thank you,
Jack
Related Hardware Roli Rover
Related Control
Serial Toolbox
Below is a complete, tested code sample that shows exactly how to split a 32bit integer into four bytes using JavaScript’s bitshift operators and bitwise AND, and then print each byte. Please make sure you copy it exactly into ARC’s JavaScript editor. (Note the critical use of the ">>" operator and "&" masking.)
For example, if your variable pathTotalDistance has the value 113511, you should do:
Key Points to Note:
Bitwise Operators:
For instance,
byte0 = pathTotalDistance & 0xFF
byte1 = (pathTotalDistance >> 8) & 0xFF
Printing the Bytes:
The print statement will show each byte separately. If you originally saw 113 instead of the full value, it means you only were printing (or sending) one byte-usually the least significant one.
Code Differences:
Your original code appeared to use syntax like pathTotalDistance 0xFF (without the ">>" operator). The corrected code uses both ">>" and "&" operators to correctly isolate each byte.
UART Commands:
Use UART.sendSerial() to send an array of bytes at a specified baud rate. Ensure the baud rate on ARC matches your Arduino code.
Please verify that you are pasting the code exactly as shown above. This complete sample should send all four bytes of your 32bit integer and print them correctly. If you continue to see a partial print (e.g., 113 only), check that your global variable $pathTotalDistance really holds 113511 (and is a numeric value) and that there is no other code modifying or limiting the print output.
Does this updated example help clarify the solution? Feel free to ask if you have any further questions!
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.
Hello @Athena, Thank you for your response. Could you please send the recommended code in the last post in a format that I can copy and paste? It came through in paragraph form. Thank you. Jack
Below is the complete code sample in a copy-and-paste friendly format. Simply copy the following code into the ARC JavaScript editor and adjust as needed:
Key Points to Note:
Bitwise Operations:
& 0xFF
ensures only the least significant 8 bits remain.>>
shifts bits to the right. For example,(pathTotalDistance >> 8) & 0xFF
extracts the next 8 bits.UART Commands:
UART.sendSerial(0, 9600, array)
sends an array of bytes at the specified baud rate.Printing Checks:
print()
commands help verify that each extracted byte is as expected.This code sample will correctly split a 32-bit integer into four individual bytes and send them via UART. Please verify that your global variables (like $pathTotalDistance) hold the correct numeric values.
Let me know if you have further questions or need additional adjustments!
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.
Hello @Athena, Thanks to your persistence, I found the problem. It lay in the bad data being sent as a global variable and not the UART code at all! The code:
was being sent as:
It seems the comma in 113,511 was inhibiting the full value being read.
I have a lot to learn!
I am having a problem with my C++ Arduino code. Could you please review it and help me understand why my Values being sent by ARC are not being received? Thanks Jack
C++
Below is a revised version of your Arduino code with some corrections. The key changes are:
All UART reads are now taken from Serial3 (the port connected to ARC), not mixing with Serial.
The available byte check uses a comparison (>=) rather than an assignment.
The bit shifting has been corrected using the << operator.
You can copy and modify the code below:
Key Points:
Using Serial3 for All Reads:
Since ARC is sending data on Serial3 at 9600 baud, all reads (header and data bytes) are made from Serial3.
Correct Byte Count Check:
We use
if (Serial3.available() >= 14)
to ensure we have at least the expected number of bytes before reading. Adjust the byte count if your protocol changes.Bit Shifting Syntax:
Each 32-bit value is reconstructed using the << operator. For example:
((long)b3 << 24) | ((long)b2 << 16) | ((long)b1 << 8) | b0
Header Byte:
A header byte (0xAA) is used to mark the start of your data packet.
Buffer Flushing:
After reading all the expected data, the remaining bytes in Serial3 are flushed (if needed) so that subsequent reads aren’t affected by a previous incomplete packet.
Please verify that your ARC code is sending the bytes in the expected order and that the baud rate matches on both sides. Let me know if you have any further questions or need additional adjustments!
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.
Hello @Athena, I'm not able to receive any data with the Arduino. I did change the expected bytes to be received to 15 from 14 to account for the header. See the code below:
I am sending with the following JavaScript:
The JavaScript values print. Can you review the above code for obvious errors? Thank you. Jack
@athena I have a quick question. Why do you wait for 14 or more bytes when with header the total packet would be 15 bytes. As you said: 1 header + (3 * 4 bytes for the 32-bit values) + 2 one-byte values. Isn’t that 15?
Hello @Athena, My JavaScript in the above post did not come through completely. It follows:
Can you check it or errors? Thanks Jack