Permalink
Browse files

Merge pull request #11 from thebadger412/master

Added working modbus support for RTU modbus
  • Loading branch information...
2 parents 3f931f9 + d6a3f3d commit affe249cacec42036386e9a370bc151e9f468037 @alexgorbatchev committed Jan 25, 2013
Showing with 496 additions and 43 deletions.
  1. +11 −9 README.md
  2. +37 −34 lib/crc.js
  3. +433 −0 lib/crcTemp.js
  4. +15 −0 test/crc.js
View
@@ -8,15 +8,17 @@ Original code is taken from http://www.digsys.se/JavaScript/CRC.aspx
The following functions are implemented:
- crc8(String) #=> Number
- crcArc(String) #=> Number
- crc16(String) #=> Number
- crc16CCITT(String) #=> Number
- fcs16(String) #=> Number
- crc32(String) #=> Number
- hex8(Number) #=> String
- hex16(Number) #=> String
- hex32(Number) #=> String
+ crc8(String) #=> Number
+ crcArc(String) #=> Number
+ crcModbusString(string) #=> Number
+ crcModbusHex(Number) #=> Number
+ crc16(String) #=> Number
+ crc16CCITT(String) #=> Number
+ fcs16(String) #=> Number
+ crc32(String) #=> Number
+ hex8(Number) #=> String
+ hex16(Number) #=> String
+ hex32(Number) #=> String
## Installation
View
@@ -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
}
};
})();
Oops, something went wrong.

0 comments on commit affe249

Please sign in to comment.