|
请问群里大佬,一个buffer需要参与CRC16校验,然后再输出新的buffer,
例如,new Bufrer = [0xFE,0x05,0x00,0x00,0xFF,0x00],
CRC16校验函数如下:
var CRCMaster = {
StringToCheck: "",
CleanedString: "",
CRCTableDNP: [],
init: function() {
this.CRCDNPInit();
},
CleanString: function(inputType) {
if (inputType == "ASCII") {
this.CleanedString = this.StringToCheck;
} else {
if (this.StringToCheck.match(/^[0-9A-F \t]+$/gi) !== null) {
this.CleanedString = this._hexStringToString(this.StringToCheck.toUpperCase().replace(/[\t ]/g, ''));
} else {
window.alert("String doesn't seem to be a valid Hex input.");
return false;
}
}
return true;
},
CRCDNPInit: function() {
var i, j, crc, c;
for (i = 0; i < 256; i++) {
crc = 0;
c = i;
for (j = 0; j < 8; j++) {
if ((crc ^ c) & 0x0001) crc = (crc >> 1) ^ 0xA6BC;
else crc = crc >> 1;
c = c >> 1;
}
this.CRCTableDNP[i] = crc;
}
},
CRC16Modbus: function() {
var crc = 0xFFFF;
var str = this.CleanedString;
for (var pos = 0; pos < str.length; pos++) {
crc ^= str.charCodeAt(pos);
for (var i = 8; i !== 0; i--) {
if ((crc & 0x0001) !== 0) {
crc >>= 1;
crc ^= 0xA001;
} else
crc >>= 1;
}
}
return crc;
},
_stringToBytes: function(str) {
var ch, st, re = [];
for (var i = 0; i < str.length; i++) {
ch = str.charCodeAt(i); // get char
st = []; // set up "stack"
do {
st.push(ch & 0xFF); // push byte to stack
ch = ch >> 8; // shift value down by 1 byte
}
while (ch);
// add stack contents to result
// done because chars have "wrong" endianness
re = re.concat(st.reverse());
}
// return an array of bytes
return re;
},
_hexStringToString: function(inputstr) {
var hex = inputstr.toString(); //force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
},
Calculate: function(str, inputType) {
this.StringToCheck = str;
if (this.CleanString(inputType)) {
crcinputcrc16modbus=this.CRC16Modbus().toString(16).toUpperCase();
crcinputcrc16modbus=crcinputcrc16modbus.substr(2) + crcinputcrc16modbus.substr(0, 2); //swap bytes
}
}
};
CRCMaster.init();
var inputType = "HEX";
var crcinputcrc16modbus;
var crcinput = msg.payload;
CRCMaster.Calculate(crcinput, inputType);
msg.payload = crcinput + crcinputcrc16modbus;
return msg;
请问怎样将结果最后输出为:new Buffer = [0xFE,0x05,0x00,0x00,0xFF,0x00,0x98,0x35]?
|
|