0

The appended piece of code produces garbage values periodically. Can someone help me fix this?

void updateStatusSentence() { char statusArray[17] = ""; int currentStatus = 7; statusArray[0] = 'A'; statusArray[1] = (currentStatus/1)%10 + 48; int bladeSpeed = 1; statusArray[5] = ((bladeSpeed/10)%10) + 48; statusArray[6] = ((bladeSpeed/1)%10)+ 48; int temperature = 359; statusArray[2] = ((temperature/100)%10)+ 48; statusArray[3] = ((temperature/10)%10)+ 48; statusArray[4] = ((temperature/1)%10)+ 48; int weight = 0; statusArray[11] = ((weight/1000)%10)+ 48; statusArray[12] = ((weight/100)%10)+ 48; statusArray[13] = ((weight/10)%10)+ 48; statusArray[14] = ((weight/1)%10)+ 48; int currentTime = 1114; statusArray[7] = ((currentTime/1000)%10+ 48); statusArray[8] = ((currentTime/100)%10)+ 48; statusArray[9] = ((currentTime/10)%10)+ 48; statusArray[10] = ((currentTime/1)%10)+ 48; statusArray[15] = 'B'; statusArray[16] = '\0'; for (int i = 0; i < 17; i++) { Serial.print(statusArray[i]); } }

9th Sep 2021, 6:43 PM
Kun Ga
Kun Ga - avatar
1 Réponse
0
This code must be for an Arduino. Though I have no experience with Arduinos, I can suggest ideas to try. It appears that it should print A73590111140000B<null> to the serial port. If you want to send the bytes exactly as they are, then it is recommended to use Serial.write(). I think you could replace the loop with Serial.write(statusArray, 17);. From the code alone I see no cause for the corruption. It is likely an issue with the receiving terminal dropping bits, which may be a hardware issue such as an improperly-wired serial cable (CTS/RTS not connected, no RF shielding, floating ground, cable too long). Otherwise there could be a software issue such as mismatched serial port parameters (start/stop bits, parity, data size, handshaking) or memory corruption from another routine.
10th Sep 2021, 5:56 AM
Brian
Brian - avatar