diff --git a/app/src/main/java/com/example/bpa/MainActivity.java b/app/src/main/java/com/example/bpa/MainActivity.java index 5ee5af3..13225c4 100644 --- a/app/src/main/java/com/example/bpa/MainActivity.java +++ b/app/src/main/java/com/example/bpa/MainActivity.java @@ -163,7 +163,7 @@ public class MainActivity extends FragmentActivity implements View.OnClickListen } } }).start(); - //ModbusTcpServer.get().ReconnectModbus(); + ModbusTcpServer.get().ReconnectModbus(); //MQTT数据监听 OrderServer.Get().MqttInit(); //消息中心 diff --git a/app/src/main/java/com/example/bpa/helper/ModbusTcpServer.java b/app/src/main/java/com/example/bpa/helper/ModbusTcpServer.java index ea25a98..8aeb7b0 100644 --- a/app/src/main/java/com/example/bpa/helper/ModbusTcpServer.java +++ b/app/src/main/java/com/example/bpa/helper/ModbusTcpServer.java @@ -4,6 +4,7 @@ package com.example.bpa.helper; import android.util.Log; import com.example.bpa.Model.IReadCallBack; +import com.example.bpa.Model.IThread; import com.example.bpa.Model.IWriteCallBack; import com.example.bpa.config.ConfigName; import com.example.bpa.service.BusinessServer; @@ -252,25 +253,23 @@ public class ModbusTcpServer { { try { - //断线重新连接 - // 必须开启新的线程执行 - new Thread(new Runnable() { + ThreadManager.Get().StartLong("PLC断线重连线程", true, new IThread() { @Override - public void run() { + public void Run() throws InterruptedException { try { - while (true) - { - ModbusMaster master= ModbusTcpHelper.get().getModbusMaster(); - if (master==null || master.isConnected() == false) { - ConnectPLC(); - } - Thread.sleep(20000); + ModbusMaster master= ModbusTcpHelper.get().getModbusMaster(); + if (master==null || master.isConnected() == false) { + ConnectPLC(); } + Thread.sleep(20000); } catch (Exception e) { Log.i("PLC", "PLC重连接失败!"+e.getMessage()); } } - }).start(); + @Override + public void RunComplete() throws InterruptedException { + } + }); }catch (Exception e) { MessageLog.ShowInfo("重新连接Modbus异常," +e.getMessage()); }