Node-RED中建立TCP服务端和客户端

生活中,最使人疲惫的往往不是道路的遥远,而是心中的郁闷;最使人痛苦的往往不是生活的不幸,而是希望的破灭;最使人颓废的往往不是前途的坎坷,而是自信的丧失;最使人绝望的往往不是挫折的打击,而是心灵的死亡。所以我们要有自己的梦想,让梦想的星光指引着我们走出落漠,走出惆怅,带着我们走进自己的理想。

导读:本篇文章讲解 Node-RED中建立TCP服务端和客户端,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

场景

Node-RED简介与Windows上安装、启动和运行示例:

Node-RED简介与Windows上安装、启动和运行示例_霸道流氓气质的博客-CSDN博客_nodered windows

Node-RED怎样导出导入流程为json文件:

Node-RED怎样导出导入流程为json文件_霸道流氓气质的博客-CSDN博客_怎么导出json文件

Node-RED中可以使用tcp in 和 tcp out节点来建立TCP服务端以及TCP客户端。

注:

博客:
霸道流氓气质的博客_CSDN博客-C#,架构之路,SpringBoot领域博主
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

1、建立服务端

Node-RED中建立TCP服务端和客户端

编辑tcp in节点,类型中选择监听,端口输入1025

输出选择字符流的字符串,分割符号是\m

Node-RED中建立TCP服务端和客户端

2、switch节点,新增两条规则,分别判断msg.payload属性值是temp还是humi

Node-RED中建立TCP服务端和客户端

3、两个change节点,指定msg.payload属性值分别为20和70的模拟值。

Node-RED中建立TCP服务端和客户端

4、template节点,在msg.payload属性值的信息后面加上\n

Node-RED中建立TCP服务端和客户端

5、tcp out节点,在类型选择回应TCP

Node-RED中建立TCP服务端和客户端

6、TCP客户端布局

Node-RED中建立TCP服务端和客户端

7、tcp in节点,在类型选择连接,端口输入1025,输出指定字串流的字符串,分割符号\n

Node-RED中建立TCP服务端和客户端

8、function节点,目的是中断流程,避免之后的tcp out节点再输出msg.payload属性值,删除function节点的return msg代码

Node-RED中建立TCP服务端和客户端

9、template节点,修改为{{payload}}输出值,别忘了按Enter回车键,才能在字符串后面加上\n

Node-RED中建立TCP服务端和客户端

10、json数据

[
    {
        "id": "f4a4d8eab7935bc8",
        "type": "tab",
        "label": "流程 3",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "c428312d1f6165d1",
        "type": "mqtt-broker",
        "name": "mqtt",
        "broker": "127.0.0.1",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "sessionExpiry": ""
    },
    {
        "id": "51eee50b4ec9422f",
        "type": "ui_group",
        "name": "dashboardDemo",
        "tab": "29ae4c620f43ee0d",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "29ae4c620f43ee0d",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    },
    {
        "id": "a506c767a5c1edbd",
        "type": "ui_base",
        "theme": {
            "name": "theme-light",
            "lightTheme": {
                "default": "#0094CE",
                "baseColor": "#0094CE",
                "baseFont": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif",
                "edited": true,
                "reset": false
            },
            "darkTheme": {
                "default": "#097479",
                "baseColor": "#097479",
                "baseFont": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif",
                "edited": false
            },
            "customTheme": {
                "name": "Untitled Theme 1",
                "default": "#4B7930",
                "baseColor": "#4B7930",
                "baseFont": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"
            },
            "themeState": {
                "base-color": {
                    "default": "#0094CE",
                    "value": "#0094CE",
                    "edited": false
                },
                "page-titlebar-backgroundColor": {
                    "value": "#0094CE",
                    "edited": false
                },
                "page-backgroundColor": {
                    "value": "#fafafa",
                    "edited": false
                },
                "page-sidebar-backgroundColor": {
                    "value": "#ffffff",
                    "edited": false
                },
                "group-textColor": {
                    "value": "#1bbfff",
                    "edited": false
                },
                "group-borderColor": {
                    "value": "#ffffff",
                    "edited": false
                },
                "group-backgroundColor": {
                    "value": "#ffffff",
                    "edited": false
                },
                "widget-textColor": {
                    "value": "#111111",
                    "edited": false
                },
                "widget-backgroundColor": {
                    "value": "#0094ce",
                    "edited": false
                },
                "widget-borderColor": {
                    "value": "#ffffff",
                    "edited": false
                },
                "base-font": {
                    "value": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"
                }
            },
            "angularTheme": {
                "primary": "indigo",
                "accents": "blue",
                "warn": "red",
                "background": "grey",
                "palette": "light"
            }
        },
        "site": {
            "name": "Node-RED Dashboard",
            "hideToolbar": "false",
            "allowSwipe": "false",
            "lockMenu": "false",
            "allowTempTheme": "true",
            "dateFormat": "DD/MM/YYYY",
            "sizes": {
                "sx": 48,
                "sy": 48,
                "gx": 6,
                "gy": 6,
                "cx": 6,
                "cy": 6,
                "px": 0,
                "py": 0
            }
        }
    },
    {
        "id": "248cbbb0.18e794",
        "type": "ui_group",
        "name": "MyGroup",
        "tab": "3f79c420.cfc1bc",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "3f79c420.cfc1bc",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    },
    {
        "id": "a0954be5.a7f7e8",
        "type": "ui_group",
        "name": "MyInput",
        "tab": "3f79c420.cfc1bc",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "508bd6f8398ab80f",
        "type": "ui_group",
        "name": "OpenWeatherMap",
        "tab": "3f79c420.cfc1bc",
        "order": 3,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "4843765b.1af508",
        "type": "tcp in",
        "z": "f4a4d8eab7935bc8",
        "name": "TCP客戶端:1025",
        "server": "client",
        "host": "127.0.0.1",
        "port": "1025",
        "datamode": "stream",
        "datatype": "utf8",
        "newline": "\\n",
        "topic": "",
        "base64": false,
        "x": 240,
        "y": 380,
        "wires": [
            [
                "a4f8ed1e.c28a3",
                "884f79cc.ff01d8"
            ]
        ]
    },
    {
        "id": "a4f8ed1e.c28a3",
        "type": "debug",
        "z": "f4a4d8eab7935bc8",
        "name": "tcp client",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 420,
        "y": 440,
        "wires": []
    },
    {
        "id": "27457a99.3109b6",
        "type": "tcp out",
        "z": "f4a4d8eab7935bc8",
        "host": "",
        "port": "",
        "beserver": "reply",
        "base64": false,
        "end": false,
        "name": "tcp out : 送出信息",
        "x": 650,
        "y": 380,
        "wires": []
    },
    {
        "id": "1a88d1d5.c129ae",
        "type": "inject",
        "z": "f4a4d8eab7935bc8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "temp",
        "payloadType": "str",
        "x": 410,
        "y": 340,
        "wires": [
            [
                "f1c7c3d.6a92c4"
            ]
        ]
    },
    {
        "id": "884f79cc.ff01d8",
        "type": "function",
        "z": "f4a4d8eab7935bc8",
        "name": "no msg back",
        "func": "",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 430,
        "y": 380,
        "wires": [
            [
                "27457a99.3109b6"
            ]
        ]
    },
    {
        "id": "d0ed337.b37dbd",
        "type": "tcp in",
        "z": "f4a4d8eab7935bc8",
        "name": "TCP服务端:1025",
        "server": "server",
        "host": "",
        "port": "1025",
        "datamode": "stream",
        "datatype": "utf8",
        "newline": "\\n",
        "topic": "",
        "base64": false,
        "x": 220,
        "y": 100,
        "wires": [
            [
                "a8ca6d2c.2926c",
                "c3baaed4.79a65"
            ]
        ]
    },
    {
        "id": "c4fce4d0.df1838",
        "type": "tcp out",
        "z": "f4a4d8eab7935bc8",
        "host": "",
        "port": "",
        "beserver": "reply",
        "base64": false,
        "end": false,
        "name": "tcp out: 发送信息",
        "x": 770,
        "y": 160,
        "wires": []
    },
    {
        "id": "a6a1d927.3f5878",
        "type": "inject",
        "z": "f4a4d8eab7935bc8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": "",
        "topic": "",
        "payload": "Hello from server.",
        "payloadType": "str",
        "x": 560,
        "y": 40,
        "wires": [
            [
                "df1d0ca7.57683"
            ]
        ]
    },
    {
        "id": "a8ca6d2c.2926c",
        "type": "debug",
        "z": "f4a4d8eab7935bc8",
        "name": "tcp server",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 400,
        "y": 160,
        "wires": []
    },
    {
        "id": "c3baaed4.79a65",
        "type": "switch",
        "z": "f4a4d8eab7935bc8",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "temp",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "humi",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 390,
        "y": 100,
        "wires": [
            [
                "9986631d.c94df"
            ],
            [
                "862b30b1.af802"
            ]
        ]
    },
    {
        "id": "9986631d.c94df",
        "type": "change",
        "z": "f4a4d8eab7935bc8",
        "name": "输出: 20",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "20",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 540,
        "y": 80,
        "wires": [
            [
                "df1d0ca7.57683"
            ]
        ]
    },
    {
        "id": "f1c7c3d.6a92c4",
        "type": "template",
        "z": "f4a4d8eab7935bc8",
        "name": "+ 換行",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "{{payload}}\n",
        "output": "str",
        "x": 550,
        "y": 320,
        "wires": [
            [
                "27457a99.3109b6"
            ]
        ]
    },
    {
        "id": "2ddba953.d7ae06",
        "type": "inject",
        "z": "f4a4d8eab7935bc8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "humi",
        "payloadType": "str",
        "x": 410,
        "y": 300,
        "wires": [
            [
                "f1c7c3d.6a92c4"
            ]
        ]
    },
    {
        "id": "862b30b1.af802",
        "type": "change",
        "z": "f4a4d8eab7935bc8",
        "name": "输出: 70",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "70",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 540,
        "y": 120,
        "wires": [
            [
                "df1d0ca7.57683"
            ]
        ]
    },
    {
        "id": "df1d0ca7.57683",
        "type": "template",
        "z": "f4a4d8eab7935bc8",
        "name": "+换行",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "{{payload}}\n",
        "output": "str",
        "x": 690,
        "y": 100,
        "wires": [
            [
                "c4fce4d0.df1838"
            ]
        ]
    }
]

11、效果

Node-RED中建立TCP服务端和客户端

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/135993.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!