String parsing, need help extracting values from string and assinging variables

Hi guys,

Hoping someone can help, I have a flow that I am having difficulty with, It is fairly simple:

I send a hex code requesting some energy data from a meter through a serial request node and I get an ASCII string in return and I need to extract the data for voltage, current , frequency ect. I have matlab code that outlines a working structure but I dont know how to parse the string, I have tried parseInt but my syntax is wrong? Essentially i need to emulate the hex2dec funcitons from the matlab code below but I dont know how to add bytes of data together also.

Working Matlab Code:

clear all

% Set up the serial port

SerialObj=serial('COM11');

set(SerialObj,'BaudRate',9600)

set(SerialObj,'parity','none')

set(SerialObj,'databit',8)

set(SerialObj,'stopbit',1)

set(SerialObj,'Timeout',1)

% Open the serial port for coms

fopen(SerialObj)

% use to open the meter coms

A1=['FE';'FE';'FE';'FE';'68';'99';'99';'99';'99';'99';'99';'68';'23';'0A';'60';'00';'34';'12';'78';...

'56';'BC';'9A';'F0';'DE';'2B';'16'];

A1_dec = hex2dec(A1);

fwrite(SerialObj,A1_dec,'uint8');

S1=fread(SerialObj,18);

% use to read phase 1 data

A2=['FE';'FE';'FE';'FE';'68';'99';'99';'99';'99';'99';'99';'68';'23';'02';'61';'00';'EC';'16'];

A2_dec = hex2dec(A2);

fwrite(SerialObj,A2_dec,'uint8');

S2=fread(SerialObj,47);

% Extract meter data into values

Voltage1=hex2dec([dec2hex(S2(18,1)),dec2hex(S2(17,1))])/100

Current1=hex2dec([dec2hex(S2(20,1)),dec2hex(S2(19,1))])/1000

ActivePower1=hex2dec([dec2hex(S2(24,1)),dec2hex(S2(23,1)),dec2hex(S2(22,1)),dec2hex(S2(21,1))])/100

ReactivePower1=hex2dec([dec2hex(S2(28,1)),dec2hex(S2(27,1)),dec2hex(S2(26,1)),dec2hex(S2(25,1))])/100

ApparentPower1=hex2dec([dec2hex(S2(32,1)),dec2hex(S2(31,1)),dec2hex(S2(30,1)),dec2hex(S2(29,1))])/100

PowerFactor1=hex2dec([dec2hex(S2(34,1)),dec2hex(S2(33,1))])

Frequency1=hex2dec([dec2hex(S2(36,1)),dec2hex(S2(35,1))])/100

PhaseAngle1=hex2dec([dec2hex(S2(38,1)),dec2hex(S2(37,1))])/100000

pause(0.1)

Node-Red flow:

[{"id":"a9abc99d.b5ce98","type":"tab","label":"ENERGY","disabled":false,"info":""},{"id":"8f28f81a.fa5d78","type":"inject","z":"a9abc99d.b5ce98","name":"Phase 1 request","topic":"","payload":"[\"254\",\"254\",\"254\",\"254\",\"104\",\"153\",\"153\",\"153\",\"153\",\"153\",\"153\",\"104\",\"35\",\"2\",\"97\",\"0\",\"236\",\"22\"]","payloadType":"bin","repeat":"60","crontab":"","once":true,"onceDelay":"10","x":190,"y":120,"wires":[["3941c038.db006"]]},{"id":"3941c038.db006","type":"serial request","z":"a9abc99d.b5ce98","name":"RS232 Serial","serial":"32946f34.c5e3b","x":430,"y":160,"wires":[["390f9b3b.44d4e4","92f377f.268d088"]]},{"id":"f72be214.8c7cb","type":"influxdb out","z":"a9abc99d.b5ce98","influxdb":"84041522.a267b8","name":"","measurement":"Phase 1","precision":"s","retentionPolicy":"","x":1350,"y":340,"wires":[]},{"id":"390f9b3b.44d4e4","type":"debug","z":"a9abc99d.b5ce98","name":"phase 1 output","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":680,"y":60,"wires":[]},{"id":"a7ee485a.eddd18","type":"inject","z":"a9abc99d.b5ce98","name":"OPEN COMMS","topic":"","payload":"[\"254\",\"254\",\"254\",\"254\",\"104\",\"153\",\"153\",\"153\",\"153\",\"153\",\"153\",\"104\",\"35\",\"10\",\"96\",\"0\",\"52\",\"18\",\"120\",\"86\",\"188\",\"154\",\"240\",\"222\",\"43\",\"22\"]","payloadType":"bin","repeat":"","crontab":"","once":true,"onceDelay":"5","x":300,"y":40,"wires":[["3941c038.db006"]]},{"id":"a41350d4.143d8","type":"inject","z":"a9abc99d.b5ce98","name":"Phase 2 request","topic":"","payload":"[\"254\",\"254\",\"254\",\"254\",\"104\",\"153\",\"153\",\"153\",\"153\",\"153\",\"153\",\"104\",\"35\",\"2\",\"98\",\"0\",\"237\",\"22\"]","payloadType":"bin","repeat":"60","crontab":"","once":true,"onceDelay":"30","x":190,"y":180,"wires":[["a713c5f7.695f98"]]},{"id":"6c279c7d.137204","type":"inject","z":"a9abc99d.b5ce98","name":"Phase 3 request","topic":"","payload":"[\"254\",\"254\",\"254\",\"254\",\"104\",\"153\",\"153\",\"153\",\"153\",\"153\",\"153\",\"104\",\"35\",\"2\",\"99\",\"0\",\"238\",\"22\"]","payloadType":"bin","repeat":"60","crontab":"","once":true,"onceDelay":"50","x":190,"y":240,"wires":[["def9e901.9dda88"]]},{"id":"a713c5f7.695f98","type":"serial request","z":"a9abc99d.b5ce98","name":"RS232 Serial","serial":"32946f34.c5e3b","x":430,"y":220,"wires":[["ad66f3a9.6c604","7e64dd66.02f784"]]},{"id":"def9e901.9dda88","type":"serial request","z":"a9abc99d.b5ce98","name":"RS232 Serial","serial":"32946f34.c5e3b","x":430,"y":280,"wires":[["ab4b67b1.fd3e88","423e126b.a9497c"]]},{"id":"ad66f3a9.6c604","type":"debug","z":"a9abc99d.b5ce98","name":"phase 2 output","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":700,"y":100,"wires":[]},{"id":"ab4b67b1.fd3e88","type":"debug","z":"a9abc99d.b5ce98","name":"phase 3 output","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":720,"y":140,"wires":[]},{"id":"92f377f.268d088","type":"function","z":"a9abc99d.b5ce98","name":"Buffer to String","func":"return {payload: msg.payload.toString('hex')};","outputs":"1","noerr":0,"x":680,"y":260,"wires":[["a094a0f0.3bd36"]]},{"id":"7e64dd66.02f784","type":"function","z":"a9abc99d.b5ce98","name":"Buffer to String","func":"return {payload: msg.payload.toString('hex')};","outputs":"1","noerr":0,"x":680,"y":300,"wires":[["fefcf5c1.e55588"]]},{"id":"423e126b.a9497c","type":"function","z":"a9abc99d.b5ce98","name":"Buffer to String","func":"return {payload: msg.payload.toString('hex')};","outputs":"1","noerr":0,"x":680,"y":340,"wires":[["a0656f70.523e3"]]},{"id":"a094a0f0.3bd36","type":"function","z":"a9abc99d.b5ce98","name":"","func":"return [ {payload: new Buffer(msg.payload, \"hex\")}];","outputs":1,"noerr":0,"x":870,"y":300,"wires":[["b0287a4b.c59dc8"]]},{"id":"fefcf5c1.e55588","type":"function","z":"a9abc99d.b5ce98","name":"","func":"return [ {payload: new Buffer(msg.payload, \"hex\")}];","outputs":1,"noerr":0,"x":870,"y":340,"wires":[["b0b52d20.466a8"]]},{"id":"a0656f70.523e3","type":"function","z":"a9abc99d.b5ce98","name":"","func":"return [ {payload: new Buffer(msg.payload, \"hex\")}];","outputs":1,"noerr":0,"x":870,"y":380,"wires":[["5fdf02ee.d0bedc"]]},{"id":"b0287a4b.c59dc8","type":"function","z":"a9abc99d.b5ce98","name":"Hexadecimal to integer","func":"\nVRMS = parseInt(0xB0,16);\n\nIRMS = parseInt(0xB1,16);\n\nVPeak = parseInt(0xB2,16);\n\nIPeak = parseInt(0xB3,16);\n\nFrequency = parseInt(0xB5,16);\n\nActivePower = parseInt(0xB6,16);\n\nReactivePower = parseInt(0xB7,16);\n\nApparentPower = parseInt(0xB8,16);\n\nActiveEnergy = parseInt(0xB9,16);\n\nReactiveEnergy = parseInt(0xBA,16);\n\nApparentEnergy = parseInt(0xBB,16);\n\nreturn [VRMS,IRMS,VPeak,IPeak,Frequency,ActivePower,ReactivePower,ApparentPower,ActiveEnergy,ReactiveEnergy,ApparentEnergy];","outputs":1,"noerr":0,"x":1100,"y":320,"wires":[["f72be214.8c7cb","d77029cc.1021a8"]]},{"id":"8a0e0d5e.30cfd","type":"influxdb out","z":"a9abc99d.b5ce98","influxdb":"84041522.a267b8","name":"","measurement":"Phase 2","precision":"s","retentionPolicy":"","x":1350,"y":400,"wires":[]},{"id":"b0b52d20.466a8","type":"function","z":"a9abc99d.b5ce98","name":"Hexadecimal to integer","func":"\nVRMS = parseInt(0xB0,16);\n\nIRMS = parseInt(0xB1,16);\n\nVPeak = parseInt(0xB2,16);\n\nIPeak = parseInt(0xB3,16);\n\nFrequency = parseInt(0xB5,16);\n\nActivePower = parseInt(0xB6,16);\n\nReactivePower = parseInt(0xB7,16);\n\nApparentPower = parseInt(0xB8,16);\n\nActiveEnergy = parseInt(0xB9,16);\n\nReactiveEnergy = parseInt(0xBA,16);\n\nApparentEnergy = parseInt(0xBB,16);\n\nreturn [VRMS,IRMS,VPeak,IPeak,Frequency,ActivePower,ReactivePower,ApparentPower,ActiveEnergy,ReactiveEnergy,ApparentEnergy];","outputs":1,"noerr":0,"x":1100,"y":380,"wires":[["8a0e0d5e.30cfd"]]},{"id":"98727545.fa1e68","type":"influxdb out","z":"a9abc99d.b5ce98","influxdb":"84041522.a267b8","name":"","measurement":"Phase 3","precision":"s","retentionPolicy":"","x":1350,"y":460,"wires":[]},{"id":"5fdf02ee.d0bedc","type":"function","z":"a9abc99d.b5ce98","name":"Hexadecimal to integer","func":"\nVRMS = parseInt(0xB0,16);\n\nIRMS = parseInt(0xB1,16);\n\nVPeak = parseInt(0xB2,16);\n\nIPeak = parseInt(0xB3,16);\n\nFrequency = parseInt(0xB5,16);\n\nActivePower = parseInt(0xB6,16);\n\nReactivePower = parseInt(0xB7,16);\n\nApparentPower = parseInt(0xB8,16);\n\nActiveEnergy = parseInt(0xB9,16);\n\nReactiveEnergy = parseInt(0xBA,16);\n\nApparentEnergy = parseInt(0xBB,16);\n\nreturn [VRMS,IRMS,VPeak,IPeak,Frequency,ActivePower,ReactivePower,ApparentPower,ActiveEnergy,ReactiveEnergy,ApparentEnergy];","outputs":1,"noerr":0,"x":1100,"y":440,"wires":[["98727545.fa1e68"]]},{"id":"d77029cc.1021a8","type":"debug","z":"a9abc99d.b5ce98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1310,"y":600,"wires":[]},{"id":"631eb7d6.2eba28","type":"inject","z":"a9abc99d.b5ce98","name":"OUTPUT SIMULATION","topic":"","payload":"����h������h#¬a� � � XR�W ","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":420,"y":480,"wires":[["92f377f.268d088"]]},{"id":"32946f34.c5e3b","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"47","bin":"bin","out":"count","addchar":"","responsetimeout":"10000"},{"id":"84041522.a267b8","type":"influxdb","z":"","hostname":"[127.0.0.1](https://127.0.0.1/)","port":"8086","protocol":"http","database":"400","name":"Energy","usetls":false,"tls":""}]

Welcome to the forum.
It is difficult to help unless you show us the string, feed it into a debug node and show us what you get.

HI colin,

The string is in inject node that I put in after the serial node in the flow

Maybe the following excellent node from @steve-mcl can help you with extracting the values from the binary input string (buffer).

1 Like

beat me to it :wink:

thanks for the vote of confidence.

Indead, the OP could replace about 60% of his nodes with buffer parser...

1 Like

Thank you kindly seems like the best solution

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