Browse Source

修改连接状态,重连

桌面小炒机
liup 6 months ago
parent
commit
7c130986e0
5 changed files with 99 additions and 57 deletions
  1. +81
    -18
      app/src/main/java/com/bonait/bnframework/HBL/Communication/Modbus/ModbusMaster.java
  2. +3
    -26
      app/src/main/java/com/bonait/bnframework/ui/activity/MainActivity.java
  3. +12
    -10
      app/src/main/res/layout/activity_rec_process.xml
  4. +1
    -1
      app/src/main/res/values/styles.xml
  5. +2
    -2
      build.gradle

+ 81
- 18
app/src/main/java/com/bonait/bnframework/HBL/Communication/Modbus/ModbusMaster.java View File

@@ -33,12 +33,38 @@ import java.util.concurrent.atomic.AtomicReference;
public class ModbusMaster implements IRead,IWrite ,IModbusMaster{
private final static String TAG = "ModbusMaster==>";
private boolean IsConnected=false;
private boolean LooperReconnect=true;
/**
* 连接类型,1=TCP,2=RUT
*/
private int ConnectionType;

private ModbusWorker mw = new ModbusWorker();

public void DisConnect(){
IsConnected = false;
mw.release(); }
LooperReconnect = false;
mw.release();
mw = null;
}

public void ReConnect(String error){
// if(ConnectionType==1){
// if(error.contains("Connection refused")||error.contains("SocketException")){
// IsConnected=false;
//// TcpConnect(TempIp,TempPort,null);
// }
// }
// else
if (ConnectionType==2){
if(error.contains("Connection refused")){
IsConnected=false;
RtuConnect("/dev/ttyCOM0",9600);
}
}

}


public DataFormat WriteDataFormat= DataFormat.ABCD;

@@ -46,6 +72,8 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

public int SlaveId=1;



@Override
public OperateResult TcpConnect(String ip, int port) {
try{
@@ -95,6 +123,14 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{
@Override
public OperateResult RtuConnect(String com, int baudRate) {
try{
IsConnected = false;
LooperReconnect = true;
if(mw == null){
mw = new ModbusWorker();
}
if(IsConnected){
return OperateResult.CreateSuccess();
}
LogUtils.d("开始连接Modbus RTU" );
RecordManager.getInstance().addLogRecord("设备异常日志","开始连接Modbus RTU");
ModbusParam param= SerialParam.create(com, baudRate) // 串口地址和波特率
@@ -102,27 +138,35 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{
.setParity(0) // 校验位
.setStopBits(1) // 停止位
.setTimeout(500).setRetries(0); // 不重试
IsConnected = false;
mw.init(param, new ModbusCallback<com.serotonin.modbus4j.ModbusMaster>() {
@Override
public void onSuccess(com.serotonin.modbus4j.ModbusMaster modbusMaster) {
IsConnected=true;
LogUtils.d("Modbus RTU 连接成功" );
RecordManager.getInstance().addLogRecord("设备异常日志","Modbus RTU 连接成功");
}
@Override
public void onFailure(Throwable tr) {
LogUtils.d(" RtuConnect onFailure tr="+tr.getMessage());
LogUtils.d("Modbus RTU 连接失败" );
RecordManager.getInstance().addLogRecord("设备异常日志","Modbus RTU 连接失败");
while (!IsConnected && LooperReconnect){
if(mw==null){
return OperateResult.CreateFailed("mw=null");
}
@Override
public void onFinally() {
mw.init(param, new ModbusCallback<com.serotonin.modbus4j.ModbusMaster>() {
@Override
public void onSuccess(com.serotonin.modbus4j.ModbusMaster modbusMaster) {
IsConnected=true;
LogUtils.d("Modbus RTU 连接成功" );
RecordManager.getInstance().addLogRecord("设备异常日志","Modbus RTU 连接成功");
}
@Override
public void onFailure(Throwable tr) {
IsConnected=false;
LogUtils.d(" RtuConnect onFailure tr="+tr.getMessage());
LogUtils.d("Modbus RTU 连接失败" );
RecordManager.getInstance().addLogRecord("设备异常日志","Modbus RTU 连接失败");
}
@Override
public void onFinally() {
LogUtils.d("Modbus RTU onFinally" );
}
});
Thread.sleep(500);
}

}
});
return OperateResult.CreateSuccess();
}catch(Exception ex){
RecordManager.getInstance().addLogRecord("设备异常日志","异常连接Modbus RTU "+ex.getMessage());
return OperateResult.CreateFailed(ex);
}
}
@@ -236,6 +280,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{
new ExceptionServer(address, length, new IExceptionHandling() {
@Override
public void onSuccess(int add) throws InterruptedException, ExecutionException, ModbusTransportException, ModbusInitException, ModbusRespException {
IsConnected = true;
ReadCoilsResponse res = mw.syncReadCoil(SlaveId, add, length);
boolean[] data = res.getBooleanData();
boolean[] result = Arrays.copyOfRange(data, 0, length);
@@ -247,6 +292,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResultT.CreateFailedT(address+",读取异常:"+error));
}
});
@@ -287,6 +333,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResultT.CreateFailedT(address+",读取异常:"+error));
}
});
@@ -328,6 +375,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
if(ConfigName.LOG_TEST){
LogUtils.d(TAG+"ReadShort address="+address+"读操作 失败:"+error);
}
@@ -368,6 +416,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResultT.CreateFailedT(address+",读取异常:"+error));
}
});
@@ -416,6 +465,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResultT.CreateFailedT(address+",读取异常:"+error));
}
});
@@ -443,6 +493,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResultT.CreateFailedT(address+",读取异常:"+error));
}
});
@@ -468,6 +519,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResultT.CreateFailedT(address+",读取异常:"+error));
}
});
@@ -504,6 +556,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{
}
@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResult.CreateFailed(address+",写入异常:"+error));
}
});
@@ -525,6 +578,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResult.CreateFailed(address+",写入异常:"+error));
}
});
@@ -546,6 +600,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResult.CreateFailed(address+",写入异常:"+error));
}
});
@@ -567,6 +622,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{
new ExceptionServer(address, 1, new IExceptionHandling() {
@Override
public void onSuccess(int add) throws InterruptedException, ExecutionException, ModbusTransportException, ModbusInitException, ModbusRespException {
IsConnected = true;
WriteRegistersResponse response = mw.syncWriteRegisters(SlaveId, add, value);
resultValue.set(OperateResult.CreateSuccess());
if(ConfigName.LOG_TEST){
@@ -576,6 +632,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResult.CreateFailed(address+",写入异常:"+error));
}
});
@@ -596,6 +653,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResult.CreateFailed(address+",写入异常:"+error));
}
});
@@ -624,6 +682,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResult.CreateFailed(address+",写入异常:"+error));
}
});
@@ -645,6 +704,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResult.CreateFailed(address+",写入异常:"+error));
}
});
@@ -667,6 +727,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResult.CreateFailed(address+",写入异常:"+error));
}
});
@@ -695,6 +756,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResult.CreateFailed(address+",写入异常:"+error));
}
});
@@ -716,6 +778,7 @@ public class ModbusMaster implements IRead,IWrite ,IModbusMaster{

@Override
public void onFailure(String error){
ReConnect(error);
resultValue.set(OperateResult.CreateFailed(address+",写入异常:"+error));
}
});


+ 3
- 26
app/src/main/java/com/bonait/bnframework/ui/activity/MainActivity.java View File

@@ -2,13 +2,9 @@ package com.bonait.bnframework.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

@@ -41,18 +37,6 @@ public class MainActivity extends BaseActivity {

private HomeFoodsFragment homeFoodsFragment;
private ActivityMainBinding viewBinding;
private Handler handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what == 1){
if(!ModbusHelper.get().getConnected()){
ModbusHelper.get().RtuInit("/dev/ttyCOM0",9600);
}
handler.sendEmptyMessageDelayed(1,3000);
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -83,17 +67,14 @@ public class MainActivity extends BaseActivity {
@Override
protected void onResume() {
super.onResume();
if(handler!=null){
handler.sendEmptyMessageDelayed(1,3000);
}
// if(handler!=null){
// handler.sendEmptyMessageDelayed(1,3000);
// }
}

@Override
protected void onPause() {
super.onPause();
if(handler!=null){
handler.removeCallbacksAndMessages(null);
}
}

private void initView(){
@@ -117,10 +98,6 @@ public class MainActivity extends BaseActivity {

@Override
protected void onDestroy() {
if(handler!=null){
handler.removeCallbacksAndMessages(null);
handler = null;
}
NewExecuteTheRecipe.IsStart = false;
ModbusHelper.get().setCookStatus(false);
ConfigData.getInstance().ColsePLC();


+ 12
- 10
app/src/main/res/layout/activity_rec_process.xml View File

@@ -118,16 +118,7 @@
app:imagesrc_tz="@mipmap/zhizuo_js2" />
</FrameLayout>

<ImageView
android:id="@+id/pause_goodmake"
android:layout_width="@dimen/dp_100"
android:layout_height="@dimen/dp_100"
android:visibility="gone"
tools:visibility="visible"
android:layout_marginEnd="@dimen/dp_25"
android:layout_marginBottom="@dimen/dp_90"
android:layout_gravity="end|bottom"
/>


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleStep"
@@ -142,6 +133,17 @@
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

<ImageView
android:id="@+id/pause_goodmake"
android:layout_width="@dimen/dp_100"
android:layout_height="@dimen/dp_100"
android:visibility="gone"
tools:visibility="visible"
android:layout_marginEnd="@dimen/dp_25"
android:layout_marginBottom="@dimen/dp_90"
android:layout_gravity="end|bottom"
/>

<include
android:layout_width="match_parent"
android:layout_height="match_parent"


+ 1
- 1
app/src/main/res/values/styles.xml View File

@@ -366,7 +366,7 @@

<!-- ActionSheet进出动画 -->
<style name="ActionSheetDialogScale" parent="@android:style/Animation.Dialog">
<item name="android:windowEnterAnimation">@anim/anim_scale_in</item>
<item name="android:windowEnterAnimation">@null</item>
<item name="android:windowExitAnimation">@anim/anim_scale_out</item>
</style>



+ 2
- 2
build.gradle View File

@@ -35,8 +35,8 @@ task clean(type: Delete) {

ext { // 统一版本入口
//App版本号
versionCode = 212
versionName = "2.1.2"
versionCode = 213
versionName = "2.1.3"

// 支持Android版本
buildToolsVersion = "33.0.0"


Loading…
Cancel
Save