Skip to content

Instantly share code, notes, and snippets.

@TooTallNate
Created April 28, 2011 16:53
Show Gist options
  • Save TooTallNate/946745 to your computer and use it in GitHub Desktop.
Save TooTallNate/946745 to your computer and use it in GitHub Desktop.
Modbus Serial RTU CRC Algorithm
var data;
process.stdin.on('data', function(chunk) {
data = chunk
});
process.stdin.on('end', function() {
console.log(data);
var givenCrc = data.slice(data.length-2);
givenCrc = (data[1] << 7 | data[0]);
console.log(givenCrc);
var calculatedCrc = CRC(data.slice(0, data.length -2));
console.log(calculatedCrc);
});
process.stdin.resume();
var POLY = 0xA001;
var SEED = 0xFFFF;
function CRC(buffer) {
var crc = SEED;
for (var i=0; i<buffer.length; i++) {
crc = Calc_CRC(buffer[i], crc);
}
return crc;
}
function Calc_CRC(b, crc) {
crc ^= b & 0xFF;
for (var i=0; i<8; i++) {
var carry = crc & 0x0001;
crc>>=1;
if (carry) crc ^= POLY;
}
return crc;
}
@exeleon
Copy link

exeleon commented Sep 12, 2016

Thaks man! This helped me a lot...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment