# How can i parse my c structure data in node red

**URL:** <https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813>\
**Category:** Developing Nodes\
**Created:** [6 October 2020 05:51 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813 "2020-10-06T05:51:58Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![swapmicro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/swapmicro/32/29747_2.png) [@swapmicro](https://discourse.nodered.org/u/swapmicro)\
**Post date:** [6 October 2020 05:51 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/1 "2020-10-06T05:51:59Z")

</div>

Hello There,  
I have c structure  
typedef struct {  
float temp;  
float status;  
unsigned char tdata;  
} data;  
data mydata;  
value of my structure variable fill by the below parameters.  
mydata.tdata = 0x55;  
mydata.temp = 28.95;  
mydata.status = 0;

and the data store in memory is  
9A99E7410000000055000000  
in this data 9a99e741 is a float value of the temp variable,  
00000000 is the value of the status variable,  
55000000 is the value of the tdata variable which is an unsigned char

If I want to pars my data on the node-red side how can I pars this  
please guide me.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [6 October 2020 06:06 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/2 "2020-10-06T06:06:49Z")

</div>

How are you getting that into node red?

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [6 October 2020 06:40 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/3 "2020-10-06T06:40:37Z")

</div>

> [@swapmicro](#):
>
> If I want to pars my data on the node-red side how can I pars this  
> please guide me.

Have a look at the excellent node [node-red-contrib-buffer-parser (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-buffer-parser) (from @Steve-Mcl )

---

<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:** [6 October 2020 07:06 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/4 "2020-10-06T07:06:38Z")

</div>

As @janvda says, you can use the buffer-parser node...

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/67e859e11a2972c63c0a81d20b2f45ebf171936c.png)

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

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [8 October 2020 01:44 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/5 "2020-10-08T01:44:20Z")

</div>

Buffer method is best I can think of, it you know C, the buffer method should be straight forward.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 October 2020 08:34 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/6 "2020-10-08T08:34:59Z")

</div>

The reason I asked how it gets from C to node-red is that if it could be converted to JSON as it leaves the C s/w it would be much easier to pick up in node red. Also this would be more rugged a solution as if he s/w were moved to a different architecture or even C compiler version the details of the structure might change.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [8 October 2020 16:23 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/7 "2020-10-08T16:23:10Z")

</div>

True.

Typical data structures in C are type structures that are added to rather than changed to support backward compatibility, but that is not required nor consistently done. It is more of an acknowledged design methodology that many C programmers choose to adopt. The same is true of JSON file content, for example, lets say one day you decide to add a new node type that requires a new data set to function, the parent structure of the flows file might still just define a 'type' value that is new, but the child structure(s) maybe radically different from other node types.

My point, and I say this for those that may read this in the future, having been a C developer for many years if not decades, is that mapping C structures, as they may now exist, the buffer method is the straight forward, you have a one to one relationship per structure 'part' to buffer method defined elements.

We do this all the time, for example using C based DLLs in Windows, in a different high level language, say VB .NET, you have to rely on the static nature of structures to do the mapping, if such changes, your mapping has to adapt. To avoid this, we come back to always adding structures, not changing structures, to again, maintain backward compatibility.

This is why many of the Windows OS core API has routines named 'IamACall' and 'IamACallEx', of course Microsoft never explains this, but if you look at the difference between the two calls, the 'Ex' call is the newer routine, and has an extended or enhanced data structure different or larger than the original API call but clearly related to the original API call.

---

<div class="post-metadata">

**Author:** ![swapmicro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/swapmicro/32/29747_2.png) [@swapmicro](https://discourse.nodered.org/u/swapmicro)\
**Post date:** [10 October 2020 05:35 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/8 "2020-10-10T05:35:32Z")

</div>

Hello everyone,  
Thanks for the reply the solution is very useful but still, I am confused that how can I parse my data from my json payload,  
my payload is {"data":"0A003A0012504858553038200F4E6D380000E4410000000000000000000000000000000001000000","devaddr":"0190A703"}  
here I agave to parse my data through buffer parser and  
store my parse data in db with device address.  
please guide me ...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/96fc25745c76acf45d19e32597e16a84a13de4d2.png)  
here I am attached my flow file [flows (3).json](https://discourse.nodered.org/uploads/short-url/qG79nhiYMp3uyB1AlzJLzYVtjLq.json) (4.3 KB)

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [10 October 2020 13:16 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/9 "2020-10-10T13:16:10Z")

</div>

What is the C typedef structures? Can you show that here? If we know the typedef and structures declarations we should be able to breakup the 'data' value into understandable elements, and use the buffer item qualifications to match the C data structure information as noted above.

---

<div class="post-metadata">

**Author:** ![swapmicro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/swapmicro/32/29747_2.png) [@swapmicro](https://discourse.nodered.org/u/swapmicro)\
**Post date:** [10 October 2020 13:34 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/10 "2020-10-10T13:34:53Z")

</div>

Hello,  
My C structure is  
typedef struct {  
uint32\_t UID0;  
uint32\_t UID1;  
uint32\_t UID2;  
uint32\_t EpochTime;  
float Temprature;  
uint8\_t key[16];  
uint8\_t LockStatus;  
uint8\_t TempratureAlart;  
} Payload;  
and the paulod json which I got in nodered using mqtt is  
{"data":"0A003A001250485855303820D8236E380000E4410000000000000000000000000000000001000000","devaddr":"0190A703"}"

the data of 0A003A001250485855303820D8236E380000E4410000000000000000000000000000000001000000 is in c structure is  
uid: array[3]

0: 3801098

1: 1481134098

2: 540553301

epochTime: 946742232

Temprature: 28.5

Key: ""

LockStatus: 1

tempratureAlart: 0

but to store in db i also require devaddr value  
Thanks  
swapnil

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [10 October 2020 13:46 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/11 "2020-10-10T13:46:21Z")

</div>

So when you configure the buffer elements, you are basically parsing the 'data', you tried to setup a flow doing the buffer parsing right?

Adding the devaddr is easy once we have a JavaScript object that represents the 'data' values.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [10 October 2020 13:57 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/12 "2020-10-10T13:57:17Z")

</div>

I looks like you have your buffer parser setup, but I do have 2 questions, how is the temperature valve encoded and is the epoch value right?

epochTime: 946684803  
Temprature: 1105461248

Do these values above make sense? An epoch valve for me, for today, now... is much higher value. For example... 1602338222532.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [10 October 2020 14:19 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/13 "2020-10-10T14:19:44Z")

</div>

Ok, I am trying to figure out how to get the buffer-parser right. I have not used this node before, but I am familiar with C.

I found two errors in your buffer parser,

1. Change the input property from msg.payload to msg.payload.data. This will allow the parser to see the correct object property 'data' as needed.

2. The temperature type definition is wrong, it is not int32 (le). It should be "float (le)" not what it is now. This on my test flow, returned 28.5, which per the C output is correct for the 'data' value you used.

To add the devaddr to the output msg.payload, can be done a few ways. Below is a flow that shows one way to do it.

```auto
[{"id":"5db3fd69.1c77a4","type":"inject","z":"f9c8a450.82cf78","name":"OTTA 26002088","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"devaddr\":\"26002088\",\"data\":\"0A003A001250485855303820D8236E380000E4410000000000000000000000000000000001000000\"}","payloadType":"json","x":200,"y":120,"wires":[["2210c9e2.4b1c96"]]},{"id":"b4fd5c1b.c3776","type":"debug","z":"f9c8a450.82cf78","name":"Message (msg)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1080,"y":120,"wires":[]},{"id":"e279784e.6490f8","type":"change","z":"f9c8a450.82cf78","name":"Restore Device Address","rules":[{"t":"set","p":"payload.devaddr","pt":"msg","to":"devaddr","tot":"msg"},{"t":"delete","p":"devaddr","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":850,"y":120,"wires":[["b4fd5c1b.c3776"]]},{"id":"42813884.cf9f98","type":"buffer-parser","z":"f9c8a450.82cf78","name":"","data":"payload.data","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"uint32le","name":"uid0","offset":0,"length":1,"offsetbit":0,"scale":1,"mask":""},{"type":"uint32le","name":"uid1","offset":4,"length":1,"offsetbit":0,"scale":1,"mask":""},{"type":"int32le","name":"uid2","offset":8,"length":1,"offsetbit":0,"scale":1,"mask":""},{"type":"int32le","name":"epochTime","offset":12,"length":1,"offsetbit":0,"scale":1,"mask":""},{"type":"floatle","name":"Temprature","offset":16,"length":1,"offsetbit":0,"scale":1,"mask":""},{"type":"ascii","name":"Key","offset":20,"length":16,"offsetbit":0,"scale":1,"mask":""},{"type":"uint8","name":"LockStatus","offset":36,"length":1,"offsetbit":0,"scale":1,"mask":""},{"type":"uint8","name":"tempratureAlart","offset":37,"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,"setTopic":true,"x":630,"y":160,"wires":[["e279784e.6490f8"]]},{"id":"2210c9e2.4b1c96","type":"change","z":"f9c8a450.82cf78","name":"Save Device Address","rules":[{"t":"set","p":"devaddr","pt":"msg","to":"payload.devaddr","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":120,"wires":[["42813884.cf9f98"]]}]

```

The next step is what to do with the data? If you want send an alert via email or to your cell phone? you can learn about how do use the NR nodes that allow this...

> **[node-red-node-email](https://flows.nodered.org/node/node-red-node-email)**
>
> Node-RED nodes to send and receive simple emails

  

> **[node-red-node-twilio](https://flows.nodered.org/node/node-red-node-twilio)**
>
> A Node-RED node to send SMS messages via the Twilio service.

If you wanted to save it to a database? You could do something like...

```auto
"INSERT INTO <table> (uid0,uid1,uid2,epochTime,Temprature,Key,LockStatus,tempratureAlart,devaddr) VALUES (msg.payload.uid0,msg.payload.uid1,msg.payload.uid2,msg.payload.epochTime,msg.payload.Temprature,msg.payload.Key,LockStatus,msg.payload.tempratureAlart,msg.payload.devaddr);"

```

Where is the name of the table in your DB. The insert query may need some changes depending on which SQL server you are using. And assuming your name the table columns the same as the payload property names is done.

For more on SQL and NR...

> **[Database examples - insert and select using binding (flow) - Node-RED](https://flows.nodered.org/flow/2c48fc395772e22ecc64)**

---

<div class="post-metadata">

**Author:** ![swapmicro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/swapmicro/32/29747_2.png) [@swapmicro](https://discourse.nodered.org/u/swapmicro)\
**Post date:** [12 October 2020 10:13 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/14 "2020-10-12T10:13:57Z")

</div>

Hello There,  
Thanks for reply.  
i have new way to solve this, please guide me. it's valid method or not  
here is my flow  
[flows (4).json](https://discourse.nodered.org/uploads/short-url/2htdNl0xmuqT7cvcIKFzDDA7mx.json) (2.2 KB)  
Thanks  
swapnil

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [12 October 2020 15:53 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/15 "2020-10-12T15:53:27Z")

</div>

Looks fine to me, as long as it works the way you want, so be it. 🙂

---

<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:** [26 October 2020 15:53 UTC](https://discourse.nodered.org/t/how-can-i-parse-my-c-structure-data-in-node-red/33813/16 "2020-10-26T15:53:28Z")

</div>

Closed on 26 October 2020 15:53 UTC

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