* @brief Computes a 16-bit CRC (CRC-16-MODBUS) using polynomial 0x8005 (reflected: 0xA001). */
| 199 | * @brief Computes a 16-bit CRC (CRC-16-MODBUS) using polynomial 0x8005 (reflected: 0xA001). |
| 200 | */ |
| 201 | static 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. |