CRC 16XMODEM node

Is there a node that does the crc calculation in question?

A quick search of the forum for crc would reveal a few solutions. Alternatively, see the video in this article: Use any npm module in Node-RED • FlowFuse

Thank you for your answer.
I tested the available nodes but they are not suitable in my case, in fact they are all for modbus.
the video is to be deepened ( I have to use subtitles) but I will see to deepen in the morning.
I have some examples of the result I would like to get, the data is sniffed from the system currently in use and I would like to post on node-red, in the table the first column represents the data to be sent in serial, the second column and the third represents what is actually translated into ascii and hex

QPIGS       QPIGS..         51 50 49 47 53 b7 a9
QMOD        QMODI.          51 4d 4f 44 49 c1 00
QPIWS       QPIWS..         51 50 49 57 53 b4 da
QPIRI       QPIRI.T         51 50 49 52 49 f8 54
PBFT55.4    PBFT55.4.i      50 42 46 54 35 35 2e 34 e0 69
PCVV55.4    PCVV55.4␆␏     50 43 56 56 35 35 2e 34 06 0f
POP00       POP00.H         50 4f 50 30 30 c2 48
PCP03       PCP03.␙         50 43 50 30 33 bd 19

The above data I have captured (sniffed) from serial traffic between devices currently in use. Trying the various nodes available gives me completely different results.
I believe there is certainly a solution... I would just like a little help to achieve the goal.
(Have mercy on my language but it is the result of translator)
thanks

Good afternoon. I’m realizing that with unicode characters is a mess...
Is it possible to send on the serial directly the characters in hex? you have to use some nodes or do something else?

You can build a Buffer object and send that.

Good morning.
Yes I did some testing and I managed to send the command in hex.
but it is not a definitive solution and brings with it a series of complications, which limit me in the commands to send.
I believe that the optimal solution is to implement the "16xmodem" CRC.
but I don’t know where to start... can the "function" node be used to get what I need?
Thanks for the help

Yes, i linked you to an article that has a section detailing "how to conduct CRC calculations" with an instructional video...

It uses a library called easy-crc which supports XMODEM

I tried this but it doesn’t give me any on the output.
Not even mistakes...
It’s not my own money but somewhere I have to start...


I tried with your examples.... and it works... as you could expect!

the crc seems to be correct now I would like to join the crc to the input string and create a buffer to pass to the serial in hex (not to run into undecipherable unicode characters).
I tried to use the join node but I didn’t understand much about how to use it.
but if you give me a tip I would incorporate the function node work join.
help me


what I need to add/edit

put the function in series, after the parser, then update the data to append your CRC.

inject -> parse -> calculate CRC & add it to your data -> done

No need for join node here.



Good morning.

I spent some time to do tests and can not understand why I do not get the correct result.
Using the two nodes (which should do the same thing) I get different results. in one case I get the correct crc but it is not hung to the original data, in the second case I am hung but the crc is wrong. Probably wrong something trivial... but I can not solve.
Thanks for the help

By changing a little you get this

in the first case, it calculates the CRC and returns that (only that). If you want it to be appended you will need to append it!

in the 2nd case, ...


Tell you what. just share your flow (screenshots of code are useless).
Select the nodes, Use CTRL+E to export, paste into a code block.

Also, tell me EXACTLY what you expect to see in the output. A string of "xxxx..."? A buffer containing [xx,xx,xx,...]? something else??

I assume you want a string with the hexadecimal value of the CRC to be appended?

You need to understand number conversions and specify what is hex, what is ASCII what is decimal etc.

Try this:

const { crc16 } = easyCrc;
msg.inputData = msg.payload;
msg.buffer = Buffer.from(msg.payload, 'hex');
msg.crcDecimal = crc16('XMODEM', msg.buffer);
msg.crcHex = msg.crcDecimal.toString(16)
msg.payload = `${msg.inputData}${msg.crcHex}`
return msg;

You dont really need to put all the intermediate values into the msg, but as you can see from the above screenshot, it helps you debug what is going on!

Below is the less informative, but concise version:

const { crc16 } = easyCrc;
const crc = crc16('XMODEM', Buffer.from(msg.payload, 'hex')).toString(16);
msg.payload = `${msg.payload}${crc}`
return msg;

ok. what I would like to get is:
given an ascii string (since the codes to be sent to the device are encoded as "QPIGS"), you want to get the same thing from the input with addition of the CRC.
from what I could understand in my research using the encoding "ascii" or better "unicode" some values correspond to particular characters that sometimes give problems, so I thought that using the values in hexadecimal (hex) this problem could be solved. The transmission on serial will actually be done in the same way or in binary.
I don’t know exactly what the best way to do it is but anything that solves the problem is well accepted.

Is there a question here?

Is your issue resolved?

If you REALLY want to stop messing around with ASCII conversions and want to support characters with a CHARCODE > 127, then stick to buffers.

Set your serial nodes to output a buffer.

Modify the function to output a buffer.

Utilise the buffer-maker and buffer-parser nodes in the package node-red-contrib-buffer-parser to simplify this.

[
    {
        "id": "edf507bc4f9d5dec",
        "type": "serial out",
        "z": "2e439fa544a77ab5",
        "name": "VMIII-TX",
        "serial": "831becf24befe30b",
        "x": 1140,
        "y": 1260,
        "wires": []
    },
    {
        "id": "2a01c2156f8e9065",
        "type": "inject",
        "z": "2e439fa544a77ab5",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "5150494753",
        "payloadType": "str",
        "x": 570,
        "y": 1260,
        "wires": [
            [
                "f422daaff4841c2a"
            ]
        ]
    },
    {
        "id": "f422daaff4841c2a",
        "type": "function",
        "z": "2e439fa544a77ab5",
        "name": "xmodem crc16 (easy-crc)",
        "func": "const { crc16 } = easyCrc;\nconst crc = crc16('XMODEM', Buffer.from(msg.payload, 'hex')).toString(16);\nmsg.payload = `${msg.payload}${crc}`\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [
            {
                "var": "easyCrc",
                "module": "easy-crc"
            }
        ],
        "x": 830,
        "y": 1260,
        "wires": [
            [
                "edf507bc4f9d5dec",
                "f3b7e8370bf8bbea"
            ]
        ]
    },
    {
        "id": "831becf24befe30b",
        "type": "serial-port",
        "name": "",
        "serialport": "/dev/ttyACM0",
        "serialbaud": "2400",
        "databits": 8,
        "parity": "none",
        "stopbits": 1,
        "waitfor": "(",
        "dtr": "none",
        "rts": "none",
        "cts": "none",
        "dsr": "none",
        "newline": "\\r",
        "bin": "false",
        "out": "char",
        "addchar": "\\r",
        "responsetimeout": 10000
    }
]

this seems to be ok from the processing point of view the crc is right and it is hung, but it is not recognized by the device to which it is transmitted... probably has to be formatted differently or the serial node accepts only text format, because if I transmit the command as text it works.