Line 345: |
Line 345: |
| The algorithm how to calculate CRC - 16(also known as CRC - 16 / IBM) you will find below. | | The algorithm how to calculate CRC - 16(also known as CRC - 16 / IBM) you will find below. |
| | | |
− | var crc = { | + | var crc = { |
| extractCRC: function (message) { | | extractCRC: function (message) { |
| crcData = message.slice(message.length - 2, message.length); | | crcData = message.slice(message.length - 2, message.length); |
Line 354: |
Line 354: |
| let idx = 0; | | let idx = 0; |
| let crc = 0x0000; | | let crc = 0x0000; |
− | | + | |
| /* the payload length is calculated with removing size of header and CRC (4 + 2 bytes) */ | | /* the payload length is calculated with removing size of header and CRC (4 + 2 bytes) */ |
| let payload_length = message.length - 6; | | let payload_length = message.length - 6; |
− | | + | |
| /* the payload is separated from the message header and CRC (4 bytes ofset + length)*/ | | /* the payload is separated from the message header and CRC (4 bytes ofset + length)*/ |
| let payload = message.slice(4, 4 + payload_length); | | let payload = message.slice(4, 4 + payload_length); |
− | | + | |
| /* Init value for the first message is zero and for the upcoming messages it is the CRC value of the previous message */ | | /* Init value for the first message is zero and for the upcoming messages it is the CRC value of the previous message */ |
| crc = previous_crc; | | crc = previous_crc; |
− |
| + | |
| for (idx = 0; idx < payload_length; idx++) { | | for (idx = 0; idx < payload_length; idx++) { |
| let bit; | | let bit; |
− |
| |
| /* Use XOR operation for initial value and payload */ | | /* Use XOR operation for initial value and payload */ |
| crc ^= payload[idx]; | | crc ^= payload[idx]; |
− | | + | |
| for (bit = 0; bit < 8; bit++) { | | for (bit = 0; bit < 8; bit++) { |
| let carry_bit = crc & 0x01; | | let carry_bit = crc & 0x01; |
| crc >>= 0x01; | | crc >>= 0x01; |
| + | |
| if (carry_bit != 0) { | | if (carry_bit != 0) { |
| crc ^= crc_poly; | | crc ^= crc_poly; |
| } | | } |
| } | | } |
− | } | + | } |
− |
| |
| return crc; | | return crc; |
| } | | } |
− | }; | + | }; |
− | exports.crc = crc; | + | exports.crc = crc; |
| | | |
| ==File transfer visual flow== | | ==File transfer visual flow== |