『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 1369|回复: 1

[流程系列] 解析tcp out的buffer字符

[复制链接]

39

主题

749

帖子

4841

积分

论坛元老

Freddy.

Rank: 8Rank: 8

积分
4841
金钱
4067
HASS币
0

最佳新人卓越贡献

发表于 2023-11-12 14:12:55 | 显示全部楼层 |阅读模式
为啥switch节点不能直接匹配十六进制的十进制数?是不是还需要转换成十进制才能包含匹配?
red.png
回复

使用道具 举报

39

主题

749

帖子

4841

积分

论坛元老

Freddy.

Rank: 8Rank: 8

积分
4841
金钱
4067
HASS币
0

最佳新人卓越贡献

 楼主| 发表于 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([238,177,17,0,0,4,4,16,1,0,255,252,255,255]);\n//msg.payload = new Buffer.from([04,04,04,02]);\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[0]);\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[48] + msg.payload[49]), 16);\n//var heartrate = parseInt((msg.payload[54] + msg.payload[55]), 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": []
    }
]


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-12-22 22:39 , Processed in 0.053130 second(s), 27 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表