南柯醉一梦丶 发表于 2023-11-12 14:12:55

解析tcp out的buffer字符

为啥switch节点不能直接匹配十六进制的十进制数?是不是还需要转换成十进制才能包含匹配?

南柯醉一梦丶 发表于 2023-11-14 09:47:46

自己解决了,先装一个节点 node-red-contrib-binary将十六进制转换,再用函数节点转换拼接成字符串。

var screenID = msg.payload.screenid
var controlID = msg.payload.contid
var status = msg.payload.status

msg.payload=screenID.toString()+controlID.toString()+status.toString();
return msg;


节点信息[
    {
      "id": "b0592bb97cfa085a",
      "type": "tab",
      "label": "流程 1",
      "disabled": false,
      "info": "",
      "env": []
    },
    {
      "id": "819a6dc5.6771e",
      "type": "inject",
      "z": "b0592bb97cfa085a",
      "name": "",
      "props": [
            {
                "p": "payload",
                "v": "",
                "vt": "str"
            },
            {
                "p": "topic",
                "v": "",
                "vt": "str"
            }
      ],
      "repeat": "",
      "crontab": "",
      "once": false,
      "onceDelay": "",
      "topic": "",
      "payload": "",
      "payloadType": "str",
      "x": 430,
      "y": 400,
      "wires": [
            [
                "153983e4.bce93c"
            ]
      ]
    },
    {
      "id": "153983e4.bce93c",
      "type": "function",
      "z": "b0592bb97cfa085a",
      "name": "create test buffer",
      "func": "// @ts-ignore\nmsg.payload = new Buffer.from();\n//msg.payload = new Buffer.from();\nreturn msg;\n",
      "outputs": 1,
      "timeout": "",
      "noerr": 0,
      "initialize": "",
      "finalize": "",
      "libs": [],
      "x": 610,
      "y": 400,
      "wires": [
            [
                "939c9b2c819b21a3",
                "ebb6cf4e89cf1c60"
            ]
      ]
    },
    {
      "id": "2e685d10.e739a2",
      "type": "comment",
      "z": "b0592bb97cfa085a",
      "name": "parse buffer to msg properties",
      "info": "",
      "x": 500,
      "y": 360,
      "wires": []
    },
    {
      "id": "939c9b2c819b21a3",
      "type": "binary",
      "z": "b0592bb97cfa085a",
      "name": "转码",
      "property": "payload",
      "pattern": "b8 => zt,\nb16 => commid,\nb16 => screenid,\nb16 => contid,\nb8 => cont,\nb8 => boole,\nb8 => status,\nl32=> zw1\n",
      "x": 810,
      "y": 400,
      "wires": [
            [
                "1265f83d7f7e3554",
                "c13392e49e5fbaf6"
            ]
      ]
    },
    {
      "id": "fb2f1e45bf3fe89c",
      "type": "debug",
      "z": "b0592bb97cfa085a",
      "name": "debug",
      "active": true,
      "tosidebar": true,
      "console": false,
      "tostatus": false,
      "complete": "payload",
      "targetType": "msg",
      "statusVal": "",
      "statusType": "auto",
      "x": 1130,
      "y": 400,
      "wires": []
    },
    {
      "id": "1265f83d7f7e3554",
      "type": "function",
      "z": "b0592bb97cfa085a",
      "name": "分割字符",
      "func": "var screenID = msg.payload.screenid\nvar controlID = msg.payload.contid\nvar status = msg.payload.status\n\nmsg.payload=screenID.toString()+controlID.toString()+status.toString();\nreturn msg;\n\n// msg.payload = \n//   {\n//         //upcommd: upcommd,\n//         str: str,\n//         screenID: screenID,\n//         controlID: controlID,\n//         status: status\n//   };\n //return msg\n",
      "outputs": 1,
      "timeout": 0,
      "noerr": 0,
      "initialize": "",
      "finalize": "",
      "libs": [],
      "x": 980,
      "y": 400,
      "wires": [
            [
                "fb2f1e45bf3fe89c"
            ]
      ]
    },
    {
      "id": "31664a503303edb7",
      "type": "debug",
      "z": "b0592bb97cfa085a",
      "name": "resowed",
      "active": false,
      "tosidebar": true,
      "console": false,
      "tostatus": false,
      "complete": "payload",
      "targetType": "msg",
      "statusVal": "",
      "statusType": "auto",
      "x": 960,
      "y": 620,
      "wires": []
    },
    {
      "id": "ebb6cf4e89cf1c60",
      "type": "function",
      "z": "b0592bb97cfa085a",
      "name": "UDP解析",
      "func": "var header = (msg.payload);\nvar upcommd = msg.payload.slice(1,3);\nvar screenID = parseInt(msg.payload.slice(3,4),16);\nvar controlIDbuff = msg.payload.slice(5, 7);\nvar status = msg.payload.slice(9, 10);\n\nvar controlID = parseInt(controlIDbuff)\n//var battery = parseInt((msg.payload + msg.payload), 16);\n//var heartrate = parseInt((msg.payload + msg.payload), 16);\n//var temperature = (parseInt(msg.payload.slice(62,63), 16)) * 0.1 + 20;\n//msg.payload = screenID\n//return msg\nif (header == \"238\") \n{\n    msg.payload = \n      {\n            //upcommd: upcommd,\n            screenID: screenID,\n            controlID: controlID,\n            status: status\n      };\n      return msg\n}\n",
      "outputs": 1,
      "timeout": 0,
      "noerr": 0,
      "initialize": "",
      "finalize": "",
      "libs": [],
      "x": 800,
      "y": 620,
      "wires": [
            [
                "31664a503303edb7"
            ]
      ]
    },
    {
      "id": "c13392e49e5fbaf6",
      "type": "debug",
      "z": "b0592bb97cfa085a",
      "name": "",
      "active": true,
      "tosidebar": true,
      "console": false,
      "tostatus": false,
      "complete": "payload",
      "targetType": "msg",
      "statusVal": "",
      "statusType": "auto",
      "x": 1190,
      "y": 560,
      "wires": []
    }
]

页: [1]
查看完整版本: 解析tcp out的buffer字符