Convert Hex casted as string to 32 point Float

First time using Node-red so I apologize. I'm trying to parse out a Hex value from a string kicked back from an instrument and convert it to a float point. Essentially, we send command, sRN mvVolumeFlow , and the instrument replies with the following payload.

msg.payload : string[27]

"sRA mvVolumeFlow C42E1D10"

I then parse out the C42E1D10 with
var payload = msg.payload;
var values = payload.split(" "); /* Splits the incoming data */
var flow_hex_value = values[2]; // flow value */

at that point, I can't figure out the syntax to convert this to a float and return it but I keep screwing up the sytnax. Any thoughts?

Thank you.

Code:

[{"id":"95b143da.d370e","type":"inject","z":"a216953e.a3fc68","name":"getMassFlowrate","topic":"","payload":"\u0002sRN mvVolumeFlow\u0003","payloadType":"str","repeat":"","crontab":"","once":false,"x":177.10000610351562,"y":235,"wires":[["5552ea14.70f6d4","e837a5a1.c0205"]]},{"id":"7232bf86.9f82c","type":"debug","z":"a216953e.a3fc68","name":"","active":true,"console":"false","complete":"payload","x":806,"y":345,"wires":[]},{"id":"5552ea14.70f6d4","type":"debug","z":"a216953e.a3fc68","name":"","active":true,"console":"false","complete":"false","x":441,"y":438,"wires":[]},{"id":"e837a5a1.c0205","type":"serial out","z":"a216953e.a3fc68","name":"Serial Out","serial":"11dc6b6c.d6ba2d","x":444,"y":267,"wires":[]},{"id":"fdb80849.f06c78","type":"serial in","z":"a216953e.a3fc68","name":"Serial In","serial":"11dc6b6c.d6ba2d","x":632,"y":273,"wires":[["7232bf86.9f82c","f1c63d9c.32e6e"]]},{"id":"f1c63d9c.32e6e","type":"function","z":"a216953e.a3fc68","name":"parse function","func":"var payload = msg.payload;\n/* Splits the incoming data */\nvar values = payload.split(\" \");\n/* Saves the sensor readings */\nvar flow_hex_value = values[2]; // flow value */ \nvar flow_value = Buffer.from(flow_hex_value,\"hex\").ToString();\nreturn flow_value;\n//var data = {};\n/* Builds the payload to be published */\n//data.payload = {flow_value};\n//data.topic = \"/v1.6/devices/siemens\";\n//return data;\n","outputs":1,"noerr":0,"x":841,"y":272,"wires":[["4f5ebcc3.fadabc"]]},{"id":"4f5ebcc3.fadabc","type":"debug","z":"a216953e.a3fc68","name":"","active":true,"console":"false","complete":"payload","x":1049,"y":272,"wires":[]},{"id":"11dc6b6c.d6ba2d","type":"serial-port","z":"","serialport":"/dev/ttyS2","serialbaud":"57600","databits":"8","parity":"none","stopbits":"1","newline":"\u0003","bin":"false","out":"char","addchar":false}]

[edited to add in ``` on a separate line before your paste and ``` on a seperate line afterwards - note they are backticks and not single quotes]

Hi and welcome to the forum!

I didn't try running your flow but I noticed in your parse function you've for:

Buffer.from(flow_hex_value,"hex").ToString();

But I believe it should be:

Buffer.from(flow_hex_value,"hex").toString();

Edit: Also you cannot return a string from a function node. Instead set the value to msg.payload and then call return msg;

This doesnt seem to fix your issue yet though.

Is the result correct if you edit the parse function to contain:

var payload = msg.payload;
/* Splits the incoming data */
var values = payload.split(" ");
/* Saves the sensor readings */
var flow_hex_value = values[2]; // flow value */ 
var flow_value = Buffer.from(flow_hex_value,"hex").readFloatLE();
msg.payload = flow_value;
return msg;

This returns 3.0998817456946907e-29 for your example response.

or maybe readFloatBE() ?
gives -696.4541015625

awesome, I did get a value and just switched it to .readFloatBE() per dccejay's suggestion and that is working! thank you both very much for the help, I really appreciate it.

1 Like

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