Asked
Hello ,
I am experiencing unreliable transmission of UART data from my EZ-B v4 to an Arduino MEGA. It seems that the Arduino is not consistently reading the data as available. Sometimes the transmission works, and other times it does not. I have double-checked the wiring connections. Could you please review the JavaScript and C++ code for any obvious syntax errors? Thank you for your assistance.
Jack
JavaScript Code:
// Global variables
setVar("$pathTotalDistance", 2590); // SET VALUE
setVar("$pathEndpointDistance", 1895); // SET VALUE
setVar("$oppositeDistance", 247); // SET VALUE
setVar("$actualCourse", Servo.getPosition(17));
setVar("$offSetCourse", 0); // Determines DesiredCourse SET VALUE
if (getVar("$offSetCourse") == 0) {
setVar("$desiredCourse", 90);
} else {
setVar("$desiredCourse", 83);
}
setVar("$run", 1); // Set VALUE
// Local variables
var pathTotalDistance = getVar("$pathTotalDistance");
var pathEndpointDistance = getVar("$pathEndpointDistance");
var oppositeDistance = getVar("$oppositeDistance");
var actualCourse = getVar("$actualCourse");
var desiredCourse = getVar("$desiredCourse");
var run = getVar("$run");
sleep(500);
UART.initHardwareUart(1, 9600);
sleep(10);
// Send the variables to Arduino
var highByte = (pathTotalDistance >> 8) & 0xFF;
var lowByte = pathTotalDistance & 0xFF;
UART.hardwareUartWrite(1, [highByte, lowByte]);
print("Sending pathTotalDistance: " + pathTotalDistance);
highByte = (pathEndpointDistance >> 8) & 0xFF;
lowByte = pathEndpointDistance & 0xFF;
UART.hardwareUartWrite(1, [highByte, lowByte]);
print("Sending pathEndpointDistance: " + pathEndpointDistance);
highByte = (oppositeDistance >> 8) & 0xFF;
lowByte = oppositeDistance & 0xFF;
UART.hardwareUartWrite(1, [highByte, lowByte]);
print("Sending oppositeDistance: " + oppositeDistance);
UART.hardwareUartWrite(1, [actualCourse]); // Send as one byte
print("Sending actualCourse: " + actualCourse);
UART.hardwareUartWrite(1, [desiredCourse]); // Send as one byte
print("Sending desiredCourse: " + desiredCourse);
UART.hardwareUartWrite(1, [run]); // Send as one byte
print("Sending run: " + run);
sleep(100);
halt();
C++ Code:
delay(10);
if (Serial2.available() >= 9) {
delay(10);
initialParameters();
}
void initialParameters() { // Read and store bytes for pathTotalDistance
byte highBytePTD = Serial2.read();
byte lowBytePTD = Serial2.read();
pathTotalDistance = (highBytePTD << 8) | lowBytePTD;
// Read and store bytes for pathEndpointDistance
byte highBytePED = Serial2.read();
byte lowBytePED = Serial2.read();
pathEndpointDistance = (highBytePED << 8) | lowBytePED;
// Read and store bytes for oppositeDistance
byte highByteOD = Serial2.read();
byte lowByteOD = Serial2.read();
oppositeDistance = (highByteOD << 8) | lowByteOD;
actualCourse = Serial2.read(); // Read and store actualCourse
desiredCourse = Serial2.read(); // Read and store desiredCourse
run = Serial2.read(); // Read and store run
// Print the above received parameters
Serial.print("pathTotalDistance: ");
Serial.println(pathTotalDistance);
Serial.print("pathEndpointDistance: ");
Serial.println(pathEndpointDistance);
Serial.print("oppositeDistance: ");
Serial.println(oppositeDistance);
Serial.print("actualCourse: ");
Serial.println(actualCourse);
Serial.print("desiredCourse: ");
Serial.println(desiredCourse);
Serial.print("run: ");
Serial.println(run);
AB_EncoderTot = 0;
delay(100);
}
Please let me know if you spot any issues or have suggestions for improving the code.
Related Hardware Roli Rover
Let's review your JavaScript and C++ code for any potential issues or improvements.
JavaScript Code (ARC)
Bitwise Operations: Ensure that the bitwise operations are correctly formatted. You should use parentheses to clarify the order of operations.
UART Initialization: Make sure the UART is initialized correctly and that the correct UART index is used. You are using
UART.initHardwareUart(1, 9600);
, which should be correct if UART1 is intended.Data Transmission: Ensure that the data is being sent in the correct order and format. You are sending high and low bytes separately, which is correct for 16-bit values.
Printing: Ensure that the
print()
function is used correctly. It seems you are concatenating strings and variables, which is correct.Error Handling: Consider adding error handling or checks to ensure the UART is initialized and data is sent successfully.
C++ Code (Arduino)
Serial Availability Check: The condition
if(Serial2.available() == 9)
should use==
for comparison, not=
. This is a common mistake that can lead to unexpected behavior.Bitwise Operations: Ensure that the bitwise operations are correctly formatted. Use parentheses to clarify the order of operations.
Serial Reading: Ensure that the
Serial2.read()
function is used correctly and that the data is read in the correct order.Printing: Ensure that the
Serial.print()
andSerial.println()
functions are used correctly. It seems you are missing quotes around the strings.Delay: Consider whether the
delay(10);
is necessary and if it might be affecting the timing of your data reading.General Suggestions
By addressing these points, you should be able to improve the reliability of your UART communication between the EZ-B v4 and Arduino MEGA. If you continue to experience issues, consider testing with a simpler data set to isolate the problem. Let me know if you have any further questions or need additional assistance!
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.