基于Java的Modbus通讯示例代码

<span class="n">package</span> <span class="n">run</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">de.re.easymodbus.server.</span><span class="o">*</span><span class="p">;</span>

<span class="n">public</span> <span class="k">class</span> <span class="nc">run</span> <span class="p">{</span>
    <span class="n">public</span> <span class="n">static</span> <span class="n">void</span> <span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="p">[]</span> <span class="n">args</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">ModbusServer</span> <span class="n">modbusServer</span> <span class="o">=</span> <span class="n">new</span> <span class="n">ModbusServer</span><span class="p">();</span>
        <span class="n">modbusServer</span><span class="o">.</span><span class="n">setPort</span><span class="p">(</span><span class="mi">1522</span><span class="p">);</span><span class="o">//</span><span class="n">Note</span> <span class="n">that</span> <span class="n">Standard</span> <span class="n">Port</span> <span class="k">for</span> <span class="n">Modbus</span> <span class="n">TCP</span> <span class="n">communication</span> <span class="ow">is</span> <span class="mi">502</span>
        <span class="n">modbusServer</span><span class="o">.</span><span class="n">coils</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">true</span><span class="p">;</span>
        <span class="n">modbusServer</span><span class="o">.</span><span class="n">holdingRegisters</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1234</span><span class="p">;</span>
        <span class="k">try</span>
        <span class="p">{</span>
            <span class="n">modbusServer</span><span class="o">.</span><span class="n">Listen</span><span class="p">();</span>
        <span class="p">}</span>
        <span class="n">catch</span> <span class="p">(</span><span class="n">java</span><span class="o">.</span><span class="n">io</span><span class="o">.</span><span class="n">IOException</span> <span class="o">|</span> <span class="n">Exceptions</span><span class="o">.</span><span class="n">ModbusException</span> <span class="n">e</span><span class="p">)</span>
        <span class="p">{</span>           
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>

相关新闻

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

邮箱

cloud@modbus.cn

QQ
QQ
微信
微信
分享本页
返回顶部