Reading the udp data with a frame header

I'm receiving data from a Dragino temp sensor via a UDP node. The payload contains 25 bytes that i need to convert to string.
According to Dragino, i have a frame definition i need to follow to i guess separate the bytes and convert them somehow to string.
How do i start?
The output i get from UDP is a Buffer.
I do not have any experience at all working with buffers and bytes :roll_eyes:
Here is the frame definition.

6|2|2|1|1|2|1|2|2|2|4|

I guess i would need to use the node-red-contrib-binary , but after reading the quick ref, I'm not sure i will have any success with it...

Here is the buffer array I have:

[114,64,49,96,112,105,0,120,11,213,8,1,0,0,0,0,11,0,188,1,236,97,102,185,61]

Might need some context here.

Does
[114,64,49,96,112,105,0,120,11,213,8,1,0,0,0,0,11,0,188,1,236,97,102,185,61]

Represent
6|2|2|1|1|2|1|2|2|2|4|

or is the definition you provided an example?
Should it be an actual string, or is the definition a series of integers?

The first array is the buffer from udp node...

Here are some instructions i have for the sensor.

Ok - this is handy.
Give me a few minutes.

Ok,

Something like this could be a starting point, in a function node.
(if I understand the document correctly)

let Cursor = 0;
function Read(buffer,length){
    let Buf =  buffer.slice(Cursor,Cursor+length)
    Cursor = Cursor + length
    return Buf;
}

let DeviceID = Read(msg.payload,6)
let Version = Read(msg.payload,2)
let Battery = Read(msg.payload,2)
let SignalStrength = Read(msg.payload,1)
let MOD = Read(msg.payload,1)
let TEMP0 = Read(msg.payload,2)
let DigitalIn = Read(msg.payload,1)
let ADC = Read(msg.payload,2)
let TEMP1 = Read(msg.payload,2)
let Humid = Read(msg.payload,2)
let Timestamp = Read(msg.payload,4)

Disclaimer: I have not checked this, and you may need to parse the sliced byte groups to an integer.

EDIT:
It seems to work.
Doing Timestamp.readInt32BE() yields 1634122045 => 2021-10-13T10:47:25.000Z

Will check it out when i get off work. Thanks for the quick responses sir :blush:

There is a node designed precisely for this kind of operation... node-red-contrib-buffer-parser

Search the forum for example uses. Read the "readme" and the built in help. Try out the built in demos (CTRL+I)

2 Likes

So I tested this, and the buffer array is now sorted throughout the properties, any help to actually get the string values ?

They are not strings - they are integers (I think), quite a different approach.
if using the node suggested by @Steve-Mcl - it should be built in?

if doing it manually however.

Properties with a length of 2;
Property.readInt16BE()

Properties with a length of 4;
Property.readInt32BE()

Properties with a length of 1;
as is: 0x08 = 8.

i.e Timestamp.readInt32BE()

I am not too hot on bits and bytes, a little maybe, but @Steve-Mcl may be more qualified

1 Like

Example:

Version.readInt61BE() returns 120

Which makes perfect sense!

But again, node-red-contrib-buffer-parser can do this for you

This data turns into this...

image

Using buffer parser (note: I had to guess some of the data types)

Demo flow...

[{"id":"e521a704f89dc9dc","type":"buffer-parser","z":"f14a3449b08c4e50","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"ascii","name":"DeviceID","offset":0,"length":6,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"Ver","offset":6,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"BAT","offset":8,"length":1,"offsetbit":0,"scale":"/1000","mask":""},{"type":"uint8","name":"Signal_Strength","offset":10,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint8","name":"MOD","offset":11,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"Temperature","offset":12,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint8","name":"DigitalIN","offset":14,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"ADC","offset":15,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1390,"y":120,"wires":[["bd1b9237bf3883cf"]]},{"id":"e6919926be416f92","type":"inject","z":"f14a3449b08c4e50","name":"buffer","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[114,64,49,96,112,105,0,120,11,213,8,1,0,0,0,0,11,0,188,1,236,97,102,185,61]","payloadType":"bin","x":1210,"y":120,"wires":[["e521a704f89dc9dc"]]},{"id":"bd1b9237bf3883cf","type":"debug","z":"f14a3449b08c4e50","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1390,"y":180,"wires":[]}]
1 Like

Thanks, guys for helping me.
Working with node-red for quite some time now, and there are still things to learn...

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