MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / crc16_modbus

Function crc16_modbus

app/src/IO/Checksum.cpp:201–215  ·  view source on GitHub ↗

* @brief Computes a 16-bit CRC (CRC-16-MODBUS) using polynomial 0x8005 (reflected: 0xA001). */

Source from the content-addressed store, hash-verified

199 * @brief Computes a 16-bit CRC (CRC-16-MODBUS) using polynomial 0x8005 (reflected: 0xA001).
200 */
201static constexpr uint16_t crc16_modbus(const char* data, const int length) noexcept
202{
203 uint16_t crc = 0xFFFF;
204
205 for (int i = 0; i < length; ++i) {
206 crc ^= static_cast<uint8_t>(data[i]);
207 for (int j = 0; j < 8; ++j)
208 if (crc & 0x0001)
209 crc = (crc >> 1) ^ 0xA001;
210 else
211 crc >>= 1;
212 }
213
214 return crc;
215}
216
217/**
218 * @brief Computes a 16-bit CRC (CRC-CCITT) using polynomial 0x1021 and initial value 0x0000.

Callers 1

Checksum.cppFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected