node-red 读写modbus设备数据
报警灯和蜂鸣器2个设备,分别是40001和40002两个地址,可读可写右侧switch按钮显示读取的状态,读取值为0时显示关闭状态,读取值为1时显示打开状态右侧switch按钮点击时,发送点击后的状态值到设备,打开发送数值1,关闭发送数值0。
功能界面
modbus功能码
功能简介
报警灯和蜂鸣器2个设备,分别是40001和40002两个地址,可读可写
右侧switch按钮显示读取的状态,读取值为0时显示关闭状态,读取值为1时显示打开状态
右侧switch按钮点击时,发送点击后的状态值到设备,打开发送数值1,关闭发送数值0
安装需要的插件
node-red程序
[
{
"id": "8405d0edc15b05a9",
"type": "group",
"z": "1bab7ad3e0da9ad6",
"style": {
"stroke": "#999999",
"stroke-opacity": "1",
"fill": "none",
"fill-opacity": "1",
"label": true,
"label-position": "nw",
"color": "#a4a4a4"
},
"nodes": [
"18e295b5608e1a1b",
"30957f259797c882"
],
"x": 28,
"y": 153,
"w": 1124,
"h": 374
},
{
"id": "18e295b5608e1a1b",
"type": "group",
"z": "1bab7ad3e0da9ad6",
"g": "8405d0edc15b05a9",
"style": {
"stroke": "#999999",
"stroke-opacity": "1",
"fill": "none",
"fill-opacity": "1",
"label": true,
"label-position": "nw",
"color": "#a4a4a4"
},
"nodes": [
"98f6198d936c943c",
"40f571290b2dd182",
"bd6a5b024c79109a",
"e00adb5ac3253026",
"58aa0d64638cf9db",
"3ed00487d123dd07",
"fd4ff84522cdd713",
"5fd80cbebf0c83fc",
"401c866fcad51fa5",
"80d5a0bf2b6ecdf8"
],
"x": 534,
"y": 179,
"w": 592,
"h": 322
},
{
"id": "98f6198d936c943c",
"type": "modbus-write",
"z": "1bab7ad3e0da9ad6",
"g": "18e295b5608e1a1b",
"name": "蜂鸣器",
"showStatusActivities": false,
"showErrors": false,
"showWarnings": true,
"unitid": "",
"dataType": "HoldingRegister",
"adr": "0",
"quantity": "1",
"server": "e71674d9be3cb703",
"emptyMsgOnFail": false,
"keepMsgProperties": false,
"delayOnStart": false,
"startDelayTime": "",
"x": 830,
"y": 240,
"wires": [
[
"58aa0d64638cf9db"
],
[
"3ed00487d123dd07"
]
]
},
{
"id": "40f571290b2dd182",
"type": "modbus-write",
"z": "1bab7ad3e0da9ad6",
"g": "18e295b5608e1a1b",
"name": "报警灯",
"showStatusActivities": false,
"showErrors": false,
"showWarnings": true,
"unitid": "",
"dataType": "HoldingRegister",
"adr": "1",
"quantity": "1",
"server": "e71674d9be3cb703",
"emptyMsgOnFail": false,
"keepMsgProperties": false,
"delayOnStart": false,
"startDelayTime": "",
"x": 830,
"y": 440,
"wires": [
[
"fd4ff84522cdd713"
],
[
"5fd80cbebf0c83fc"
]
]
},
{
"id": "bd6a5b024c79109a",
"type": "ui_switch",
"z": "1bab7ad3e0da9ad6",
"g": "18e295b5608e1a1b",
"name": "",
"label": "报警灯 #1",
"tooltip": "",
"group": "fcaf2ee10c2db92e",
"order": 0,
"width": 0,
"height": 0,
"passthru": false,
"decouple": "true",
"topic": "topic",
"topicType": "msg",
"style": "",
"onvalue": "1",
"onvalueType": "num",
"onicon": "",
"oncolor": "",
"offvalue": "0",
"offvalueType": "num",
"officon": "",
"offcolor": "",
"animate": false,
"className": "",
"x": 620,
"y": 380,
"wires": [
[
"40f571290b2dd182",
"80d5a0bf2b6ecdf8"
]
]
},
{
"id": "e00adb5ac3253026",
"type": "ui_switch",
"z": "1bab7ad3e0da9ad6",
"g": "18e295b5608e1a1b",
"name": "",
"label": "蜂鸣器 #0",
"tooltip": "",
"group": "fcaf2ee10c2db92e",
"order": 0,
"width": 0,
"height": 0,
"passthru": false,
"decouple": "true",
"topic": "topic",
"topicType": "msg",
"style": "",
"onvalue": "1",
"onvalueType": "num",
"onicon": "",
"oncolor": "",
"offvalue": "0",
"offvalueType": "num",
"officon": "",
"offcolor": "",
"animate": false,
"className": "",
"x": 620,
"y": 300,
"wires": [
[
"98f6198d936c943c",
"401c866fcad51fa5"
]
]
},
{
"id": "58aa0d64638cf9db",
"type": "debug",
"z": "1bab7ad3e0da9ad6",
"g": "18e295b5608e1a1b",
"name": "debug 13",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 1020,
"y": 220,
"wires": []
},
{
"id": "3ed00487d123dd07",
"type": "debug",
"z": "1bab7ad3e0da9ad6",
"g": "18e295b5608e1a1b",
"name": "debug 14",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 1020,
"y": 260,
"wires": []
},
{
"id": "fd4ff84522cdd713",
"type": "debug",
"z": "1bab7ad3e0da9ad6",
"g": "18e295b5608e1a1b",
"name": "debug 15",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 1020,
"y": 420,
"wires": []
},
{
"id": "5fd80cbebf0c83fc",
"type": "debug",
"z": "1bab7ad3e0da9ad6",
"g": "18e295b5608e1a1b",
"name": "debug 16",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 1020,
"y": 460,
"wires": []
},
{
"id": "401c866fcad51fa5",
"type": "debug",
"z": "1bab7ad3e0da9ad6",
"g": "18e295b5608e1a1b",
"name": "read #0",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 840,
"y": 300,
"wires": []
},
{
"id": "80d5a0bf2b6ecdf8",
"type": "debug",
"z": "1bab7ad3e0da9ad6",
"g": "18e295b5608e1a1b",
"name": "read #1",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 840,
"y": 380,
"wires": []
},
{
"id": "e71674d9be3cb703",
"type": "modbus-client",
"name": "",
"clienttype": "tcp",
"bufferCommands": true,
"stateLogEnabled": false,
"queueLogEnabled": false,
"failureLogEnabled": true,
"tcpHost": "192.168.1.21",
"tcpPort": "502",
"tcpType": "DEFAULT",
"serialPort": "/dev/ttyUSB",
"serialType": "RTU-BUFFERD",
"serialBaudrate": "9600",
"serialDatabits": "8",
"serialStopbits": "1",
"serialParity": "none",
"serialConnectionDelay": "100",
"serialAsciiResponseStartDelimiter": "0x3A",
"unit_id": "1",
"commandDelay": "1",
"clientTimeout": "1000",
"reconnectOnTimeout": true,
"reconnectTimeout": "2000",
"parallelUnitIdsAllowed": true,
"showErrors": false,
"showWarnings": true,
"showLogs": true
},
{
"id": "fcaf2ee10c2db92e",
"type": "ui_group",
"name": "PLC 21",
"tab": "cc160c7a8f81818f",
"order": 1,
"disp": true,
"width": "6",
"collapse": false,
"disabled": false,
"hidden": false,
"className": ""
},
{
"id": "cc160c7a8f81818f",
"type": "ui_tab",
"name": "PLC",
"icon": "dashboard",
"disabled": false,
"hidden": false
},
{
"id": "30957f259797c882",
"type": "group",
"z": "1bab7ad3e0da9ad6",
"g": "8405d0edc15b05a9",
"style": {
"stroke": "#999999",
"stroke-opacity": "1",
"fill": "none",
"fill-opacity": "1",
"label": true,
"label-position": "nw",
"color": "#a4a4a4"
},
"nodes": [
"f075472e9ce98153",
"5368f89892281138",
"8fd750ac42382765",
"1319edc67a2a1b7d"
],
"x": 54,
"y": 259,
"w": 432,
"h": 242
},
{
"id": "f075472e9ce98153",
"type": "modbus-read",
"z": "1bab7ad3e0da9ad6",
"g": "30957f259797c882",
"name": "",
"topic": "",
"showStatusActivities": false,
"logIOActivities": false,
"showErrors": false,
"showWarnings": true,
"unitid": "",
"dataType": "HoldingRegister",
"adr": "0",
"quantity": "2",
"rate": "3",
"rateUnit": "s",
"delayOnStart": false,
"startDelayTime": "",
"server": "e71674d9be3cb703",
"useIOFile": false,
"ioFile": "",
"useIOForPayload": false,
"emptyMsgOnFail": false,
"x": 170,
"y": 380,
"wires": [
[
"1319edc67a2a1b7d",
"8fd750ac42382765"
],
[
"5368f89892281138"
]
]
},
{
"id": "5368f89892281138",
"type": "debug",
"z": "1bab7ad3e0da9ad6",
"g": "30957f259797c882",
"name": "debug 6",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 380,
"y": 460,
"wires": []
},
{
"id": "8fd750ac42382765",
"type": "function",
"z": "1bab7ad3e0da9ad6",
"g": "30957f259797c882",
"name": "payload[0]",
"func": "msg.payload = msg.payload[0];\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 390,
"y": 300,
"wires": [
[
"e00adb5ac3253026"
]
]
},
{
"id": "1319edc67a2a1b7d",
"type": "function",
"z": "1bab7ad3e0da9ad6",
"g": "30957f259797c882",
"name": "payload[1]",
"func": "msg.payload = msg.payload[1];\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 390,
"y": 380,
"wires": [
[
"bd6a5b024c79109a"
]
]
}
]
程序讲解
Modbus-Read 节点
Modbus-Read 节点 的 两个出口
两个输出口输出的内容差不多,第一个将读取的内容封装在payload中输出,第二个将读取的内容封装在values中输出
Output 2 的 输出内容
使用 Modbus-Read 节点 读取的数据 控制switch按钮的开关状态
获取数组中的对应值给下游switch按钮
使用上游给的数据切换switch按钮状态
这里一定要取消 Pass through msg
if payload matches valid state: 的勾选状态,
然后选择 Indicator 下拉列表中的 Switch icon shows state of the input 选项
Pass through msg
if payload matches valid state: 勾选表示不管是入口有数据过来还是点击按钮触发出口有数据,都会切换switch按钮状态
Pass through msg
if payload matches valid state: 取消勾选后,会出来一个Indicator下拉选项,
选项 Switch icon shows state of the input 表示仅仅在节点入口有数据过来时改变switch按钮状态,并且在入口有数据过来时不会触发点击事件,也就是不会触发出口连接的节点
另一个选项 Switch icon shows state of the output ,表示仅仅在switch按钮触发点击事件时切换switch按钮状态
这里选择 Switch icon shows state of the input ,是因为设备的状态(switch按钮的状态)要显示读取数据的状态,而非点击按钮后直接切换switch按钮的状态,点击switch按钮后仅仅是触发Modbus Write节点写数据到设备,等设备数据被修改后,Modbus Read节点读取到切换后的数值才会去改变switch按钮的状态,这样switch按钮显示的才是真实的设备状态
还有一个很重要的原因,选择 Switch icon shows state of the input 模式,入口有数据过来后,不会触发switch按钮的点击事件,这就意味着不会在每次读数据后都触发一次Modbus Write节点;想像一下,如果每次读数据过来,都触发一次写数据,是不是很浪费性能,所以这个选项很关键,切记!!!
switch按钮点击时 触发Modbus Write节点写数据到设备
点击switch按钮切换状态时,分别会发送 1(开启)、0(关闭)两个值到出口
完结

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)