{"id":11717,"date":"2023-02-12T10:25:42","date_gmt":"2023-02-12T02:25:42","guid":{"rendered":"https:\/\/www.modbus.cn\/?p=11717"},"modified":"2023-02-12T11:08:13","modified_gmt":"2023-02-12T03:08:13","slug":"easymodbustcp-net-shi-li-dai-ma","status":"publish","type":"post","link":"https:\/\/www.modbus.cn\/en\/11717.html","title":{"rendered":"EasyModbusTCP.NET\u793a\u4f8b\u4ee3\u7801"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>.NET\uff1aModbus-RTU Master Simple Read and Write \u64cd\u4f5c<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">using System;\nusing EasyModbus;\n \nnamespace ModbusRS485Master\n{\n\tclass Program\n\t{\n\t\tpublic static void Main(string[] args)\n\t\t{\n\t\t\tModbusClient modbusClient = new ModbusClient(&quot;COM1&quot;);\n\t\t\t\/\/modbusClient.UnitIdentifier = 1; Not necessary since default slaveID = 1;\n\t\t\t\/\/modbusClient.Baudrate = 9600;\t\/\/ Not necessary since default baudrate = 9600\n\t\t\t\/\/modbusClient.Parity = System.IO.Ports.Parity.None;\n\t\t\t\/\/modbusClient.StopBits = System.IO.Ports.StopBits.Two;\n\t\t\t\/\/modbusClient.ConnectionTimeout = 500;\t\t\t\n\t\t\tmodbusClient.Connect();\n\t\t\t\n\t\t\tConsole.WriteLine(&quot;Value of Discr. Input #1: &quot;+modbusClient.ReadDiscreteInputs(0,1)[0].ToString());\t\/\/Reads Discrete Input #1\n\t\t\tConsole.WriteLine(&quot;Value of Input Reg. #10: &quot;+modbusClient.ReadInputRegisters(9,1)[0].ToString());\t\/\/Reads Inp. Reg. #10\n\t\t\t\n\t\t\tmodbusClient.WriteSingleCoil(4,true);\t\t\/\/Writes Coil #5\n\t\t\tmodbusClient.WriteSingleRegister(19,4711);\t\/\/Writes Holding Reg. #20\n\t\t\t\n\t\t\tConsole.WriteLine(&quot;Value of Coil #5: &quot;+modbusClient.ReadCoils(4,1)[0].ToString());\t\/\/Reads Discrete Input #1\n\t\t\tConsole.WriteLine(&quot;Value of Holding Reg.. #20: &quot;+modbusClient.ReadHoldingRegisters(19,1)[0].ToString());\t\/\/Reads Inp. Reg. #10\n\t\t\tmodbusClient.WriteMultipleRegisters(49, new int[10] {1,2,3,4,5,6,7,8,9,10});\n\t\t\tmodbusClient.WriteMultipleCoils(29, new bool[10] {true,true,true,true,true,true,true,true,true,true,});\n\t\t\t\n\t\t\tConsole.Write(&quot;Press any key to continue . . . &quot;);\n\t\t\tConsole.ReadKey(true);\n\t\t}\n\t}\n}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>NET\uff1aModbus-TCP \u5ba2\u6237\u7aef\u7b80\u5355\u8bfb\u5199\u64cd\u4f5c<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">namespace ModbusClientApplication\n{\n    class Program\n    {\n        public static void Main(string[] args)\n        {\n            ModbusClient modbusClient = new ModbusClient(&quot;190.201.100.100&quot;, 502);    \/\/Ip-Address and Port of Modbus-TCP-Server\n            modbusClient.Connect();                                                    \/\/Connect to Server\n            modbusClient.WriteMultipleCoils(4, new bool[] {true, true, true, true, true, true, true, true, true, true});    \/\/Write Coils starting with Address 5\n            bool[] readCoils = modbusClient.ReadCoils(9,10);                        \/\/Read 10 Coils from Server, starting with address 10\n            int[] readHoldingRegisters = modbusClient.ReadHoldingRegisters(0,10);    \/\/Read 10 Holding Registers from Server, starting with Address 1\n\n            \/\/ Console Output\n            for (int i = 0; i &amp;lt; readCoils.Length; i++)\n                Console.WriteLine(&quot;Value of Coil &quot; + (9 + i + 1) + &quot; &quot; + readCoils[i].ToString());\n                        \n            for (int i = 0; i &amp;lt; readHoldingRegisters.Length; i++)\n                Console.WriteLine(&quot;Value of HoldingRegister &quot; + (i + 1) + &quot; &quot;+ readHoldingRegisters[i].ToString());            \n            modbusClient.Disconnect();                                                \/\/Disconnect from Server\n            Console.Write(&quot;Press any key to continue . . . &quot;);\n            Console.ReadKey(true);\n        }\n    }\n}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4ece\u6a21\u7ec4\u670d\u52a1\u5668\u8bfb\u53d6\u503c\u5e76\u5c06\u503c\u53d1\u5e03\u5230 MQTT \u4ee3\u7406<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\"> class Program\n    {\n        static void Main(string[] args)\n        {\n            EasyModbus.ModbusClient modbusClient = new EasyModbus.ModbusClient(&quot;127.0.0.1&quot;, 502);\n            \/\/Increase the Connection Timeout to 5 seconds\n            modbusClient.ConnectionTimeout = 5000;\n            \/\/We create a Log File. This will also active the Event logging \n            modbusClient.LogFileFilename = &quot;test.txt&quot;;\n            \/\/The Messages sent to the MQTT-Broker will be retained in the Broker. After subscribtion, the client will automatically\n            \/\/receive the last retained message. By default the Retain-Flag is FALSE -&amp;gt; Messages will not be retained\n            modbusClient.MqttRetainMessages = true;\n            \/\/Connect to the ModbusTCP Server\n            modbusClient.Connect();\n            while (true)\n            {\n                \/\/ We read two registers from the Server, and Publish them to the MQTT-Broker. By default Values will be published\n                \/\/ on change By default we publish to the Topic 'easymodbusclient\/' and the the address e.g. ''easymodbusclient\/holdingregister1'\n                \/\/ The propery &quot;Password&quot; and &quot;Username&quot; can be used to connect to a Broker which require User and Password. By default the \n                \/\/MQTT-Broker port is 1883\n                int[] holdingRegister = modbusClient.ReadHoldingRegisters(60, 2, &quot;www.mqtt-dashboard.com&quot;);\n                System.Threading.Thread.Sleep(1000);\n            }\n            modbusClient.Disconnect();\n            Console.ReadKey();\n        }\n    }<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u81ea\u52a8\u4ece\u6a21\u7ec4\u670d\u52a1\u5668\u8f6e\u8be2\u503c\u5e76\u5c06\u5176\u53d1\u5e03\u5230 MQTT \u4ee3\u7406 \u2013 \u4e3b\u9898\u5df2\u66f4\u6539<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">        static void Main(string[] args)\n        {\n            EasyModbus.EasyModbus2Mqtt easyModbus2Mqtt= new EasyModbus.EasyModbus2Mqtt();\n            \/\/easyModbus2Mqtt.AutomaticReconnect = false;\n            easyModbus2Mqtt.MqttUserName = &quot;sr555&quot;;\n            easyModbus2Mqtt.MqttPassword = &quot;******************&quot;;\n            easyModbus2Mqtt.MqttBrokerPort = 18972;\n            \/\/easyModbus2Mqtt.MqttBrokerAddress = &quot;broker.hivemq.com&quot;;\n            \/\/easyModbus2Mqtt.MqttBrokerAddress = &quot;192.168.178.101&quot;;\n            easyModbus2Mqtt.MqttBrokerAddress = &quot;m21.cloudmqtt.com&quot;;\n            easyModbus2Mqtt.ModbusIPAddress = &quot;127.0.0.1&quot;;\n            easyModbus2Mqtt.AddReadOrder(EasyModbus.FunctionCode.ReadCoils, 2, 0, 200, new string[] { &quot;easymodbusclient\/customtopic1&quot;, &quot;easymodbusclient\/customtopic2&quot; });\n            easyModbus2Mqtt.AddReadOrder(EasyModbus.FunctionCode.ReadInputRegisters, 2, 0, 200);\n            EasyModbus.ReadOrder readOrder = new EasyModbus.ReadOrder();\n            readOrder.Hysteresis = new int[10] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 };\n            readOrder.Unit = new string[10] {&quot;\u00b0C&quot;, &quot;\u00b0C&quot;, &quot;\u00b0C&quot;, &quot;\u00b0C&quot;, &quot;\u00b0C&quot;, &quot;\u00b0C&quot;, &quot;\u00b0C&quot;, &quot;\u00b0C&quot;, &quot;\u00b0C&quot;, &quot;\u00b0C&quot;};\n            readOrder.Scale = new float[10] { 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f };\n            readOrder.Quantity = 10;\n            readOrder.StartingAddress = 10;\n            readOrder.FunctionCode = EasyModbus.FunctionCode.ReadHoldingRegisters;          \n            easyModbus2Mqtt.AddReadOrder(readOrder);\n            easyModbus2Mqtt.start();\n        }<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Modbus-TCP \u670d\u52a1\u5668\uff0c\u5c06\u66f4\u6539\u65f6\u7684\u503c\u53d1\u5e03\u5230 MQTT-Broker<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">   class Program\n    {\n        static void Main(string[] args)\n        {\n            Program application = new Program();\n            application.startServer();\n\n        }\n\n        public void startServer()\n        {\n            EasyModbus.ModbusServer modbusServer = new EasyModbus.ModbusServer();\n            modbusServer.MqttRootTopic = &quot;examplemodbusserver&quot;;\n            modbusServer.MqttBrokerAddress = &quot;www.mqtt-dashboard.com&quot;;\n            modbusServer.Listen();\n            modbusServer.holdingRegistersChanged += new EasyModbus.ModbusServer.HoldingRegistersChanged(holdingRegistersChanged);\n            Console.ReadKey();\n            \n            modbusServer.StopListening();\n        }\n\n        public void holdingRegistersChanged(int startingAddress, int quantity)\n        {\n            Console.WriteLine(startingAddress);\n            Console.WriteLine(quantity);\n        }\n    }<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">EasyModbusTCP\/UDP\/RTU .NET\u5e93\u94fe\u63a5\u5728\u672c\u9875\u4e0b\u8f7d<\/h2>\n<span id=\"magicpostMarker\"><\/span>","protected":false},"excerpt":{"rendered":"<p>.NET\uff1aModbus-RTU Master Simple &#8230;<\/p>","protected":false},"author":1,"featured_media":27977,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[691],"tags":[1460,1456,1458,1457,1195,1459],"class_list":["post-11717","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modbus-code-base","tag-modbus-rtu-master-simple-read-and-write-using-system","tag-string","tag-tostring","tag-true","tag-1195","tag-1459"],"_links":{"self":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/11717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/comments?post=11717"}],"version-history":[{"count":0,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/11717\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media\/27977"}],"wp:attachment":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media?parent=11717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/categories?post=11717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/tags?post=11717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}