# Point me in the right direction to Parse TCP string

**URL:** <https://discourse.nodered.org/t/point-me-in-the-right-direction-to-parse-tcp-string/41375>\
**Category:** General\
**Created:** [21 February 2021 14:00 UTC](https://discourse.nodered.org/t/point-me-in-the-right-direction-to-parse-tcp-string/41375 "2021-02-21T14:00:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vonksmurf](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@Vonksmurf](https://discourse.nodered.org/u/Vonksmurf)\
**Post date:** [21 February 2021 14:00 UTC](https://discourse.nodered.org/t/point-me-in-the-right-direction-to-parse-tcp-string/41375/1 "2021-02-21T14:00:18Z")

</div>

I try to parse the string below .  
I think it is possible with the contrib-buffer-parser node but i don`t understand the working of the node completly

If header 1 is 100 then there is a error

```auto
[1,49,53,2,48,49,31,49,48,48,30,48,50,31,69,82,82,79,82,32,32,32,32,32,32,32,30,48,51,31,83,69,82,86,73,67,69,30,48,52,31,49,30,48,53,31,30,48,54,31,49,50,51,52,53,30,48,55,31,65,70,68,46,49,66,30,48,56,31,56,30,3]

```

```auto
150110002ERROR 03SERVICE04105061234507AFD.1B088

```

If header 1 is 101 then the error is fixed

```auto
[1,49,53,2,48,49,31,49,48,49,30,48,50,31,78,79,32,69,82,82,79,82,32,32,32,32,30,48,51,31,83,69,82,86,73,67,69,30,48,52,31,49,30,48,53,31,30,48,54,31,49,50,51,52,53,30,48,55,31,65,70,68,46,49,66,30,48,56,31,57,30,3]

```

```auto
150110102NO ERROR 03SERVICE04105061234507AFD.1B089

```

I want to make the error message (header index 1 100) visible in the UI table,  
and let it disappear (header index 1 101) when the fault is resolved.  
I got that part working, but when parsing the string I'm stuck  
Maybe someone can help me in the right direction

regards mark

---

<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:** [21 February 2021 15:30 UTC](https://discourse.nodered.org/t/point-me-in-the-right-direction-to-parse-tcp-string/41375/2 "2021-02-21T15:30:45Z")

</div>

This is not suited to buffer parser as I suspect data sizes are not fixed.

there is a protocol if you look closely at the data...

everything is key/val pairs with markers between them.

after STX (2) starts a KEY then US (unit separator 31) denotes end of KEY / start of VALUE  
Following the VALUS is an RS (record separator 30) that marks the end of the VALUE.

This is a case where a function node is best suited...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cbe9dd9ce5a16b5d0e275296680551f03231ea3e.png)

```auto
//character markers to look for
const STX = 2;
const ETX = 3;
const RS = 31; //RS (record separator)
const US = 30; //US (unit separator)

//state machine modes
const MODE_INIT = 0;
const MODE_KEY = 1;
const MODE_VAL = 2;

//data to parse
const data = msg.payload;

//variables
var result = {};
var mode = MODE_INIT;
var key = [];
var val = [];

//helper function for converting KEY and VAL to strings
//and store in the result object
function store(k,v) {
    if (!k || !k.length) return;
    var skey = Buffer.from(k).toString().trim();
    var sval = Buffer.from(v).toString().trim();
    result[skey] = sval;
}

//loop the buffers data and extract the parts 
for(let i = 0; i < data.length; i++){
    const c = data[i];
    if(c === STX) {
        mode = MODE_KEY;
    } else if(c === RS) {
        mode = MODE_VAL;
    } else if(c === US || c === ETX) {
        store(key,val);
        key = [];
        val = []; 
        mode = MODE_KEY;
    } else {
        if (mode === MODE_KEY) key.push(c);
        if (mode === MODE_VAL) val.push(c);
    }
}

//put results into payload and return it to next node.
msg.payload = result;
return msg;

```

---

<div class="post-metadata">

**Author:** ![Vonksmurf](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@Vonksmurf](https://discourse.nodered.org/u/Vonksmurf)\
**Post date:** [21 February 2021 15:43 UTC](https://discourse.nodered.org/t/point-me-in-the-right-direction-to-parse-tcp-string/41375/3 "2021-02-21T15:43:13Z")

</div>

Hi Steve,  
thank you very much for the solution.  
I would never have solved this myself 👌

regards mark

---

<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:** [7 March 2021 15:43 UTC](https://discourse.nodered.org/t/point-me-in-the-right-direction-to-parse-tcp-string/41375/4 "2021-03-07T15:43:51Z")

</div>

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