Sum calculation error in Arduino Mega 2560

899 Views Asked by At

I am using Arduino Mega 2560 to communicate with the server.

I create a byte array, uses the first digit as a indicator (to tell the server this message is from a arduino device) and the last digit for check sum.

// for creating msg
void createmsg(){
int index = 0;
memset(MSGpack,0,sizeof(MSGpack));
byte sum;
MSGpack[0] = 0x23; // for identifing it is arduino


// for current readings
index = 14;
for (int i = 0; i < 7; i++){
  float voltage = readcurrent(i);
  injectByte(voltage, index);    
  index = index + 4;
}


////////////////////////////////////////////////////////////DATE
myRTC.updateTime();
index = 162;
int timeVAR = myRTC.dayofmonth;//reporting day
injectByte(timeVAR, index);

timeVAR = myRTC.month;
injectByte(timeVAR, 166); //reporting month

timeVAR = myRTC.year;
injectByte(timeVAR, 170); //reporting year

timeVAR = myRTC.year + myRTC.month + myRTC.dayofmonth; //sum of date
injectByte(timeVAR, 158);
////////////////////////////////////////////////////////////DATE

////////////////////////////////////////////////////////////TIME
myRTC.updateTime();
timeVAR = myRTC.hours;
injectByte(timeVAR, 146); //reporting hour

timeVAR = myRTC.minutes; 
injectByte(timeVAR, 150); //reporting second

timeVAR = myRTC.hours + myRTC.minutes;
injectByte(timeVAR, 154); //sum of time
////////////////////////////////////////////////////////////TIME

//to pass buffer verification
for (int i = 0; i < 186; i++) {
  sum += MSGpack[i];
}
MSGpack[186] = sum;

}

void injectByte(float value, int index){
  byte * b = (byte *) &value;
  MSGpack[index] = b[3];
  MSGpack[index + 1] = b[2];
  MSGpack[index + 2] = b[1];
  MSGpack[index + 3] = b[0];  
}

At the server side, it checks if the last digit equals to the sum of all the previous digit, if yes, it identify the received package is valid.

The problems is, if I comment out the date and time data, the server could identify the package as valid. But if I add the data back into the package, the server says the package is not valid.

So I conclude it is check sum error at the Arduino side.

According to here, "some constant calculations may overflow" && "Know at what point your variable will "roll over" " etc at "Programming tips:"

A byte stores an 8-bit unsigned number, from 0 to 255. So what if the check sum calculated is larger than 255? What will be resulted?

And how should I solve this issue and let the server receive a valid package? Thanks!

1

There are 1 best solutions below

4
On BEST ANSWER
  1. I would suggest changing your byte array to being an unsigned byte array
  2. then change sum to an unsigned int or unsigned short (16 bits will suffice)
  3. Where you calculate the sum: sum = (sum + MSGpack[i]) & 0xFF;
  4. MSGpack[186] = (unsigned byte)sum;

I think the issue is that you are adding signed numbers together and also not limiting the output to 8 bits. As you are using the bytes as unsigned, it's best to tell the compiler explicitly so it doesn't make rash assumptions.