分享一个 大牛的代码就是MODBUS 的CRC16的校验程序!
By
凯文
at 2021-07-18 • 0人收藏 • 1259人看过
import console;
//计算modbus的CRC16校验值
var modbus_calc_CRC=function(pucCommandData, ucCommandLen){
var usCRC = 0xFFFF;
var i,j;
for(i=1; ucCommandLen; 1){
usCRC ^= pucCommandData[i];
for(i=1; 8; 1){
if (usCRC & 1)
{
usCRC >>= 1;
usCRC ^= 0xA001;
}
else
{
usCRC >>= 1;
}
}
}
//低字节在前
pucCommandData[ucCommandLen + 1] = usCRC&0x0ff;
//高字节在后
pucCommandData[ucCommandLen + 2] = (usCRC >> 8) & 0x0ff;
return pucCommandData;
}
//举例
var tab = {0x12; 0x22};
//计算
var ret = modbus_calc_CRC(tab, #tab);
for(i=1; #ret; 1){
//输出结果
console.log( string.format("%02X", ret[i]) );
}
console.pause(true);
1 个回复 | 最后更新于 2021-07-18
登录后方可回帖
https://www.chengxu.xyz/t/299
你说的大牛是这个吗?