I am using the node 'serial in' from node-red-node-serial-port, to read data from the smart meter.
This node is connected to a function node to process the data coming from the smart meter.
It works well, but sometimes I get corrupted telegrams. I am trying to figure out how to do a crc-check on the data, since there seems to be no option for this in the configuration in the serial-port node.
I'm not a pro, but to do a CRC check, the source of the data must provide one, and for you to do your own CRC check to ensure it matches with what was provided.
So if the original data does not carry a CRC - you have no way to determine if its altered/currupted.
I could be wrong here.
EDIT
The serial port node, will not have a place to do CRC - as it needs to understand the data structure, to be able to do so (and for the source to provide one)
Ok that makes things easier. Do you know the format of the CRC (8, 16, 32) etc?
There are node libraries, that you can use to calculate the CRC of a value.
Using such library, you can generate the CRC and check it with the provided CRC.
@Steve-Mcl and @marcus-j-davies I first tried getting the telegram through the crc calculator. I tried all different ways, but no outcome returned a match. This telegram I tried,
function calc_crc_telegram(telegram) {
// This function calculates a CRC with algorithm CRC-16-IBM,
// that is used in Dutch smart meters.
let x = 0;
let y = 0;
let crc = 0;
while (x < telegram.length) {
crc = crc ^ telegram.charCodeAt(x);
x = x + 1;
y = 0;
while (y < 8) {
if ((crc & 1) !== 0) {
crc = crc >> 1;
crc = crc ^ parseInt("0xA001", 16);
} else {
crc = crc >> 1;
}
y = y + 1;
}
}
return crc;
}
let telegram = msg.payload;
let str = telegram.split("!");
let telStr = str[0] + "!";
let crcStr = str[1];
let crc = calc_crc_telegram(telStr)
let h_crc = crc.toString(16).toUpperCase().padStart(4, '0');
msg = {telegram: str[0], crc: str[1], crc_calc: h_crc}
I wil integrate this in my project, hopefully I will then get rid of corrupted messages that appear now and then.I will tell you the result.
As I said, I integrated the solution to my project, and it worked well. About 30% of the messages was corrupt. By now I also know the reason of the corrupt messages. Before I had a 4-fold hub between the smart meter and the pi. Later I removed that. After the removal the corrupt messages started coming in. So I placed back the hub and the corrupt messages were gone. My conclusion is that this hub improves the signal coming from the smart meter. I have zero corrupt messages now.