Changes

Line 316: Line 316:     
==CRC-16 calculation==
 
==CRC-16 calculation==
 +
CRC is calculated with using 3 input variables:
 +
 +
1. init_value - last packet CRC (or 0 if it's the first packet)<br>
 +
2. poly - binary polynomial expression (0x8408 in our case)<br>
 +
3. data - raw data of the packet (not including the first 4 bytes (cmd ID and packet size) and 2 last bytes (the CRC)
 +
 +
An example of structure can be found below:
 +
 +
function crc16_generic(init_value, poly, data) {
 +
    let RetVal = init_value;
 +
    let offset;
 +
 +
    for (offset = 0; offset < data.length; offset++) {
 +
        let bit;
 +
        RetVal ^= data[offset];
 +
        for (bit = 0; bit < 8; bit++) {
 +
            let carry = RetVal & 0x01;
 +
            RetVal >>= 0x01;
 +
            if (carry) {
 +
                RetVal ^= poly;
 +
            }
 +
        }
 +
    }
 +
    return RetVal;
 +
}
 +
 
CRC (Cyclic Redundancy Check) is an error-detecting code using for detect accidental changes to RAW data. The algorithm how to calculate CRC-16 (also known as CRC-16/IBM) you will find below.
 
CRC (Cyclic Redundancy Check) is an error-detecting code using for detect accidental changes to RAW data. The algorithm how to calculate CRC-16 (also known as CRC-16/IBM) you will find below.
  
0

edits

Navigation menu