<?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[Feature Request: run the main loop of start() method of TcpSlave&#x2F;UdpSlave in another thread]]></title><description><![CDATA[<p dir="auto">Thanks for you guys' easy-of-use Modbus implementation at the beginning!</p>
<p dir="auto">Recently I have been developing a web system used to test Modbus (TCP) devices. What I want to implement now is to offer system users an API to create some Modbus slaves running on the server.<br />
However, when I want to return whether the new slave is started successfully (IMO, main loop start can means success) to the frontend, it turns out that <code>tcpSlave.start()</code> won't end until an exception happened. If I run this method in a new thread, still I cannot detect whether the slave is started successfully or the port has been occupied. Ways like setting a timeout are not elegant IMO. I think if the main loop of this method is run in a new thread instead of the main thread calling the method, it will be much more convenient for me to use this method.</p>
<p dir="auto">Regarding code:</p>
<pre><code>    /** {@inheritDoc} */
    @Override
    public void start() throws ModbusInitException {
        try {
            serverSocket = new ServerSocket(port);

            Socket socket;
            while (true) {
                socket = serverSocket.accept();
                TcpConnectionHandler handler = new TcpConnectionHandler(socket);
                executorService.execute(handler);
                synchronized (listConnections) {
                    listConnections.add(handler);
                }
            }
        }
        catch (IOException e) {
            throw new ModbusInitException(e);
        }
    }
</code></pre>
]]></description><link>https://forum.mango-os.com/topic/5767/feature-request-run-the-main-loop-of-start-method-of-tcpslave-udpslave-in-another-thread</link><generator>RSS for Node</generator><lastBuildDate>Wed, 20 May 2026 04:57:46 GMT</lastBuildDate><atom:link href="https://forum.mango-os.com/topic/5767.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 19 Aug 2023 04:17:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Feature Request: run the main loop of start() method of TcpSlave&#x2F;UdpSlave in another thread on Wed, 23 Aug 2023 18:06:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/gbccccc" aria-label="Profile: gbccccc">@<bdi>gbccccc</bdi></a> first you need to define what "new slave is started successfully means".</p>
<p dir="auto">If just binding to the port and waiting for connections is enough then you can assume that if you call the method and a <code>ModbusInitException</code> is thrown then it is not running.  Otherwise it is bound to the port and waiting for connections.</p>
<p dir="auto">If you want to know that something is connected you could extend the TcpSlave class like this:</p>
<pre><code>public class MonitoredTcpSlave extends TcpSlave {

    public MonitoredTcpSlave(int port, boolean encapsulated) {
        super(port, encapsulated);
    }

    public boolean hasConnection() {
        return !listConnections.isEmpty();
    }
}
</code></pre>
<p dir="auto">As a last resort you could create your own TcpSlave by extending <code>com.serotonin.modbus4j.ModbusSlaveSet.ModbusSlaveSet</code></p>
]]></description><link>https://forum.mango-os.com/post/28001</link><guid isPermaLink="true">https://forum.mango-os.com/post/28001</guid><dc:creator><![CDATA[terrypacker]]></dc:creator><pubDate>Wed, 23 Aug 2023 18:06:26 GMT</pubDate></item></channel></rss>