2021年10月23日 星期六

Modbus RTU (Serial Port)Node-Red Dashboards and MQTT Modbus Bridge

Modbus RTU(Serial Port) Node-Red Dashboards and MQTT Modbus Bridge 

參考來源https://stevesnoderedguide.com/node-red-modbus-dashboards




316201"C倉庫"Modbus RTU 4路繼電器模組12V 四路開關量輸入 光耦隔離 RS485 W8.19012

https://www.ruten.com.tw/item/show?21904711229817













ModBus RTU

Form


Function

var fc=1;

var sa=0;

var addresses=8;

var slave_ip="COM8";

msg.payload={value: msg.payload, 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };

 

return msg;

Function

var fc=msg.payload.fc;

var sa=msg.payload.sa;

var addresses=msg.payload.addresses;

var slave_ip=msg.payload.slave_ip;

var payload={ 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };

msg.payload=JSON.stringify(payload);

return msg

Function

var topic=msg.topic;

if(topic=="alex9ufo/modbus/response/read")

msg.read=msg.payload.data;

else

msg.write=msg.payload;

 

return msg;

Form


Function

var fc=msg.payload.fc;

var sa=msg.payload.sa;

var addresses=msg.payload.addresses;

var value=msg.payload.value;

var values=msg.payload.values;

var slave_ip=msg.payload.slave_ip;

if (fc==16 || fc==15){

     value=JSON.parse(msg.payload.values);

    node.log(value);

}

payload={"value":value , 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };

 

msg.payload=JSON.stringify(payload);

return msg

 

ModBus

Client 設定

 

Local getter

 

Modbus Flex Write



 

Change

 

設定msg.topic



 Node-Red 

[{"id":"ee694368.3972f","type":"debug","z":"d9626aca.91e21","name":"received","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":400,"y":600,"wires":[]},{"id":"b839b204.440808","type":"function","z":"d9626aca.91e21","name":"","func":"\nvar fc=msg.payload.fc;\nvar sa=msg.payload.sa;\nvar addresses=msg.payload.addresses;\nvar slave_ip=msg.payload.slave_ip;\nvar payload={ 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };\nmsg.payload=JSON.stringify(payload);\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":160,"wires":[["ffd00ac8.95bca","89afe41a.edf1b"]]},{"id":"beba480e.211a78","type":"function","z":"d9626aca.91e21","name":"write mqtt","func":"var fc=msg.payload.fc;\nvar sa=msg.payload.sa;\nvar addresses=msg.payload.addresses;\nvar value=msg.payload.value;\nvar values=msg.payload.values;\nvar slave_ip=msg.payload.slave_ip;\nif (fc==16 || fc==15){\n     value=JSON.parse(msg.payload.values);\n    node.log(value);\n}\npayload={\"value\":value , 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };\n\nmsg.payload=JSON.stringify(payload);\nreturn msg\n\n","outputs":1,"noerr":0,"x":480,"y":400,"wires":[["257946b2.2703da"]]},{"id":"89afe41a.edf1b","type":"debug","z":"d9626aca.91e21","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":630,"y":180,"wires":[]},{"id":"b2ee29d7.deb458","type":"ui_form","z":"d9626aca.91e21","name":"","label":"Read MQTT fc=Codes 1(S_Coil),2,(D_In),3(Holding_R),4(IN_reg)","group":"19dad5af.6814ca","order":3,"width":"0","height":"0","options":[{"label":"Function Code","value":"fc","type":"number","required":true,"rows":null},{"label":"Slave_ip","value":"slave_ip","type":"text","required":false,"rows":null},{"label":"start address","value":"sa","type":"number","required":true,"rows":null},{"label":"number to read","value":"addresses","type":"number","required":true,"rows":null}],"formValue":{"fc":"","slave_ip":"","sa":"","addresses":""},"payload":"","submit":"submit","cancel":"cancel","topic":"","topicType":"str","splitLayout":false,"className":"","x":260,"y":80,"wires":[["b839b204.440808"]]},{"id":"1c2da2ca.175045","type":"function","z":"d9626aca.91e21","name":"","func":"node.log(\"payload \"+msg.payload);\ndata=msg.payload[0]*65535 +msg.payload[1];\nnode.log(\"payload \"+data);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":200,"wires":[["89afe41a.edf1b"]]},{"id":"edcec760.6c2e2","type":"inject","z":"d9626aca.91e21","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":160,"wires":[["348e0b65.748344"]]},{"id":"348e0b65.748344","type":"function","z":"d9626aca.91e21","name":"","func":"var fc=1;\nvar sa=0;\nvar addresses=8;\nvar slave_ip=\"COM8\";\nmsg.payload={value: msg.payload, 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":160,"wires":[["b839b204.440808"]]},{"id":"ffd00ac8.95bca","type":"mqtt out","z":"d9626aca.91e21","name":"","topic":"alex9ufo/modbus/commands/read","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"2db287b.addf978","x":700,"y":120,"wires":[]},{"id":"f0b386ab.d3a888","type":"mqtt in","z":"d9626aca.91e21","name":"","topic":"alex9ufo/modbus/response/#","qos":"0","datatype":"json","broker":"2db287b.addf978","nl":false,"rap":false,"x":140,"y":280,"wires":[["7b4fc821.22c538","4ecdcbf7.dc8a8c","8ebb1c8d.a2913"]]},{"id":"257946b2.2703da","type":"mqtt out","z":"d9626aca.91e21","name":"","topic":"alex9ufo/modbus/commands/write","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"2db287b.addf978","x":700,"y":400,"wires":[]},{"id":"7b4fc821.22c538","type":"switch","z":"d9626aca.91e21","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"alex9ufo/modbus/response/read","vt":"str"},{"t":"eq","v":"alex9ufo/modbus/response/write","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":280,"wires":[[],[]]},{"id":"29c8bd31.ddd502","type":"ui_text","z":"d9626aca.91e21","group":"b1e93ddb.38aa38","order":3,"width":"0","height":"0","name":"","label":"Result Write","format":"{{msg.write}}","layout":"row-left","className":"","x":630,"y":320,"wires":[]},{"id":"bfa650ea.0fdd6","type":"ui_form","z":"d9626aca.91e21","name":"","label":"Write MQTT Codes 5(SC),6(SR),15(MC),16(MR)","group":"b1e93ddb.38aa38","order":2,"width":"0","height":"0","options":[{"label":"Function Code","value":"fc","type":"number","required":true,"rows":null},{"label":"Slave_ip","value":"slave_ip","type":"text","required":false,"rows":null},{"label":"start address","value":"sa","type":"number","required":true,"rows":null},{"label":"number to write","value":"addresses","type":"number","required":true,"rows":null},{"label":"Value","value":"value","type":"number","required":false,"rows":null},{"label":"Values","value":"values","type":"text","required":false,"rows":null}],"formValue":{"fc":"","slave_ip":"","sa":"","addresses":"","value":"","values":""},"payload":"","submit":"submit","cancel":"cancel","topic":"","topicType":"str","splitLayout":false,"className":"","x":200,"y":400,"wires":[["beba480e.211a78"]]},{"id":"a6570060.1cf24","type":"mqtt in","z":"d9626aca.91e21","name":"","topic":"alex9ufo/modbus/commands/read","qos":"0","datatype":"json","broker":"2db287b.addf978","nl":false,"rap":false,"x":160,"y":520,"wires":[["ee694368.3972f","b89db30c.a37a68"]]},{"id":"f56b2fb.642915","type":"mqtt out","z":"d9626aca.91e21","name":"","topic":"","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"2db287b.addf978","x":770,"y":580,"wires":[]},{"id":"b89db30c.a37a68","type":"modbus-flex-getter","z":"d9626aca.91e21","name":"local getter","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"server":"54dde46e.53267c","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":410,"y":520,"wires":[[],["5517ba73.9f0d24","9907658f.e99d88"]]},{"id":"5517ba73.9f0d24","type":"debug","z":"d9626aca.91e21","name":"out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":590,"y":460,"wires":[]},{"id":"4ecdcbf7.dc8a8c","type":"debug","z":"d9626aca.91e21","name":"received","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":380,"y":240,"wires":[]},{"id":"8ebb1c8d.a2913","type":"function","z":"d9626aca.91e21","name":"received function","func":"var topic=msg.topic;\nif(topic==\"alex9ufo/modbus/response/read\")\nmsg.read=msg.payload.data;\nelse\nmsg.write=msg.payload;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":320,"wires":[["29c8bd31.ddd502","7652b3d7.11683c"]]},{"id":"3038d2ee.9e2bf6","type":"mqtt in","z":"d9626aca.91e21","name":"","topic":"alex9ufo/modbus/commands/write","qos":"0","datatype":"json","broker":"2db287b.addf978","nl":false,"rap":false,"x":160,"y":680,"wires":[["ee694368.3972f","8aba8951.d541a"]]},{"id":"8aba8951.d541a","type":"modbus-flex-write","z":"d9626aca.91e21","name":"","showStatusActivities":false,"showErrors":false,"server":"54dde46e.53267c","emptyMsgOnFail":false,"keepMsgProperties":false,"x":430,"y":680,"wires":[[],["6850eba3.a0cb84"]]},{"id":"9907658f.e99d88","type":"change","z":"d9626aca.91e21","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"alex9ufo/modbus/response/read","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":540,"wires":[["f56b2fb.642915"]]},{"id":"6850eba3.a0cb84","type":"change","z":"d9626aca.91e21","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"alex9ufo/modbus/response/write","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":640,"wires":[["f56b2fb.642915"]]},{"id":"7652b3d7.11683c","type":"ui_text","z":"d9626aca.91e21","group":"f9918dcb.b9e1f8","order":3,"width":"0","height":"0","name":"","label":"Result read","format":"{{msg.read}}","layout":"row-left","className":"","x":630,"y":280,"wires":[]},{"id":"19dad5af.6814ca","type":"ui_group","name":"Read","tab":"c6d493fc.df122","order":1,"disp":true,"width":"12","collapse":false,"className":""},{"id":"2db287b.addf978","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"/vm/mqtt/birth","birthQos":"0","birthPayload":"birth","birthMsg":{},"closeTopic":"","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"b1e93ddb.38aa38","type":"ui_group","name":"Write","tab":"c6d493fc.df122","order":2,"disp":true,"width":"12","collapse":false,"className":""},{"id":"54dde46e.53267c","type":"modbus-client","name":"local","clienttype":"simpleser","bufferCommands":true,"stateLogEnabled":true,"queueLogEnabled":false,"tcpHost":"192.168.1.31","tcpPort":"502","tcpType":"TCP-RTU-BUFFERED","serialPort":"COM8","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":1,"commandDelay":1,"clientTimeout":1000,"reconnectOnTimeout":false,"reconnectTimeout":2000,"parallelUnitIdsAllowed":false},{"id":"f9918dcb.b9e1f8","type":"ui_group","name":"Read Result","tab":"c6d493fc.df122","order":3,"disp":true,"width":"12","collapse":false,"className":""},{"id":"c6d493fc.df122","type":"ui_tab","name":"Modbus","icon":"dashboard","disabled":false,"hidden":false}]




MODBUS TCP Client 測試程式
MODBUS TCP Server 模擬程式
MODBUS RTU Master 測試程式
MODBUS RTU Slave 模擬程式

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...