Parsing ASCII Text - Resolved

I am receiving data formated as ASCII text as follows:

65,90,58,49,50,57,46,57,13 ; which means AZ:129.9

Using the Node-Red "text" ui node, it converts the message code to decimal as shown above. Good.

I want to use the same data to input into a ui gauge, so I need to strip away the "AZ:" portion of the message.

I am a novice Node-Red user.

What are the ways parsing can be done?

Alan

Can you pass a message containing that data to a debug node and then share a screenshot of the message? That will help us determine exactly what you've got and from that, how best to convert it to a proper JavaScript string you can start extracting bits from.

Thanks, Nick.

Here is the screenshot. I expanded two of the messages so you can see how Node-Red is handling the object.

Alan

Alan

Try in a Function node

msg.payload = msg.payload.toString()
return msg

and feed that into a debug node and check it gives you "AZ:129.9". If so then feed that into JSON node which should convert it from a JSON string to a javascript object. Again check in a debug node that you get {AZ: 129.9}. If so then you can reference the value as msg.payload.AZ in the gauge node.

No it won't - AZ:129/9 is not valid JSON.

@n61ab once you have the String, you can use the various JavaScript String functions to split it up as you require - String - JavaScript | MDN

I was confused there by the fact that you have 129/9 instead of 129.9, but I see what you mean. The string needs to be surrounded by { } to be JSON.

@n61ab use this in the function node instead, to make it valid JSON

msg.payload = "{" + msg.payload.toString() + "}"
return msg

@Colin that still isn't valid JSON - the AZ would need to be in quotes - "AZ". Forcing it into a JSON format isn't worth the effort.

@n61ab the screenshot you've shared shows that msg.payloads is a Buffer type. That is why the toString() function Colin suggests works.

Here's a quick function that will do the string conversion and then split out the number:

// Convert Buffer to String:
var data = msg.payload.toString()
// Split it using the `:` character
var parts = data.split(":");

// parts is now a two-element array, containing "AZ" and "129.9"
// use the second element:
msg.payload = parts[1];

return msg

Colin and knolleary

Thanks for your help. I will give this a try this afternoon and let you know how I do.

Alan

knolleary

I could not wait! Your function works great! How can I say thank-you enough?

Alan

Doh, I must remember to engage brain before typing.

1 Like