Node-Red Double Word to 32 bit Int Conversion

Post Reply
Cloud
Posts: 11
Joined: 25 Apr 2018, 03:18

Node-Red Double Word to 32 bit Int Conversion

Post by Cloud »

Hello guys ,

I want to use Node-Red to receive data from codesys via modbus tcp and then send to cloud using MQTT. The original data is a DINT (say -593) in codesys, I split it into two words to Modbus TCP (High word "FFFF" and low word "FDAF").
Now in Node-Red I have received two words in sequence and combined them to a DWORD "FFFFFDAF" , but after I use parseInt in JS I got the number 4294966703, instead of -593. I suppose it is converted into a 64-bit int rather then 32. How can I change it to a 32-bit int? Below is my convert code. Thank you for your support.
Code.png
Code.png (95.25 KiB) Viewed 8920 times
Zhiyao Yin
dfilsinger-erminas
Official 3rd Party Support Erminas
Posts: 6
Joined: 16 Mar 2022, 18:54

Re: Node-Red Double Word to 32 bit Int Conversion

Post by dfilsinger-erminas »

Hello Zhiyao Yin,
basically, you would have to use the two's complement to convert the DWORD into Int.
To get around this, you can convert the result of parseInt() into 32-bit-int by forcing it with a binary operation. In this case, the or operation is always done by parseInt(), so this has no other effects.
code.png
code.png (32.49 KiB) Viewed 8581 times
Regards
Dennis from Erminas
Cloud
Posts: 11
Joined: 25 Apr 2018, 03:18

Re: Node-Red Double Word to 32 bit Int Conversion

Post by Cloud »

dfilsinger-erminas wrote: 09 Nov 2022, 10:15 Hello Zhiyao Yin,
basically, you would have to use the two's complement to convert the DWORD into Int.
To get around this, you can convert the result of parseInt() into 32-bit-int by forcing it with a binary operation. In this case, the or operation is always done by parseInt(), so this has no other effects.
code.png

Regards
Dennis from Erminas
Got it. Thanks Dennis.
Post Reply