Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update lib/crc.js

added modbus support
  • Loading branch information...
commit 71a5a086d1c8ee9822a936b8c61e9ae6c514cde7 1 parent 3f931f9
@thebadger412 thebadger412 authored
Showing with 37 additions and 34 deletions.
  1. +37 −34 lib/crc.js
View
71 lib/crc.js
@@ -43,8 +43,8 @@
// is taken from an article by David Schwaderer in the April 1985
// issue of PC Tech Journal.
var CRC_ARC_TAB = new Array(
+ // THIS TABLE IS THE RIGHT ONE FOR MODBUS CALCULATIONS
// C/C++ language:
- //
// unsigned short CRC_ARC_TAB[] = {...};
0x0000,0xC0C1,0xC181,0x0140,0xC301,0x03C0,0x0280,0xC241,0xC601,0x06C0,0x0780,0xC741,0x0500,
0xC5C1,0xC481,0x0440,0xCC01,0x0CC0,0x0D80,0xCD41,0x0F00,0xCFC1,0xCE81,0x0E40,0x0A00,0xCAC1,
@@ -278,54 +278,57 @@
return crc;
};
- function crc16(str)
+ function crcModbusString(str)
{
+ //SAME AS ABOVE EXCEPT THIS HAS THE MODBUS CRC AT 0XFFFF
var i,
len = str.length,
- crc = 0
+ crc = 65535
;
-
- for(i = 0; i < len; i++)
- crc = crc16Add(crc, str.charCodeAt(i));
+ for(i = 0; i < len; i++)
+ crc = crcArcAdd(crc, str.charCodeAt(i));
+
return crc;
};
- function crc16Buffer(buf)
+ function crcModbusHex(buf)
{
- var crc = 0;
-
+ //SAME AS ABOVE EXCEPT THIS HAS THE MODBUS CRC AT 0XFFFF
+ var crc = 65535
+
for (var i = 0, len = buf.length; i < len; ++i)
{
- crc = crc16Add(crc, buf[i]);
+ crc = crcArcAdd(crc, buf[i]);
}
return crc;
- }
+ };
- function crc16CCITT(str)
+ function crc16(str)
{
var i,
len = str.length,
- crc = 0xFFFF
+ crc = 0
;
for(i = 0; i < len; i++)
crc = crc16Add(crc, str.charCodeAt(i));
- return crc^0x0000;
+ return crc;
};
- function crc16CCITTBuffer(buf)
+
+ function crc16Buffer(buf)
{
- var crc = 0xFFFF;
+ var crc = 0;
for (var i = 0, len = buf.length; i < len; ++i)
{
crc = crc16Add(crc, buf[i]);
}
- return crc^0x0000;
+ return crc;
}
function fcs16(str)
@@ -351,7 +354,7 @@
for(i = 0; i < len; i++)
crc = crc32Add(crc, str.charCodeAt(i));
- return (crc^0xFFFFFFFF) >>> 0;
+ return crc^0xFFFFFFFF;
};
function crc32Buffer(buf)
@@ -363,7 +366,7 @@
crc = crc32Add(crc, buf[i]);
}
- return (crc ^ 0xFFFFFFFF) >>> 0;
+ return crc ^ 0xFFFFFFFF;
}
/**
@@ -399,7 +402,7 @@
var target, property;
- if(typeof(module) !== 'undefined' && module.exports)
+ if(typeof(window) == 'undefined')
{
target = module;
property = 'exports';
@@ -411,20 +414,20 @@
}
target[property] = {
- 'crc8' : crc8,
- 'crcArc' : crcArc,
- 'crc16' : crc16,
- 'crc16CCITT' : crc16CCITT,
- 'fcs16' : fcs16,
- 'crc32' : crc32,
- 'hex8' : hex8,
- 'hex16' : hex16,
- 'hex32' : hex32,
- 'buffer' : {
- crc8 : crc8Buffer,
- crc16 : crc16Buffer,
- crc16CCITT : crc16CCITTBuffer,
- crc32 : crc32Buffer
+ 'crc8' : crc8,
+ 'crcArc' : crcArc,
+ 'crcModbusString' : crcModbusString,
+ 'crcModbusHex' : crcModbusHex,
+ 'crc16' : crc16,
+ 'fcs16' : fcs16,
+ 'crc32' : crc32,
+ 'hex8' : hex8,
+ 'hex16' : hex16,
+ 'hex32' : hex32,
+ 'buffer' : {
+ crc8 : crc8Buffer,
+ crc16 : crc16Buffer,
+ crc32 : crc32Buffer
}
};
})();
Please sign in to comment.
Something went wrong with that request. Please try again.