bin.crc16()

bin.crc16(data,len) → number

Modbus CRC16 checksum function

Generate a crc16 checksum from a chunk of data

Arguments

NameTypeRequiredDefaultDescription
data string yes
len number optional data.length

Returns

number Checksum Value

Example

serial.init(2,9600);

let modbusFrame = bin.alloc(8);
modbusFrame.write("U8",3); // Slave Address
modbusFrame.write("U8",0x06); // 0x06 Function Code (Write Single Register)
modbusFrame.write("U16BA",0); // Register
modbusFrame.write("U16BA",1234); // Value
let crc = modbus.crc16(modbusFrame,6); // Generate the CRC from the first 6 bytes of our payload
modbusFrame.write("U16BA,crc); // write the CRC to the end of our message
 
serial.tx(2,modbusFrame); // Send the frame over the RS485 port.