Modbus

Node-REDを使ってModbusのMasterとSlaveの検証をしてみた

Node-REDでModbusを使う方法を検証中。

こちらのライブラリーを使います。

node-red-contrib-modbus (node) – Node-RED

ちょっと古いですが、こちらの動画を見ながら動作の確認をすることができました。

フロー

UntitledImage

動画を見ながら作成したフローはこちら。

[{"id":"a93effa614da2604","type":"modbus-server","z":"d78b8f871190815d","name":"","logEnabled":false,"hostname":"0.0.0.0","serverPort":"502","responseDelay":100,"delayUnit":"ms","coilsBufferSize":10000,"holdingBufferSize":10000,"inputBufferSize":10000,"discreteBufferSize":10000,"showErrors":false,"x":460,"y":240,"wires":[["f8ed3667788a4fd0"],["f8ed3667788a4fd0"],["f8ed3667788a4fd0"],["f8ed3667788a4fd0"],["f8ed3667788a4fd0"]]},{"id":"ae1b6df3bb9c4b5a","type":"inject","z":"d78b8f871190815d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":250,"y":220,"wires":[["a93effa614da2604"]]},{"id":"f8ed3667788a4fd0","type":"debug","z":"d78b8f871190815d","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":260,"wires":[]},{"id":"edc21ae6596480ce","type":"modbus-flex-getter","z":"d78b8f871190815d","name":"FlexGetter","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"server":"b62d434859b22bc4","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":550,"y":420,"wires":[[],["a4e6a3afceb67fea"]]},{"id":"a4e6a3afceb67fea","type":"modbus-response","z":"d78b8f871190815d","name":"","registerShowMax":20,"x":750,"y":420,"wires":[]},{"id":"922ed923301accb7","type":"inject","z":"d78b8f871190815d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":250,"y":320,"wires":[["9c6490e8241964b4"]]},{"id":"9c6490e8241964b4","type":"function","z":"d78b8f871190815d","name":"FC3 0[4]","func":"msg.payload = { \n        'fc': 3,\n        'unitid': 0,\n        'address': 0 ,\n        'quantity': 4\n    };\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":360,"wires":[["edc21ae6596480ce"]]},{"id":"826b7a0cae32b0c2","type":"function","z":"d78b8f871190815d","name":"FC16 0[2]","func":"msg.payload = {\n        value : msg.payload,\n        'fc': 16,\n        'unitid': 0,\n        'address': 0 ,\n        'quantity': 2\n    };\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":520,"wires":[["98fa71a376265232"]]},{"id":"98fa71a376265232","type":"modbus-flex-write","z":"d78b8f871190815d","name":"FlexWrite","showStatusActivities":false,"showErrors":false,"server":"b62d434859b22bc4","emptyMsgOnFail":false,"keepMsgProperties":false,"x":720,"y":520,"wires":[[],[]]},{"id":"602494bf34f00e32","type":"inject","z":"d78b8f871190815d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":230,"y":460,"wires":[["5c419a49d91c4cba"]]},{"id":"5c419a49d91c4cba","type":"join","z":"d78b8f871190815d","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":370,"y":520,"wires":[["826b7a0cae32b0c2"]]},{"id":"ecb171afe55be84e","type":"inject","z":"d78b8f871190815d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":230,"y":520,"wires":[["5c419a49d91c4cba"]]},{"id":"b62d434859b22bc4","type":"modbus-client","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":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":"0x3A","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

フロー解説

まずはModbusServerについて。これは、ModbusのSlaveにあたるもの。

UntitledImage

Modbus Serverノードは動画はPortを502にしただけで他はそのままの設定でした。

この次に、Modbus Flex GetterとModbus Flex Writeを使います。こちらでSlaveからの情報を取得するMaster側の動作を行います。

Function3:Read Holding Registers

UntitledImage

Modbus Flex GetterノードではServer(Modbus Slave)を設定します。

UntitledImage

今回、Server(Slave)は同じNode-REDのものをつかいますのでHostは127.0.0.1としています。

UntitledImage

Modbus Responseはそのままのものを挿入。

msg.payload = { 
        'fc': 3,
        'unitid': 0,
        'address': 0 ,
        'quantity': 4
    };

return msg;

Functionノードには上記のように設定。

fc3というのは、function 3: Read Multiple Holding Registersとなり記載された内容でレジスタの値を読み取る命令をFlexGetterに送り、結果がResponseに表示されます。

Function16:Write Multiple Holding Registers

次は書き込む方法。

UntitledImage

Injectノードから数字を送ると、Joinノードで配列にしてPayloadに送ります。

UntitledImage

Functionノードではこの後に書き込む内容を表記。

UntitledImage

Flex-Writeでは、Server(Slave)を選択します。

実行

UntitledImage

FlexGetter(読み込み)のInjectノードを押すと、Serverに対してRead Multiple Holding Registersを行い結果がResponseに表示されます。

UntitledImage

Flex-Getterにdebugノードをつけることで配列で値が取得できます。

UntitledImage

次に、FlexWriteのInjectノードを1回づつ押した後、FlexGetterのInjectノードを押すと、書き込んだ内容がResponseとして取得できます。

まとめ

以上、node-red-contrib-modbusを使って、Node-RED上でModbusのSlaveを立ち上げ、Modbus MasterとしてSlaveのデータを読み書きする方法の確認ができました。

次はWindowsのModbus Master/Slaveを使ってもデータのやりとりができるか確認してみます。

ABOUT ME
中畑 隆拓
スマートライト㈱ 代表取締役。IoTソリューションの開発、スマートホーム&オフィスのコンサルティング、DALI,KNX,EnOceanなどのインテグレーションを行っています。

COMMENT

メールアドレスが公開されることはありません。 が付いている欄は必須項目です