慢 Android 使用线程的 Modbus 连接
Slow Android Modbus Connection using thread
app功能是连接PLC。我在 PLC 上使用 Modbus 服务器,在 Android 应用程序中使用 EasyModbus 客户端。如果应用程序第一次连接失败,大约需要 2-3 分钟才能再次尝试连接。它通常在主线程上 运行,但是当我将它移动到单独的线程时,问题就出现了。
我尝试更改连接超时,但没有结果。
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();
}
日志:
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.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
记住线程的工作原理后,我添加了计时器以每 x 秒执行一次线程:
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run()
{
connect();
}
}, 0, 1000);
app功能是连接PLC。我在 PLC 上使用 Modbus 服务器,在 Android 应用程序中使用 EasyModbus 客户端。如果应用程序第一次连接失败,大约需要 2-3 分钟才能再次尝试连接。它通常在主线程上 运行,但是当我将它移动到单独的线程时,问题就出现了。
我尝试更改连接超时,但没有结果。
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();
}
日志:
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.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
记住线程的工作原理后,我添加了计时器以每 x 秒执行一次线程:
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run()
{
connect();
}
}, 0, 1000);