MODBUS TCP to MODBUS RTU with NODE-RED

var A = [8];
A[0]= msg.payload[6];
A[1]= msg.payload[7];
A[2]= msg.payload[8];
A[3]= msg.payload[9];
A[4]= msg.payload[10];
A[5]= msg.payload[11];

// Compute the MODBUS RTU CRC
var crc = 0xFFFF;

for (var pos = 0; pos < 6; pos++) { crc ^= A[pos]; // XOR byte into least sig. byte of crc for (var i = 8; i !== 0; i--) { // Loop over each bit if ((crc & 0x0001) !== 0) { // If the LSB is set crc >>= 1; // Shift right and XOR 0xA001
crc ^= 0xA001;
}
else // Else LSB is not set
crc >>= 1; // Just shift right
}
}
var TempS = crc.toString(16);

A[6] = parseInt(TempS.substring(0,2),10);
A[7] = parseInt(TempS.substring(2,4),10);

msg.payload = A;
return msg;

Leave a Reply

Your email address will not be published.