function calculateCRC16Modbus(dataHexString) {
const dataBytes = [];
for (let i = 0; i < dataHexString.length; i += 2) {
dataBytes.push(parseInt(dataHexString.substr(i, 2), 16));
}
let crc = 0xFFFF;
const polynomial = 0xA001; // This is the polynomial x^16 + x^15 + x^2 + 1
for (const byte of dataBytes) {
crc ^= byte;
for (let i = 0; i < 8; i++) {
if (crc & 0x0001) {
crc = ((crc >> 1) ^ polynomial) & 0xFFFF;
} else {
crc >>= 1;
}
}
}
return crc.toString(16).toUpperCase();
}
const dataHexString = "01031C100001";
const crc = calculateCRC16Modbus(dataHexString);
console.log(crc); // Output the CRC as an uppercase hexadecimal string