{"id":6775,"date":"2022-03-18T08:25:26","date_gmt":"2022-03-18T00:25:26","guid":{"rendered":"https:\/\/www.modbus.cn\/?p=6775"},"modified":"2022-03-18T08:28:01","modified_gmt":"2022-03-18T00:28:01","slug":"ji-yu-java-de-modbus-tong-xun-shi-li-dai-ma","status":"publish","type":"post","link":"https:\/\/www.modbus.cn\/en\/6775.html","title":{"rendered":"\u57fa\u4e8eJava\u7684Modbus TCP Server \u901a\u8baf\u793a\u4f8b\u4ee3\u7801"},"content":{"rendered":"<p class=\"wp-block-paragraph\">\u57fa\u4e8eJava\u7684Modbus\u901a\u8baf\u793a\u4f8b\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;span class=&quot;n&quot;&gt;package&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;run&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;\/span&gt;\r\n&lt;span class=&quot;kn&quot;&gt;import&lt;\/span&gt; &lt;span class=&quot;nn&quot;&gt;de.re.easymodbus.server.&lt;\/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;\/span&gt;\r\n\r\n&lt;span class=&quot;n&quot;&gt;public&lt;\/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;\/span&gt; &lt;span class=&quot;nc&quot;&gt;run&lt;\/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;\/span&gt;\r\n    &lt;span class=&quot;n&quot;&gt;public&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;static&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;void&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;\/span&gt;\r\n    &lt;span class=&quot;p&quot;&gt;{&lt;\/span&gt;\r\n        &lt;span class=&quot;n&quot;&gt;ModbusServer&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;modbusServer&lt;\/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;ModbusServer&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;\/span&gt;\r\n        &lt;span class=&quot;n&quot;&gt;modbusServer&lt;\/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;\/span&gt;&lt;span class=&quot;n&quot;&gt;setPort&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;mi&quot;&gt;1522&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;\/span&gt;&lt;span class=&quot;o&quot;&gt;\/\/&lt;\/span&gt;&lt;span class=&quot;n&quot;&gt;Note&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;that&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;Standard&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;Port&lt;\/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;Modbus&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;TCP&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;communication&lt;\/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;\/span&gt; &lt;span class=&quot;mi&quot;&gt;502&lt;\/span&gt;\r\n        &lt;span class=&quot;n&quot;&gt;modbusServer&lt;\/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;\/span&gt;&lt;span class=&quot;n&quot;&gt;coils&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;\/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;\/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;true&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;\/span&gt;\r\n        &lt;span class=&quot;n&quot;&gt;modbusServer&lt;\/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;\/span&gt;&lt;span class=&quot;n&quot;&gt;holdingRegisters&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;\/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;\/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;\/span&gt; &lt;span class=&quot;mi&quot;&gt;1234&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;\/span&gt;\r\n        &lt;span class=&quot;k&quot;&gt;try&lt;\/span&gt;\r\n        &lt;span class=&quot;p&quot;&gt;{&lt;\/span&gt;\r\n            &lt;span class=&quot;n&quot;&gt;modbusServer&lt;\/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;\/span&gt;&lt;span class=&quot;n&quot;&gt;Listen&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;\/span&gt;\r\n        &lt;span class=&quot;p&quot;&gt;}&lt;\/span&gt;\r\n        &lt;span class=&quot;n&quot;&gt;catch&lt;\/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;n&quot;&gt;java&lt;\/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;\/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;\/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;\/span&gt;&lt;span class=&quot;n&quot;&gt;IOException&lt;\/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;Exceptions&lt;\/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;\/span&gt;&lt;span class=&quot;n&quot;&gt;ModbusException&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;\/span&gt;\r\n        &lt;span class=&quot;p&quot;&gt;{&lt;\/span&gt;           \r\n        &lt;span class=&quot;p&quot;&gt;}&lt;\/span&gt;\r\n    &lt;span class=&quot;p&quot;&gt;}&lt;\/span&gt;\r\n&lt;span class=&quot;p&quot;&gt;}&lt;\/span&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<span id=\"magicpostMarker\"><\/span>","protected":false},"excerpt":{"rendered":"<p>\u57fa\u4e8eJava\u7684Modbus\u901a\u8baf\u793a\u4f8b\u4ee3\u7801<\/p>","protected":false},"author":1,"featured_media":27977,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[691],"tags":[1543,1087,690,638],"class_list":["post-6775","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modbus-code-base","tag-java","tag-modbus-tcp","tag-modbus-protocol","tag-tcp"],"_links":{"self":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/6775","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=6775"}],"version-history":[{"count":0,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/6775\/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=6775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/categories?post=6775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/tags?post=6775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}