Arduino Mega 2560 中的总和计算错误 [英] Sum calculation error in Arduino Mega 2560

查看:22
本文介绍了Arduino Mega 2560 中的总和计算错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Arduino Mega 2560 与服务器通信.

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

我创建了一个字节数组,使用第一个数字作为指示符(告诉服务器这条消息来自 arduino 设备)和最后一个数字作为校验和.

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.

所以我得出结论,这是 Arduino 端的校验和错误.

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:"

一个字节存储一个8位无符号数,从0到255.那么如果计算的校验和大于255怎么办?结果会怎样?

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. 我建议将您的字节数组更改为无符号字节数组
  2. 然后将 sum 更改为 unsigned int 或 unsigned short(16 位就足够了)
  3. 计算总和的地方:sum = (sum + MSGpack[i]) &0xFF;
  4. MSGpack[186] = (无符号字节)sum;

我认为问题在于您将有符号数相加,并且没有将输出限制为 8 位.当您使用无符号字节时,最好明确告诉编译器,这样它就不会做出轻率的假设.

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.

这篇关于Arduino Mega 2560 中的总和计算错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆