# Creating Custom Message String To Send

**URL:** <https://discourse.nodered.org/t/creating-custom-message-string-to-send/82406>\
**Category:** Industrial\
**Created:** [27 October 2023 18:11 UTC](https://discourse.nodered.org/t/creating-custom-message-string-to-send/82406 "2023-10-27T18:11:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jts450](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jts450](https://discourse.nodered.org/u/jts450)\
**Post date:** [27 October 2023 18:11 UTC](https://discourse.nodered.org/t/creating-custom-message-string-to-send/82406/1 "2023-10-27T18:11:22Z")

</div>

I am trying to create a message to send to our WMS system and I received help with sending the needed ASCII codes in the message but I also need to insert 2 items in the Message. 1) a unique message number that always has 5 digits from 00001-99999 and starts back over when at 99999. 2) barcode scanner data.

I think with what I have so far here I just need create the new message number every time. I was testing a counter here but I need to add the leading zeros to create the new message number each time a message is sent. Any recommendations would be appreciated.

```auto
[
    {
        "id": "d19e83cbdada70ae",
        "type": "function",
        "z": "f2ee9bee06dc25c0",
        "name": "function 2",
        "func": "//Define Variable and get the stored flow value\n//if there is no stored value, then the value is 0\nvar counter = flow.get(\"counter\") || 0\n\n//add 1 to counter value\ncounter ++\n\n//save the counter value in the flow ariable\nflow.set(\"counter\", counter)\n\n//set msg.payload to counter value\nmsg.payload = counter\n\n//return the message object\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 480,
        "y": 1420,
        "wires": [
            [
                "b59579d87c20c7f8"
            ]
        ]
    },
    {
        "id": "5db1caeb25c9e401",
        "type": "inject",
        "z": "f2ee9bee06dc25c0",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 220,
        "y": 1420,
        "wires": [
            [
                "d19e83cbdada70ae"
            ]
        ]
    },
    {
        "id": "e2fab8f37a0a7ff8",
        "type": "change",
        "z": "f2ee9bee06dc25c0",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "counter",
                "pt": "flow",
                "to": "0",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 480,
        "y": 1480,
        "wires": [
            [
                "d19e83cbdada70ae"
            ]
        ]
    },
    {
        "id": "54991614fa8c41ef",
        "type": "inject",
        "z": "f2ee9bee06dc25c0",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 220,
        "y": 1480,
        "wires": [
            [
                "e2fab8f37a0a7ff8"
            ]
        ]
    },
    {
        "id": "243606a904fddfdb",
        "type": "comment",
        "z": "f2ee9bee06dc25c0",
        "name": "Counter Test",
        "info": "",
        "x": 450,
        "y": 1380,
        "wires": []
    },
    {
        "id": "b22a1435f31124e2",
        "type": "inject",
        "z": "f2ee9bee06dc25c0",
        "name": "",
        "props": [
            {
                "p": "header",
                "v": "HeaderTest",
                "vt": "str"
            },
            {
                "p": "number",
                "v": "00001",
                "vt": "str"
            },
            {
                "p": "barcode",
                "v": "412555",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 230,
        "y": 1320,
        "wires": [
            [
                "4a255b06578e2600"
            ]
        ]
    },
    {
        "id": "4a255b06578e2600",
        "type": "function",
        "z": "f2ee9bee06dc25c0",
        "name": "function 5",
        "func": "const SOH = String.fromCharCode(1);\nconst STX = String.fromCharCode(2);\nconst ETX = String.fromCharCode(3);\nmsg.payload = `${SOH}${msg.number}${STX}${msg.barcode}${ETX}`\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 460,
        "y": 1340,
        "wires": [
            [
                "43cd44e93314a0e8"
            ]
        ]
    },
    {
        "id": "43cd44e93314a0e8",
        "type": "debug",
        "z": "f2ee9bee06dc25c0",
        "name": "debug 25",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 640,
        "y": 1340,
        "wires": []
    },
    {
        "id": "774d2f308b84f49c",
        "type": "tcp in",
        "z": "f2ee9bee06dc25c0",
        "name": "Barcode Data",
        "server": "client",
        "host": "10.3.13.80",
        "port": "23",
        "datamode": "stream",
        "datatype": "utf8",
        "newline": "",
        "topic": "msg.barcode",
        "trim": false,
        "base64": false,
        "tls": "",
        "x": 210,
        "y": 1360,
        "wires": [
            [
                "4a255b06578e2600"
            ]
        ]
    }
]

```

---

<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:** [27 October 2023 18:29 UTC](https://discourse.nodered.org/t/creating-custom-message-string-to-send/82406/2 "2023-10-27T18:29:08Z")

</div>

Hi @jts450

Something like below should work in the `function` node after your incoming data?  
I am adding the sequence to a property of `sequence`

```auto
let sequenceNumber = flow.get("sequenceNumber") || 0
if(sequenceNumber >= 99999){
    sequenceNumber = 0
}
sequenceNumber++
flow.set("sequenceNumber",sequenceNumber)

const SOH = String.fromCharCode(1);
const STX = String.fromCharCode(2);
const ETX = String.fromCharCode(3);
const SequenceStr = (sequenceNumber).toString().padStart(5,'0')
msg.payload = `${SOH}${SequenceStr}${STX}${msg.barcode}${ETX}`

return msg;

```

**EDIT**  
Sorry - I can see you wanted it as part of the `payload` (updated)

---

<div class="post-metadata">

**Author:** ![jts450](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jts450](https://discourse.nodered.org/u/jts450)\
**Post date:** [27 October 2023 21:45 UTC](https://discourse.nodered.org/t/creating-custom-message-string-to-send/82406/3 "2023-10-27T21:45:25Z")

</div>

Thank you!! this is exactly what I was looking for

---

<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:** [10 November 2023 21:45 UTC](https://discourse.nodered.org/t/creating-custom-message-string-to-send/82406/4 "2023-11-10T21:45:38Z")

</div>

Closed on 10 November 2023 21:45 UTC

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