Search code examples
androidmultithreadingmodbus

Slow Android Modbus Connection using thread


The app function is to connect with a PLC. I am using Modbus server on the PLC and EasyModbus Client in the Android App. If app fail to connect the first time, it takes about 2-3 minutes to try to connect again. It is running normally on Main thread, but when I move it to separate thread the problem occurs.

I tried changing the connection timeout, but without a result.

public void connect() {

Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                while(!connected) {
                    ModbusClient modbusClient = new ModbusClient("192.168.0.1", 502);
                    modbusClient.setConnectionTimeout(30);

                    try {
                        Log.e(TAG, "Try connecting to port: " + 502;
                        modbusClient.Connect();
                        connected = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        thread.start();
}

Log:

Try connecting to port: 502
W/System: A resource failed to call close. 
W/System.err: java.net.ConnectException: failed to connect to /192.168.21.136 (port 1201) from /:: (port 46369): connect failed: ETIMEDOUT (Connection timed out)
W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:142)
W/System.err:     at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
W/System.err:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
        at java.net.Socket.connect(Socket.java:621)
W/System.err:     at java.net.Socket.connect(Socket.java:570)
W/System.err:     at java.net.Socket.<init>(Socket.java:450)
W/System.err:     at java.net.Socket.<init>(Socket.java:218)
W/System.err:     at de.re.easymodbus.modbusclient.ModbusClient.Connect(ModbusClient.java:88)
W/System.err:     at com.example.roseoilproduction.MainActivity$1.run(MainActivity.java:101)
W/System.err:     at java.lang.Thread.run(Thread.java:923)
W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
W/System.err:     at libcore.io.Linux.connect(Native Method)
W/System.err:     at libcore.io.ForwardingOs.connect(ForwardingOs.java:94)
        at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:138)
W/System.err:     at libcore.io.ForwardingOs.connect(ForwardingOs.java:94)
        at libcore.io.IoBridge.connectErrno(IoBridge.java:156)
        at libcore.io.IoBridge.connect(IoBridge.java:134)
        ... 12 more


Solution

  • After remembering how thread works I just added timer to execute the thread every x seconds:

    Timer timer = new Timer();
            timer.schedule(new TimerTask()
            {
                @Override
                public void run()
                {
                    connect();
                }
            }, 0, 1000);