25.05.2017, 16:38
It does work, but the byte order is flipped. You just need to swap byteh and bytel when appending CRC to the source message.
Code:
msg = string.char(0x02, 0x03, 0x00, 0xD6, 0x35, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xBC, 0x10, 0x02, 0x00, 0x52, 0xDE)
crc = crc16modbus(msg)
-- split crc into 2 bytes
byteh = bit.rshift(crc, 8)
byteh = bit.band(byteh, 0xFF)
bytel = bit.band(crc, 0xFF)
-- high=B8 low=78
alert(string.format('high=%02X low=%02X', byteh, bytel))