参考:https://bbs.iobroker.cn/t/topic/1657
硬件需求:
1.ddsu666 智能电表已经配置为modbus通讯模式
2.485转TTL模块+USB串口
软件需求:
nodered
Docker安装nodered
修改/dev/ttyUSB0权限 sudo chmod 777 /dev/ttyUSB0
sudo apt install build-essential git curl
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
docker run -it -p 1880:1880 --device=/dev/ttyUSB0 --name mynodered nodered/node-red
nodered 安装 node-red-contrib-modbus节点
http://127.0.0.1:1880 访问nodered 节点管理->安装 node-red-contrib-modbus节点
nodered配置如下
[
{
"id": "6238296b06f69a38",
"type": "tab",
"label": "Flow 1",
"disabled": false,
"info": ""
},
{
"id": "27d49c8d9d77de90",
"type": "debug",
"z": "6238296b06f69a38",
"name": "",
"active": true,
"tosidebar": true,
"console": true,
"tostatus": true,
"complete": "true",
"targetType": "full",
"statusVal": "payload",
"statusType": "auto",
"x": 1310,
"y": 260,
"wires": []
},
{
"id": "8bbf1d74c4d8a84d",
"type": "function",
"z": "6238296b06f69a38",
"name": "瞬时电压",
"func": "var b = parseInt(msg.payload[0].toString(16)+msg.payload[1].toString(16),16);\nvar s = b&0x80000000/0x80000000;\nvar e = (b&0x7f800000)/0x800000-127;\nvar c = (b&0x7fffff)/0x800000;\nvar r = Math.pow(-1,s)*(1+c)*Math.pow(2,e);\nif(r<0) return null;\nelse msg.payload = r;\nmsg.topic= '瞬时电压';\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1000,
"y": 160,
"wires": [
[
"27d49c8d9d77de90"
]
],
"info": "1234"
},
{
"id": "87aa8f7c61c5d66c",
"type": "function",
"z": "6238296b06f69a38",
"name": "瞬时电流",
"func": "var b = parseInt(msg.payload[2].toString(16)+msg.payload[3].toString(16),16);\nvar s = b&0x80000000/0x80000000;\nvar e = (b&0x7f800000)/0x800000-127;\nvar c = (b&0x7fffff)/0x800000;\nvar r = Math.pow(-1,s)*(1+c)*Math.pow(2,e);\nif(r<0) return null;\nelse msg.payload =r;\nmsg.topic= '瞬时电流';\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1000,
"y": 220,
"wires": [
[
"27d49c8d9d77de90"
]
]
},
{
"id": "757946ed0949a518",
"type": "function",
"z": "6238296b06f69a38",
"name": "有功功率",
"func": "var b = parseInt(msg.payload[4].toString(16)+msg.payload[5].toString(16),16);\nvar s = b&0x80000000/0x80000000;\nvar e = (b&0x7f800000)/0x800000-127;\nvar c = (b&0x7fffff)/0x800000;\nvar r = Math.pow(-1,s)*(1+c)*Math.pow(2,e)*1000;\nif(r<0) return null;\nelse msg.payload =r;\nmsg.topic= '有功功率';\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1000,
"y": 280,
"wires": [
[
"27d49c8d9d77de90"
]
]
},
{
"id": "cabbb5f59a04fc56",
"type": "function",
"z": "6238296b06f69a38",
"name": "功率因数",
"func": "var b = parseInt(msg.payload[10].toString(16)+msg.payload[11].toString(16),16);\nvar s = b&0x80000000/0x80000000;\nvar e = (b&0x7f800000)/0x800000-127;\nvar c = (b&0x7fffff)/0x800000;\nvar r = Math.pow(-1,s)*(1+c)*Math.pow(2,e);\nif(r<0) return null;\nelse msg.payload =r;\nmsg.topic= '功率因数';\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1000,
"y": 340,
"wires": [
[
"27d49c8d9d77de90"
]
]
},
{
"id": "fc5583a057e1b6d7",
"type": "modbus-getter",
"z": "6238296b06f69a38",
"name": "",
"showStatusActivities": true,
"showErrors": true,
"showWarnings": true,
"logIOActivities": false,
"unitid": "",
"dataType": "HoldingRegister",
"adr": "8192",
"quantity": "12",
"server": "ebec70e5.2bab8",
"useIOFile": false,
"ioFile": "",
"useIOForPayload": false,
"emptyMsgOnFail": true,
"keepMsgProperties": true,
"delayOnStart": false,
"startDelayTime": "",
"x": 340,
"y": 320,
"wires": [
[
"8bbf1d74c4d8a84d",
"87aa8f7c61c5d66c",
"757946ed0949a518",
"cabbb5f59a04fc56"
],
[
"15c00fdbe99d22aa",
"f57e3cab512d1186",
"a61e306e76ff120f"
]
]
},
{
"id": "0519c4d54494fec7",
"type": "inject",
"z": "6238296b06f69a38",
"name": "start",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 110,
"y": 320,
"wires": [
[
"fc5583a057e1b6d7"
]
]
},
{
"id": "89dc947368207782",
"type": "modbus-getter",
"z": "6238296b06f69a38",
"name": "",
"showStatusActivities": false,
"showErrors": false,
"showWarnings": true,
"logIOActivities": false,
"unitid": "",
"dataType": "HoldingRegister",
"adr": "8206",
"quantity": "2",
"server": "ebec70e5.2bab8",
"useIOFile": false,
"ioFile": "",
"useIOForPayload": false,
"emptyMsgOnFail": false,
"keepMsgProperties": false,
"delayOnStart": false,
"startDelayTime": "",
"x": 800,
"y": 440,
"wires": [
[
"31bb3f7ef4a62609"
],
[]
]
},
{
"id": "31bb3f7ef4a62609",
"type": "function",
"z": "6238296b06f69a38",
"name": "电网频率",
"func": "var b = parseInt(msg.payload[0].toString(16)+msg.payload[1].toString(16),16);\nvar s = b&0x80000000/0x80000000;\nvar e = (b&0x7f800000)/0x800000-127;\nvar c = (b&0x7fffff)/0x800000;\nvar r = Math.pow(-1,s)*(1+c)*Math.pow(2,e);\nif(r<0) return null;\nelse msg.payload =r;\nmsg.topic= '电网频率';\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1000,
"y": 400,
"wires": [
[
"27d49c8d9d77de90"
]
]
},
{
"id": "18a1a2faa7d194a4",
"type": "modbus-getter",
"z": "6238296b06f69a38",
"name": "",
"showStatusActivities": false,
"showErrors": false,
"showWarnings": true,
"logIOActivities": false,
"unitid": "",
"dataType": "HoldingRegister",
"adr": "16384",
"quantity": "2",
"server": "ebec70e5.2bab8",
"useIOFile": false,
"ioFile": "",
"useIOForPayload": false,
"emptyMsgOnFail": false,
"keepMsgProperties": false,
"delayOnStart": false,
"startDelayTime": "",
"x": 800,
"y": 540,
"wires": [
[
"af288ae2bf508e07"
],
[]
]
},
{
"id": "af288ae2bf508e07",
"type": "function",
"z": "6238296b06f69a38",
"name": "总用电量",
"func": "var b = parseInt(msg.payload[0].toString(16)+msg.payload[1].toString(16),16);\nvar s = b&0x80000000/0x80000000;\nvar e = (b&0x7f800000)/0x800000-127;\nvar c = (b&0x7fffff)/0x800000;\nvar r = Math.pow(-1,s)*(1+c)*Math.pow(2,e);\nif(r<0) return null;\nelse msg.payload =r;\nmsg.topic= '总用电量';\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1000,
"y": 520,
"wires": [
[
"27d49c8d9d77de90"
]
]
},
{
"id": "a61e306e76ff120f",
"type": "delay",
"z": "6238296b06f69a38",
"name": "",
"pauseType": "delay",
"timeout": "20",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"allowrate": false,
"outputs": 1,
"x": 320,
"y": 800,
"wires": [
[
"fc5583a057e1b6d7"
]
]
},
{
"id": "edc5fac1588b374b",
"type": "comment",
"z": "6238296b06f69a38",
"name": "DDSU666 modbus电表数据读取 地址01",
"info": "",
"x": 330,
"y": 100,
"wires": []
},
{
"id": "15c00fdbe99d22aa",
"type": "delay",
"z": "6238296b06f69a38",
"name": "",
"pauseType": "delay",
"timeout": "1",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"allowrate": false,
"outputs": 1,
"x": 580,
"y": 440,
"wires": [
[
"89dc947368207782"
]
]
},
{
"id": "f57e3cab512d1186",
"type": "delay",
"z": "6238296b06f69a38",
"name": "",
"pauseType": "delay",
"timeout": "4",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"allowrate": false,
"outputs": 1,
"x": 580,
"y": 540,
"wires": [
[
"18a1a2faa7d194a4"
]
]
},
{
"id": "ebec70e5.2bab8",
"type": "modbus-client",
"name": "",
"clienttype": "serial",
"bufferCommands": true,
"stateLogEnabled": true,
"queueLogEnabled": true,
"failureLogEnabled": true,
"tcpHost": "127.0.0.1",
"tcpPort": "502",
"tcpType": "DEFAULT",
"serialPort": "/dev/ttyUSB0",
"serialType": "RTU",
"serialBaudrate": "9600",
"serialDatabits": "8",
"serialStopbits": "1",
"serialParity": "none",
"serialConnectionDelay": "100",
"serialAsciiResponseStartDelimiter": "",
"unit_id": "1",
"commandDelay": "1",
"clientTimeout": "1000",
"reconnectOnTimeout": true,
"reconnectTimeout": "2000",
"parallelUnitIdsAllowed": true
}
]