2024年4月3日 星期三

Node-red 與 Modbus ( 利用模擬軟體) Read/Write

 Node-red 與 Modbus ( 利用模擬軟體) Read/Write 

  • Modbus是一種用於工業控制的標準通信協議,它定義了裝置之間在應用層的消息封裝模式、溝通方法、溝通順序。
  • Modbus的優勢 :
    1. Modbus協議是開源免費的
    2. Modbus支援多種常見工控接口(RS232, RS485, TCP/IP etc.),且可以各種物理層裝置上傳輸(雙絞線、光纖、無線等)
    3. Modbus格式簡單易於開發
  • Modbus能夠將眾多設備連接到DCS, PLC系統上,再利用服務器(雲、中央計算機)進行監控與下達指令,以便於集中式控制。




write 16 bits data

var fc=6;
var sa=40;
var addresses=1;
var value=16001;

//msg.slave_ip="192.168.1.76";
msg.slave_ip="127.0.0.1";
msg.payload={"value":value , 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };
return msg;



write 32 bits data

var fc=16;
var sa=50;
var addresses=2;
var buf=Buffer.alloc(4);

//We then write the integer (68001) into this buffer.
//For multiple resisters the node expects 16 bit decimal values 
//for each register and so we need to split our buffer into 2 parts.
//We then create an array of the form [value1,value2].
//Notice value1 is the low order 2 buffers (0,1), 
//and value 2 is the higher order buffers (2,3). 
//Notice also we need to multiple the buffers 1 and 2 by 256.

buf.writeInt32BE(68001);
var values=[(buf[0]*256+buf[1]),(buf[2]*256)+buf[3]]

//msg.slave_ip="192.168.1.76";
msg.slave_ip="127.0.0.1";
msg.payload={"value":values , 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };
return msg;

Read 16 bits data


var fc=3;
var sa=40;
var addresses=1;


var slave_ip=msg.payload.slave_ip;
//msg.slave_ip="192.168.1.31";
msg.slave_ip="127.0.0.1";
msg.payload={value: msg.payload, 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };
return msg;

Read 32 bits data

var fc=3;
var sa=50;
var addresses=2;

var slave_ip=msg.payload.slave_ip;
//msg.slave_ip="192.168.1.31";
msg.slave_ip="127.0.0.1";
msg.payload={value: msg.payload, 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };
return msg;


[{"id":"4adfe51099bc41eb","type":"modbus-flex-getter","z":"fd65c1497a694644","name":"local getter","showStatusActivities":true,"showErrors":true,"showWarnings":true,"logIOActivities":false,"server":"54dde46e.53267c","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"delayOnStart":false,"startDelayTime":"","x":570,"y":380,"wires":[[],["88165e7795193864"]]},{"id":"6b1cb5d7c0ccbee3","type":"debug","z":"fd65c1497a694644","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":850,"y":380,"wires":[]},{"id":"0b12478b3517da2f","type":"function","z":"fd65c1497a694644","name":"16 Bit INT 16001","func":"\nvar fc=3;\nvar sa=40;\nvar addresses=1;\n\n\nvar slave_ip=msg.payload.slave_ip;\n//msg.slave_ip=\"192.168.1.31\";\nmsg.slave_ip=\"127.0.0.1\";\nmsg.payload={value: msg.payload, 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":380,"wires":[["4adfe51099bc41eb"]]},{"id":"3e1ca19ea730a4b8","type":"inject","z":"fd65c1497a694644","name":"Read 16bit Integer","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":380,"wires":[["0b12478b3517da2f"]]},{"id":"88165e7795193864","type":"function","z":"fd65c1497a694644","name":"","func":"const buf = Buffer.from(msg.payload.buffer);\nconst value = buf.readUInt16BE();\nmsg.value = value;\nreturn msg;","outputs":1,"noerr":0,"x":730,"y":380,"wires":[["6b1cb5d7c0ccbee3"]]},{"id":"e29a61c6612de4b4","type":"debug","z":"fd65c1497a694644","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":750,"y":126,"wires":[]},{"id":"d7c29393e377ffe8","type":"inject","z":"fd65c1497a694644","name":"Write32INT","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":200,"wires":[["d81adf9deae3b636"]]},{"id":"d81adf9deae3b636","type":"function","z":"fd65c1497a694644","name":"write int 68001-32 bit","func":"\nvar fc=16;\nvar sa=50;\nvar addresses=2;\nvar buf=Buffer.alloc(4);\n\n//We then write the integer (68001) into this buffer.\n//For multiple resisters the node expects 16 bit decimal values \n//for each register and so we need to split our buffer into 2 parts.\n//We then create an array of the form [value1,value2].\n//Notice value1 is the low order 2 buffers (0,1), \n//and value 2 is the higher order buffers (2,3). \n//Notice also we need to multiple the buffers 1 and 2 by 256.\n\nbuf.writeInt32BE(68001);\nvar values=[(buf[0]*256+buf[1]),(buf[2]*256)+buf[3]]\n\n//msg.slave_ip=\"192.168.1.76\";\nmsg.slave_ip=\"127.0.0.1\";\nmsg.payload={\"value\":values , 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":200,"wires":[["fa4911e652250a3d","dab1314c93cc4812"]]},{"id":"fa4911e652250a3d","type":"modbus-flex-write","z":"fd65c1497a694644","name":"","showStatusActivities":false,"showErrors":false,"server":"54dde46e.53267c","emptyMsgOnFail":false,"keepMsgProperties":false,"x":590,"y":200,"wires":[[],["f53fd56c9554f0f4"]]},{"id":"1dea16e4b43369ac","type":"inject","z":"fd65c1497a694644","name":"Write16INT","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":120,"wires":[["ac2545061d959e56"]]},{"id":"ac2545061d959e56","type":"function","z":"fd65c1497a694644","name":"Write 16001-16bit","func":"\nvar fc=6;\nvar sa=40;\nvar addresses=1;\nvar value=16001;\n\n//msg.slave_ip=\"192.168.1.76\";\nmsg.slave_ip=\"127.0.0.1\";\nmsg.payload={\"value\":value , 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":120,"wires":[["fc985d43d8a71f3f"]]},{"id":"fc985d43d8a71f3f","type":"modbus-flex-write","z":"fd65c1497a694644","name":"","showStatusActivities":false,"showErrors":true,"showWarnings":true,"server":"54dde46e.53267c","emptyMsgOnFail":false,"keepMsgProperties":false,"delayOnStart":false,"startDelayTime":"","x":570,"y":120,"wires":[[],["e29a61c6612de4b4"]]},{"id":"dab1314c93cc4812","type":"debug","z":"fd65c1497a694644","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":550,"y":260,"wires":[]},{"id":"b9ba12b598c221fe","type":"comment","z":"fd65c1497a694644","name":"read functionm","info":"read functionm\n function codes 1,2,3 and 4 \n \nFunction Code\tAction\tTable Name\n01\tRead\tDiscrete Output Coils\n02\tRead\tDiscrete Input Contacts\n04\tRead\tAnalogue Input Registers\n03\tRead\tAnalogue Output Holding Registers\n\n\nCoil/Register Numbers\tType\tTable Name\tData Addresses\tData Type\n\n1-9999\tRead-Write\tDiscrete Output Coils\t0000 to 270E\t**Boolean**\n10001-19999\tRead-Only\tDiscrete Input Coils\t0000 to 270E\t**Boolean**\n\n30001-39999\tRead-Only\tAnalogue Input Registers\t0000 to 270E\t**Unsigned Word**\n\n40001-49999\tRead-Write\tAnalogue Output Holding Registers\t0000 to 270E\t**Unsigned Word**","x":160,"y":260,"wires":[]},{"id":"3056d8bd26283f71","type":"comment","z":"fd65c1497a694644","name":"write  functionm","info":"read functionm\n function codes 5,15,6 and 16 \n \n \nFunction Code\t  Action   \tTable Name\n05\tWrite single\tDiscrete Output Coil\n15\tWrite multiple\tDiscrete Output Coils\n06\tWrite single\tAnalogue Output Holding Register\n16\tWrite multiple\tAnalogue Output Holding Registers\n\n\n\nCoil/Register Numbers\tType\tTable Name\tData Addresses\tData Type\n\n1-9999\tRead-Write\tDiscrete Output Coils\t0000 to 270E\t**Boolean**\n10001-19999\tRead-Only\tDiscrete Input Coils\t0000 to 270E\t**Boolean**\n\n30001-39999\tRead-Only\tAnalogue Input Registers\t0000 to 270E\t**Unsigned Word**\n\n40001-49999\tRead-Write\tAnalogue Output Holding Registers\t0000 to 270E\t**Unsigned Word**","x":340,"y":260,"wires":[]},{"id":"f53fd56c9554f0f4","type":"debug","z":"fd65c1497a694644","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":750,"y":200,"wires":[]},{"id":"431929ddf7c78057","type":"inject","z":"fd65c1497a694644","name":"Read 32bit Integer","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":420,"wires":[["59ffde370c5ac068"]]},{"id":"59ffde370c5ac068","type":"function","z":"fd65c1497a694644","name":"32 Bit INT 68001","func":"\nvar fc=3;\nvar sa=50;\nvar addresses=2;\n\nvar slave_ip=msg.payload.slave_ip;\n//msg.slave_ip=\"192.168.1.31\";\nmsg.slave_ip=\"127.0.0.1\";\nmsg.payload={value: msg.payload, 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":420,"wires":[["4adfe51099bc41eb"]]},{"id":"a4e7ea1df47b0d10","type":"comment","z":"fd65c1497a694644","name":"映射地址","info":"\n## 代码 中文名称 寄存器 PLC 地址 位操作/字操作 操作数量\n## 01   读数字量输出 00001-09999 位操作 单个或多个\n## 02   读数字量输入 10001-19999 位操作 单个或多个\n## 03   读模拟量输出 40001-49999 字操作 单个或多个\n## 04   读模拟量输入 30001-39999 字操作 单个或多个\n## \n## 05   写单个数字量输出 00001-09999 位操作 单个\n## 06   写单个模拟量输出 40001-49999 字操作 单个\n## 15   写多个数字量输出 00001-09999 位操作 多个\n## 16   写多个模拟量输出 40001-49999 字操作 多个","x":160,"y":320,"wires":[]},{"id":"93dd4da734fc21a8","type":"comment","z":"fd65c1497a694644","name":"","info":"fc (function code)\n(fc=1  <--> fc 5,15)\nread \n读数字量输出\nfc=1 , 00001-09999\nwrite\n写单个数字量输出\nfc =5 , 00001-09999\n写多个数字量输出\nfc =15 , 00001-09999\n\nread \n读数字量输出\nfc=2 , 10001-19999\nfc=4 ,30001-39999\n\n\n(fc=3 <--> fc 6,16)\nread \n读模拟量输出出\nfc=3 , 40001-49999\nwrite\n写单个模拟量输出\nfc =6 , 40001-49999\nwrite\n写单个模拟量输出\nfc =16 , 40001-49999\n\n\n\n","x":330,"y":320,"wires":[]},{"id":"54dde46e.53267c","type":"modbus-client","name":"local","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":true,"queueLogEnabled":false,"failureLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"","unit_id":1,"commandDelay":1,"clientTimeout":1000,"reconnectOnTimeout":false,"reconnectTimeout":2000,"parallelUnitIdsAllowed":false}]

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...