Splitting and Assigning String Data

Hi,

I am using the Serial Node to Read Data from a serial USB device, it returns the following string:
z+2.524+2.280+2.109+0.000
Can anyone point me int he right direction of how to:
Remove the 'z' and then split each value out and assign each value with a seperate 'channel' so I can distinguish between each channel from my device.

Thanks

How about this as a starting point?

[{"id":"9948412e.7102c8","type":"inject","z":"b9924a74.4d98f8","name":"","topic":"","payload":"z+2.524+2.280+2.109+0.000","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":4470,"wires":[["8c09fe88.a41d4"]]},{"id":"8c09fe88.a41d4","type":"split","z":"b9924a74.4d98f8","name":"","splt":"+","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":260,"y":4470,"wires":[["d2512309.42fb8","63d52c69.73181c"]]},{"id":"d2512309.42fb8","type":"debug","z":"b9924a74.4d98f8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":260,"y":4420,"wires":[]},{"id":"63d52c69.73181c","type":"switch","z":"b9924a74.4d98f8","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"4","vt":"num"},{"t":"eq","v":"5","vt":"num"}],"checkall":"true","repair":false,"outputs":5,"x":410,"y":4470,"wires":[["8a6ffe84.0403b8"],["4f6ebec3.095e58"],["9d02e6fc.3ab27"],["af01a655.f963a"],["9173b25c.e3be4"]]},{"id":"8a6ffe84.0403b8","type":"debug","z":"b9924a74.4d98f8","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":560,"y":4390,"wires":[]},{"id":"4f6ebec3.095e58","type":"debug","z":"b9924a74.4d98f8","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":560,"y":4430,"wires":[]},{"id":"9d02e6fc.3ab27","type":"debug","z":"b9924a74.4d98f8","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":560,"y":4470,"wires":[]},{"id":"af01a655.f963a","type":"debug","z":"b9924a74.4d98f8","name":"4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":560,"y":4510,"wires":[]},{"id":"9173b25c.e3be4","type":"debug","z":"b9924a74.4d98f8","name":"5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":560,"y":4550,"wires":[]}]

I went over the top with outputs, but it will show you how to expand on things if you need more.

If you don't, just edit the switch node and delete the last output.

1 Like

I would probably use a function node to convert it to a javascript object, something like this

const splitted = msg.payload.split("+")
msg.payload = {field1: splitted[1], field2: splitted[2], field3: splitted[3], field4: splitted[4]}
return msg;

Obviously change field1 etc to appropriate names.

[{"id":"46ff6251.23cd64","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload","v":"z+2.524+2.280+2.109+0.000","vt":"str"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"z+2.524+2.280+2.109+0.000","payloadType":"str","x":110,"y":1000,"wires":[["52cffe47.f91248"]]},{"id":"52cffe47.f91248","type":"function","z":"bdd7be38.d3b55","name":"","func":"const splitted = msg.payload.split(\"+\")\nmsg.payload = {field1: splitted[1], field2: splitted[2], field3: splitted[3], field4: splitted[4]}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":280,"y":1000,"wires":[["46b3ee82.308e88"]]},{"id":"46b3ee82.308e88","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":1000,"wires":[]}]

I would also do it in a function node.

msg.payload = msg.payload.split("+").slice(1);

edit/ did not catch the seperate channel part .

1 Like

You could also try a csv node set to split on +

1 Like

Cheers, I have implemented your solution and it works perfect!

Thanks for your time to write a response and flow.

Cheers Colin, much appreciated

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