Skip to main content

CRC16

Latest Version: 1.0.0

The CRC16 library implements CRC-16 (also known as CRC-16-ANSI or CRC-16-IBM) which is used in propular protocols such as Modbus and USB. The CRC16 class contains a single static function — calculate() — which computes the CRC16 of the data passed in.

You can view the library’s source code on GitHub. Click here to see information on other versions of this library.

To add this library to your project, add #require "CRC16.class.nut:1.0.0" to the top of your agent and/or device code.

Class Methods

CRC16.calculate(data[, start][, end][, initialValue])

The calculate() method calculates the CRC16 value of data, which should be in the form of a string or a blob.

You can pass optional parameters (start and end) to specify the range. If no range is specified, the CRC will be computed against the entire data structure.

You may also pass an optional initialValue, which can be used for chaining together multiple CRC calculations.

#require "CRC16.class.nut:1.0.0"

// Verification Values from:
// http://www.lammertbies.nl/comm/info/crc-calculation.html
// Make sure to use "hex" option for non-typable characters

//Simple Test
local crc = CRC16.calculate("HelloCRC16");
server.log(format("0x%04X", crc)); // Should log 0x6AF6

//More advanced tests
tests <- [
    ["\x00\x00\x00\x00", 0x2400],
    [blob(4), 0x2400],
    ["\x00\x00\x00\x01", 0xE4C1],
    ["\x01\x23\x45\x67\x89\xAB\xCD\xEF", 0xF8E6],
    ["QWERTY", 0x7132]
];

foreach (pair in tests) {
    if (CRC16.calculate(pair[0]) == pair[1]) {
        server.log("Complete CRC Passed");
    } else {
        server.error("Complete CRC Failed");
    }

    // Compute the CRC from bytes 0-2, then from 2-end using the
    // first part CRC as the initial value
    local firstPart = CRC16.calculate(pair[0], 0, 2);
    local secondPart = CRC16.calculate(pair[0], 2, null, firstPart);

    if (secondPart == pair[1]) {
        server.log("Partial CRC Passed")
    } else {
        server.error("Partial CRC Failed")
    }
}

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release

License

The CRC16 class is licensed under MIT License.