# String to response code

**URL:** <https://discourse.nodered.org/t/string-to-response-code/63470>\
**Category:** General\
**Tags:** function-node\
**Created:** [5 June 2022 00:45 UTC](https://discourse.nodered.org/t/string-to-response-code/63470 "2022-06-05T00:45:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmac101](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dmac101](https://discourse.nodered.org/u/dmac101)\
**Post date:** [5 June 2022 00:45 UTC](https://discourse.nodered.org/t/string-to-response-code/63470/1 "2022-06-05T00:45:55Z")

</div>

Hi All,  
I am working on a bit of a side project where I am trying to monitor the status of a radio, with the final goal of using it to log signal level against GPS location. The uses a proprietary serial protocol that sends and receives messages as basic text. The format of this is generally [ID][Size][Parameters][checksum] where ID is a single ASCII character to categorize the message, Size is the number of characters in the parameters field and is expressed in ASCII hex notation (2 characters), The parameters field is optional and depends on the type of message, checksum is 8 bit checksum of the ID, Size & Parameters fields.  
I am thinking that i will need to detect the first character (ID) and then have some function to break up the remaining text and create an output message.  
some examples of message id's are: e = error, p=progress report, v=version numbers, etc.  
for instance, if i receive p0205C9 it is a progress report, 2 characters in the parameters field, 05 = receiver busy.  
I am not sure how the best way is to split these messages up so that i can match it with the list of different functions/parameters.  
Are there specific nodes that would be suited or should i do it all in a single function node? I am a complete novice with writing functions but can generally learn quickly from examples.

Any help would be greatly appreciated

---

<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:** [5 June 2022 01:08 UTC](https://discourse.nodered.org/t/string-to-response-code/63470/2 "2022-06-05T01:08:38Z")

</div>

You can use buffer parser to extract the first character then route the message (using a switch node) to another appropriately set up buffer parser for that particular ID to parse out the remaining elements.

Search the forum for "buffer parser" & you will see write a few examples. Also, read the readme & built in help.

Give it a go, see where you get to.

If you don't succeed, post several examples of data & annotate then with what they should be parsed to & I'll show you how.

---

<div class="post-metadata">

**Author:** ![dmac101](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dmac101](https://discourse.nodered.org/u/dmac101)\
**Post date:** [5 June 2022 01:56 UTC](https://discourse.nodered.org/t/string-to-response-code/63470/3 "2022-06-05T01:56:19Z")

</div>

Thanks for the feedback, i will see how i go and let you know

---

<div class="post-metadata">

**Author:** ![dmac101](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dmac101](https://discourse.nodered.org/u/dmac101)\
**Post date:** [5 June 2022 03:05 UTC](https://discourse.nodered.org/t/string-to-response-code/63470/4 "2022-06-05T03:05:58Z")

</div>

The buffer parser looks like it would be ideal for this.  
I am having some issues in getting data out.

I have made a simple demo with a copy of the data string in an inject node.  
It looks like the payload is empty

```auto
[{"id":"b235016f1d0add12","type":"buffer-parser","z":"687fd45598d0e446","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"string","name":"ID","offset":0,"length":2,"offsetbit":0,"scale":"1","mask":""},{"type":"string","name":"Size","offset":2,"length":2,"offsetbit":0,"scale":"1","mask":""},{"type":"string","name":"Data","offset":4,"length":6,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"return","multipleResult":true,"fanOutMultipleResult":false,"setTopic":false,"outputs":1,"x":870,"y":340,"wires":[["99e8056ff941dba4"]]},{"id":"99e8056ff941dba4","type":"debug","z":"687fd45598d0e446","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1110,"y":340,"wires":[]},{"id":"fe3d112337a2d0f1","type":"inject","z":"687fd45598d0e446","name":"Eg Data","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"..m0853103.069B","payloadType":"str","x":540,"y":340,"wires":[["b235016f1d0add12"]]}]

```

---

<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:** [5 June 2022 07:44 UTC](https://discourse.nodered.org/t/string-to-response-code/63470/5 "2022-06-05T07:44:54Z")

</div>

Where are the example data strings and the descriptions to help me break them down for you?

> [@Steve-Mcl](#):
>
> If you don't succeed, post several examples of data & annotate them with what they should be parsed to & I'll show you how.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [5 June 2022 09:40 UTC](https://discourse.nodered.org/t/string-to-response-code/63470/6 "2022-06-05T09:40:32Z")

</div>

> [@dmac101](#):
>
> p0205C9

The flow you posted above seems to have spurious ``` characters at the end, causing an error when I try and import it.

In the first post you gave an example of the data `p0205C9` but in the flow you inject `..m0853103.069B`.

---

<div class="post-metadata">

**Author:** ![dmac101](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dmac101](https://discourse.nodered.org/u/dmac101)\
**Post date:** [5 June 2022 22:00 UTC](https://discourse.nodered.org/t/string-to-response-code/63470/7 "2022-06-05T22:00:02Z")

</div>

Im am not sure why there is an issue importing the flow, I reimported from the above post without any issues.  
The 2 separate examples follow the pattern [ID][Size][Parameters][checksum]:  
`p0205C9`  
ID= **p** rogress report message  
Size= **02** Characters in Parameters field  
Parameters= **05** =Receiver busy  
Checksum= **C9**

..m0853103.069B  
. are received when message is in response to command  
ID= **m** odel  
Size= **08** Characters in Parameters field  
Parameters= **53103.06** =P25 Digital Mobile Phase 2-capable,TM9400(531), v3.06 software (3.06)  
Checksum= **9B**

---

<div class="post-metadata">

**Author:** ![dmac101](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dmac101](https://discourse.nodered.org/u/dmac101)\
**Post date:** [7 June 2022 22:10 UTC](https://discourse.nodered.org/t/string-to-response-code/63470/8 "2022-06-07T22:10:29Z")

</div>

I have managed to get this working using a combination of a switch node and function nodes. For some reasonI just could not get the buffer parser node to work. The switch node is looking for an occurrence of one of the ID's, ie .p or .m, .j, etc. each of the different ID's is switched to its own output and fed into a function node. In the function node I am using indexOf to locate the ID (some strings have a single . preceding the ID letter and some have multiple) I am then using substr to break down the string into its components.

Here is an example of one of the function nodes

```auto
var ID = msg.payload.indexOf(".j")
var size = msg.payload.substr(ID+2,2)
var cctmcmd = msg.payload.substr(ID+4,3)
msg.size=size
msg.cctmcmd=cctmcmd
if (cctmcmd=="064") {
    var rssi=Number(msg.payload.substr(ID+7,size-3)/10)
    msg.rssi=rssi
}

return msg;

```

---

<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:** [6 August 2022 22:11 UTC](https://discourse.nodered.org/t/string-to-response-code/63470/9 "2022-08-06T22:11:16Z")

</div>

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