# Node serial port, how to perform crc check

**URL:** https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425
**Category:** General
**Tags:** function-node
**Created:** [28 October 2023 14:17 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425 "2023-10-28T14:17:43Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)
#### Post date: [28 October 2023 14:17 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/1 "2023-10-28T14:17:43Z")

</div>

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.

Can anyone help me with this issue?

 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4e0aab1e7d9d3ffe298fa4f976de680689cf6b66.png)

This is the output from the serial-in node:

 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/e/2e36a1551d0ef1b9b54deeb300167ccb0c528d03.png)

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [28 October 2023 15:50 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/2 "2023-10-28T15:50:53Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)
#### Post date: [28 October 2023 16:20 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/3 "2023-10-28T16:20:44Z")

</div>

The value after the exclamation mark is the crc value. So in this case 6FEC.

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [28 October 2023 16:28 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/4 "2023-10-28T16:28:50Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [28 October 2023 16:36 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/5 "2023-10-28T16:36:42Z")

</div>

Assuming you know how to extract that CRC at the end of the data, you can likely use [this solution](https://discourse.nodered.org/t/creating-crc16-xmodem-hex-value/81981/3) to compute and verify it.

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [28 October 2023 16:38 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/6 "2023-10-28T16:38:21Z")

</div>

Nice!

I was looking for a library to use as an example - you saved me having to do it. 🙏

---

<div class="post-metadata">

### Author: ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)
#### Post date: [28 October 2023 17:47 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/7 "2023-10-28T17:47:36Z")

</div>

@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,

```auto
/Ene5\T211 ESMR 5.0

1-3:0.2.8(50)

0-0:1.0.0(231028193754S)

0-0:96.1.1(4530303632303030303130313639363232)

1-0:1.8.1(000545.093*kWh)

1-0:1.8.2(000237.780*kWh)

1-0:2.8.1(001126.383*kWh)

1-0:2.8.2(002106.647*kWh)

0-0:96.14.0(0001)

1-0:1.7.0(02.354*kW)

1-0:2.7.0(00.000*kW)

0-0:96.7.21(00008)

0-0:96.7.9(00004)

1-0:99.97.0(1)(0-0:96.7.19)(220816003006S)(0000000380*s)

1-0:32.32.0(00001)

1-0:52.32.0(00001)

1-0:72.32.0(00002)

1-0:32.36.0(00000)

1-0:52.36.0(00000)

1-0:72.36.0(00000)

0-0:96.13.0()

1-0:32.7.0(225.0*V)

1-0:52.7.0(227.0*V)

1-0:72.7.0(224.0*V)

1-0:31.7.0(001*A)

1-0:51.7.0(000*A)

1-0:71.7.0(008*A)

1-0:21.7.0(00.301*kW)

1-0:41.7.0(00.133*kW)

1-0:61.7.0(01.920*kW)

1-0:22.7.0(00.000*kW)

1-0:42.7.0(00.000*kW)

1-0:62.7.0(00.000*kW)

0-1:24.1.0(003)

0-1:96.1.0(4730303733303034313539343039353232)

0-1:24.2.1(231028193500S)(00019.612*m3)

!668D

```

I guess it must be crc16. What part of the telegram is crc calculated to get the good result?

> **[Online CRC-8 CRC-16 CRC-32 Calculator](https://crccalc.com/?crc=01C80101C900000000000000&method=crc32&datatype=hex&outtype=0)**
>
> Calculate CRC-8, CRC-16, CRC-32 checksums online

I just found out, that the type of CRC algorithm = CRC-16-IBM. This algorithm is not in the CRC calculator and also not present in easy-CRC!

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [28 October 2023 18:05 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/8 "2023-10-28T18:05:43Z")

</div>

I have just put this together - it uses `easy-crc`

> [@A Simple CRC Checksum Node (Subflow)](https://discourse.nodered.org/t/a-simple-crc-checksum-node-subflow/82429):
>
> This isn't a published node, but a subflow to easily validate checksums. The subflow requires the following inputs: crcLength : example : 8 crcChecksum : example : "362d" crcAlgorithm : example: "XMODEM" The check will be applied against the payload - so you will need to remove any checksum that maybe part of the value to be checked. The subflow will check the type of the payload and if its a: Buffer - will use as is String - will create a buffer from it Object - will stringify it befor…

---

<div class="post-metadata">

### Author: ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)
#### Post date: [28 October 2023 18:05 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/9 "2023-10-28T18:05:57Z")

</div>

@marcus-j-davies @Steve-Mcl

> [@henkkas](#):
>
> I guess it must be crc16. What part of the telegram is crc calculated to get the good result?

That seems to be from "/" until "!" included. See also my previous remarks about the calculator.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [28 October 2023 18:26 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/10 "2023-10-28T18:26:37Z")

</div>

> [@henkkas](#):
>
> ```auto
> 668D
> 
> ```
> 
> I guess it must be crc16

Since it is a 4 digit WORD, I believe it will be a 32bit CRC. It could be a well known or a completely made up CRC.

---

<div class="post-metadata">

### Author: ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)
#### Post date: [28 October 2023 18:27 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/11 "2023-10-28T18:27:27Z")

</div>

@Steve-Mcl @marcus-j-davies I found a python programm that seems to do the trick.

```auto
def calc_crc_telegram(telegram):
x = 0
y = 0
crc = 0
#print "Lengte telegram",len(telegram)
while (x < len(telegram)):
crc = crc ^ ord(telegram[x])
x = x + 1
y = 0
while (y < 8):
if ((crc & 1) != 0):
crc = (crc >> 1)
crc = crc ^ (int("0xA001",16))
else:
crc = crc >> 1
y = y + 1
#print "CRC calculated :", crc
return crc

```

I just need to translate it to javascript. 😃

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [28 October 2023 18:33 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/12 "2023-10-28T18:33:16Z")

</div>

Shamelessly pulled from a converter 👼 (untested)

```auto
function calc_crc_telegram(telegram) {
    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;
}

```

---

<div class="post-metadata">

### Author: ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)
#### Post date: [28 October 2023 18:36 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/13 "2023-10-28T18:36:10Z")

</div>

@marcus-j-davies Woow great, I was struggling already.

Will test it in one hour or so, have to do some urgent things.

---

<div class="post-metadata">

### Author: ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)
#### Post date: [28 October 2023 19:31 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/14 "2023-10-28T19:31:11Z")

</div>

@marcus-j-davies @Steve-Mcl Yep it is working.

I made a test function like this,

```auto
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.

---

<div class="post-metadata">

### Author: ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)
#### Post date: [26 November 2023 09:12 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/15 "2023-11-26T09:12:06Z")

</div>

@marcus-j-davies @Steve-Mcl

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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [10 December 2023 09:12 UTC](https://discourse.nodered.org/t/node-serial-port-how-to-perform-crc-check/82425/16 "2023-12-10T09:12:48Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
