uint16_t crc16_modbus(const uint8_t *data, size_t data_len) { uint16_t crc = 0xFFFF; for (size_t i = 0; i < data_len; ++i) { crc ^= (uint16_t)data[i]; for (uint8_t j = 0; j < 8; ++j) { if (crc & 1) { crc = (crc >> 1) ^ 0xA001; } else { crc = (crc >> 1); } } } return crc; }