# Reading the udp data with a frame header

**URL:** <https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161>\
**Category:** General\
**Created:** [13 October 2021 11:27 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161 "2021-10-13T11:27:00Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [13 October 2021 11:27 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161/1 "2021-10-13T11:27:00Z")

</div>

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 🙄  
Here is the frame definition.

```auto
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]

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [13 October 2021 12:04 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161/2 "2021-10-13T12:04:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [13 October 2021 12:48 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161/3 "2021-10-13T12:48:41Z")

</div>

![Attachment_1634122839-1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f6c4d69ce6b56a67832732ec02ead2a98ab157cc.jpeg)

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

Here are some instructions i have for the sensor.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [13 October 2021 12:49 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161/4 "2021-10-13T12:49:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [13 October 2021 13:12 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161/5 "2021-10-13T13:12:43Z")

</div>

Ok,

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

```javascript
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`

---

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [13 October 2021 14:41 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161/6 "2021-10-13T14:41:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 October 2021 14:50 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161/7 "2021-10-13T14:50:57Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [14 October 2021 11:27 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161/8 "2021-10-14T11:27:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [14 October 2021 11:44 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161/9 "2021-10-14T11:44:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [14 October 2021 12:00 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161/10 "2021-10-14T12:00:08Z")

</div>

Example:

`Version.readInt61BE()` returns 120

Which makes perfect sense!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/20f5154cdfd3e23d53cbe857e77dd5a78ec8cb9f.jpeg)

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 October 2021 12:15 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161/11 "2021-10-14T12:15:02Z")

</div>

> [@lizzardguki](#):
>
> [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]

This data turns into this...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/2/f2ea82ad01c0c0179d5be3c6935226792c408ccd.png)

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fd26a3aa3abc5a8454276ab5fef44bed3385b181.png)

Demo flow...

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [14 October 2021 12:26 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161/12 "2021-10-14T12:26:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 October 2021 12:27 UTC](https://discourse.nodered.org/t/reading-the-udp-data-with-a-frame-header/52161/13 "2021-10-28T12:27:18Z")

</div>

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