Parse 8 bit value from string

I am receiving this string from a Pic micro using an RS484 to serial (USB) converter.


The last char, showing as a replacement char of something that cannot be displayed, it is a 8bit value.
It represent the checksum.
I am extracting that char from the string using:

let cs = m.substr(33,1);

This gives me this: �
I have tried to convert it using:


This gives me an empty string " "

cs=parseInt(cs, 8)

This gives me a NaN

Whatever I tried is not returning me the value. I have successfully decoded the rest of the string but not that last part!
Here is the decoding function with the string I receive copied into an injection node.

[{"id":"5780c7918e4f68a9","type":"function","z":"b114e20b20dbeb01","name":"","func":"let m = msg.payload\n\nlet mAddr = m.substr(1,2)\nlet sAdrr = m.substr(3,2);\nlet comm = m.substr(5,2);\nlet vc1 = m.substr(9,2) + m.substr(7,2);\nlet vc2 = m.substr(13,2)+ m.substr(11,2);\nlet vc3 = m.substr(17,2)+ m.substr(15,2);\nlet vc4 = m.substr(21,2);+ m.substr(19,2)\nlet t1 = m.substr(23,2);\nlet t2 = m.substr(25,2);\nlet t3 = m.substr(27,2);\nlet t4 = m.substr(29,2);\nlet flag = m.substr(31,2);\n//let cs = m.substr(33,1);\nlet cs = m.substr(33,1);\n\nvc1=parseInt(vc1, 16)/1000;\nvc2=parseInt(vc2, 16)/1000;\nvc3=parseInt(vc3, 16)/1000;\nvc4=parseInt(vc4, 16)/1000;\nt1=parseInt(t1, 16);\nt2=parseInt(t2, 16);\nt3=parseInt(t3, 16);\nt4=parseInt(t4, 16);\n//cs=String.fromCharCode(cs);\n//cs=parseInt(cs, 8)\n\n\n//formatting\nreturn {payload:{MasterAddress:mAddr,\nSlaveAddress:sAdrr,\nCommand:comm,\nVc1:vc1,\nVc2:vc2,\nVc3:vc3,\nVc4:vc3,\nT1:t1,\nT2:t2,\nT3:t3,\nT4:t4,\nFlag:flag,\nCS:cs\n}}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":440,"wires":[["23556ff73afb7655"]]},{"id":"23556ff73afb7655","type":"debug","z":"b114e20b20dbeb01","name":"Decode","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":580,"y":440,"wires":[]},{"id":"b521d8c461b7e66d","type":"inject","z":"b114e20b20dbeb01","name":"Dummy Value","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\u0002FE5A6144141A140C1413142324222000�","payloadType":"str","x":300,"y":520,"wires":[["5780c7918e4f68a9"]]}]

How can I get and parse that 8bit value to an integer?
Thanks for helping.

What do you get if you use Buffer.from()

Hi Colin,
I am learning on the go... I am not a coder.
How do I use Buffer.from() in this context?

In a function node, assuming the string is in msg.payload

msg.payload = Buffer.from(msg.payload)
return msg

Then feed it into a debug node.
Do you have control of the message? If so then change that code to send the data in, for example, JSON.

[Edit] I meant the data coming from the micro.

Thank you Colin,
that's quite interesting. I get an object with 4 arrays containing 36 values.
I will take it from there and rewrite the decoding function or just pickup the troubling value from the buffer arrays.

Thanks a million.

That's 1 buffer, with 37 values (it is showing them in blocks of 10 so you can access them more easily in the debug window).
You appear to have 33 printable characters, ending in two zeros (0x30) then there are four additional bytes. So what do you want out of those?

Are you in control of the pic code? If so then change it to make the data easier to extract.

I am not in control of the Pic code. There are 4 voltage measurements and 4 temperatures which I have extracted using substr(). I was battling with the last value which is a checksum to verify if the returned string is valid. But now I can get that value from the buffer and complete the verification.
Thank you again for the directions.

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