<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[RTU example]]></title><description><![CDATA[<p dir="auto">Hi All,</p>
<p dir="auto">Downloading Modbus4J-src from sourceforge, you get a file, called Test.java helping you to set up a Modbus/Tcp master ..</p>
<p dir="auto">Is something similar available for Modbus RTU?</p>
<p dir="auto">The device I'm having supports only the following instructions:<br />
03 (0x03) Read Holding Registers<br />
04 (0x04) Read Input Registers<br />
06 (0x06) Write Single Register<br />
16 (0x10) Write Multiple Registers<br />
17 (0x11) Report Slave ID</p>
<p dir="auto">In the Modbus specification, register numbers are documented as "one based", but transmitted as "zero based" -- How are the frames created in Modbus4J for the RS485 bus? 1000 is 1000 (0x03E8) or 1001?</p>
<p dir="auto">many thanks</p>
]]></description><link>https://forum.mango-os.com/topic/737/rtu-example</link><generator>RSS for Node</generator><lastBuildDate>Fri, 12 Jun 2026 11:18:11 GMT</lastBuildDate><atom:link href="https://forum.mango-os.com/topic/737.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 03 Feb 2011 02:10:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to RTU example on Mon, 21 Feb 2011 15:39:14 GMT]]></title><description><![CDATA[<p dir="auto">Thank you Matthew.</p>
<p dir="auto">I was able to use the factory and create the RTU master. The issue I was having was related with the definition of the serial parameters .. This is the code I came up with. It works .. knowing that the slave I'm reading from has the ID 7, and the connection over RS485 (COM6 here) is of "N81" type, at 9600 baudrate .</p>
<pre><code>
package appliancemb;

import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.code.RegisterRange;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.io.serial.*;

// declare variables
public class appliance {

   public static void main(String[] args) throws Exception {
        ModbusFactory factory = new ModbusFactory();
        SerialParameters params = new SerialParameters();
          params.setCommPortId("COM6");
          params.setBaudRate(9600);
          params.setDataBits(8);
          params.setStopBits(1);
          params.setParity(0);
        ModbusMaster master = factory.createRtuMaster(params);
        // master.setRetries(4);
        master.setTimeout(1000);
        master.setRetries(0);
        long start = System.currentTimeMillis();

        // Don't start if the RTU master can't be initialized.
        try {
            master.init();
        } catch (ModbusInitException e) {
          System.out.println( "Modbus Master Init Error: " + e.getMessage());
          return;
        }


        try {
            System.out.println("Reg. 1001 Value:" + master.getValue(7, RegisterRange.HOLDING_REGISTER, 1000, DataType.FOUR_BYTE_FLOAT_SWAPPED));

            // more like the above until all required register values are read.
            // ..
        }
        finally {
            master.destroy();
        }

        System.out.println("Time elapsed: " + (System.currentTimeMillis() - start) + "ms");
  }
}

</code></pre>
<p dir="auto">Thanks again.</p>
]]></description><link>https://forum.mango-os.com/post/5449</link><guid isPermaLink="true">https://forum.mango-os.com/post/5449</guid><dc:creator><![CDATA[whooper]]></dc:creator><pubDate>Mon, 21 Feb 2011 15:39:14 GMT</pubDate></item><item><title><![CDATA[Reply to RTU example on Sun, 20 Feb 2011 16:49:08 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto">Is something similar available for Modbus RTU?</p>
</blockquote>
<p dir="auto">Yes. Just use the factory to create an RTU master.</p>
<blockquote>
<p dir="auto">1000 is 1000 (0x03E8) or 1001?</p>
</blockquote>
<p dir="auto">There are multiple ways of creating locators in Modbus4J, but in all cases registers are specified zero-based. If you provide the register range - e.g. "coil" - your register will be 0. If you don't provide the register range (using a different constructor) it will be 1000.</p>
]]></description><link>https://forum.mango-os.com/post/5431</link><guid isPermaLink="true">https://forum.mango-os.com/post/5431</guid><dc:creator><![CDATA[mlohbihler]]></dc:creator><pubDate>Sun, 20 Feb 2011 16:49:08 GMT</pubDate></item></channel></rss>