Creating Custom Message String To Send

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.

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

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

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)

1 Like

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

1 Like

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