|
|
@@ -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)); |
|
|
|
} |
|
|
|
}); |
|
|
|