|
|
@@ -7,8 +7,11 @@ import com.example.bpa.config.DataBus; |
|
|
|
import com.example.bpa.helper.MessageLog; |
|
|
|
import com.licheedev.modbus4android.ModbusCallback; |
|
|
|
import com.licheedev.modbus4android.ModbusParam; |
|
|
|
import com.licheedev.modbus4android.ModbusRespException; |
|
|
|
import com.licheedev.modbus4android.param.TcpParam; |
|
|
|
import com.serotonin.modbus4j.ModbusMaster; |
|
|
|
import com.serotonin.modbus4j.exception.ModbusInitException; |
|
|
|
import com.serotonin.modbus4j.exception.ModbusTransportException; |
|
|
|
import com.serotonin.modbus4j.msg.ReadCoilsResponse; |
|
|
|
import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse; |
|
|
|
import com.serotonin.modbus4j.msg.WriteCoilResponse; |
|
|
@@ -16,6 +19,7 @@ import com.serotonin.modbus4j.msg.WriteRegistersResponse; |
|
|
|
|
|
|
|
import java.nio.ByteBuffer; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
|
|
|
|
|
|
|
|
public class ModbusTcpServer { |
|
|
@@ -117,7 +121,7 @@ public class ModbusTcpServer { |
|
|
|
@Override |
|
|
|
public void onSuccess(ModbusMaster modbusMaster) { |
|
|
|
MessageLog.ShowInfo("设备 " + host + " 连接成功"); |
|
|
|
DataBus.getInstance().PlcIsConnect=true; |
|
|
|
DataBus.getInstance().PlcIsConnect = true; |
|
|
|
//1.数据中心 |
|
|
|
DeviceData.Get().Init(); |
|
|
|
//2.业务线程 |
|
|
@@ -126,7 +130,7 @@ public class ModbusTcpServer { |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable tr) { |
|
|
|
DataBus.getInstance().PlcIsConnect=false; |
|
|
|
DataBus.getInstance().PlcIsConnect = false; |
|
|
|
MessageLog.ShowError("设备 " + host + " 连接失败:" + tr.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
@@ -137,6 +141,91 @@ public class ModbusTcpServer { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private Float BytesToFloat(byte[] buffers, DataFormat df) { |
|
|
|
if (buffers.length == 4) { |
|
|
|
byte[] bytes = new byte[4]; |
|
|
|
if (df == DataFormat.ABCD) { |
|
|
|
bytes[0] = buffers[3]; |
|
|
|
bytes[1] = buffers[2]; |
|
|
|
bytes[2] = buffers[1]; |
|
|
|
bytes[3] = buffers[0]; |
|
|
|
} else if (df == DataFormat.CDAB) { |
|
|
|
bytes[0] = buffers[1]; |
|
|
|
bytes[1] = buffers[0]; |
|
|
|
bytes[2] = buffers[3]; |
|
|
|
bytes[3] = buffers[2]; |
|
|
|
} else if (df == DataFormat.BADC) { |
|
|
|
bytes[0] = buffers[2]; |
|
|
|
bytes[1] = buffers[3]; |
|
|
|
bytes[2] = buffers[0]; |
|
|
|
bytes[3] = buffers[1]; |
|
|
|
} else if (df == DataFormat.DCBA) { |
|
|
|
bytes[0] = buffers[0]; |
|
|
|
bytes[1] = buffers[1]; |
|
|
|
bytes[2] = buffers[2]; |
|
|
|
bytes[3] = buffers[3]; |
|
|
|
} |
|
|
|
return ByteBuffer.wrap(bytes).getFloat(); |
|
|
|
} |
|
|
|
return 0.0f; |
|
|
|
} |
|
|
|
|
|
|
|
private Integer BytesToInt(byte[] buffers, DataFormat df) { |
|
|
|
if (buffers.length == 4) { |
|
|
|
byte[] bytes = new byte[4]; |
|
|
|
if (df == DataFormat.ABCD) { |
|
|
|
bytes[0] = buffers[3]; |
|
|
|
bytes[1] = buffers[2]; |
|
|
|
bytes[2] = buffers[1]; |
|
|
|
bytes[3] = buffers[0]; |
|
|
|
} else if (df == DataFormat.CDAB) { |
|
|
|
bytes[0] = buffers[1]; |
|
|
|
bytes[1] = buffers[0]; |
|
|
|
bytes[2] = buffers[3]; |
|
|
|
bytes[3] = buffers[2]; |
|
|
|
} else if (df == DataFormat.BADC) { |
|
|
|
bytes[0] = buffers[2]; |
|
|
|
bytes[1] = buffers[3]; |
|
|
|
bytes[2] = buffers[0]; |
|
|
|
bytes[3] = buffers[1]; |
|
|
|
} else if (df == DataFormat.DCBA) { |
|
|
|
bytes[0] = buffers[0]; |
|
|
|
bytes[1] = buffers[1]; |
|
|
|
bytes[2] = buffers[2]; |
|
|
|
bytes[3] = buffers[3]; |
|
|
|
} |
|
|
|
return ByteBuffer.wrap(bytes).getInt(); |
|
|
|
} |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
private byte[] IntToByte(int number) { |
|
|
|
int temp = number; |
|
|
|
byte[] b = new byte[4]; |
|
|
|
for (int i = 0; i < b.length; i++) { |
|
|
|
b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位 |
|
|
|
temp = temp >> 8; // 向右移8位 |
|
|
|
} |
|
|
|
return b; |
|
|
|
} |
|
|
|
|
|
|
|
private short[] IntToShorts(int value) { |
|
|
|
short[] res = new short[2]; |
|
|
|
int temp = value; |
|
|
|
byte[] b = new byte[4]; |
|
|
|
for (int i = 0; i < b.length; i++) { |
|
|
|
b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位 |
|
|
|
temp = temp >> 8; // 向右移8位 |
|
|
|
} |
|
|
|
for (int i = 0; i < res.length; i++) { |
|
|
|
short s0 = (short) (b[i * 2] & 0xff);// 最低位 |
|
|
|
short s1 = (short) (b[i * 2 + 1] & 0xff); |
|
|
|
s1 <<= 8; |
|
|
|
res[i] = (short) (s0 | s1); |
|
|
|
} |
|
|
|
return res; |
|
|
|
} |
|
|
|
|
|
|
|
/*** |
|
|
|
*读取整形数据 |
|
|
|
* @param Address the address |
|
|
@@ -147,78 +236,102 @@ public class ModbusTcpServer { |
|
|
|
public void ReadShort(String Address, int length, IReadCallBack<short[]> callback) { |
|
|
|
int add = GetAddress(Address); |
|
|
|
if (add < 0) return; |
|
|
|
ModbusTcpHelper.get().readHoldingRegisters(1, add, length, new ModbusCallback<ReadHoldingRegistersResponse>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(ReadHoldingRegistersResponse readHoldingRegistersResponse) { |
|
|
|
short[] data = readHoldingRegistersResponse.getShortData(); |
|
|
|
if (data.length == length) |
|
|
|
callback.onSuccess(data); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable tr) { |
|
|
|
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFinally() { |
|
|
|
} |
|
|
|
}); |
|
|
|
try { |
|
|
|
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length); |
|
|
|
short[] data = res.getShortData(); |
|
|
|
if (data.length == length) |
|
|
|
callback.onSuccess(data); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void ReadBool(String Address, int length, IReadCallBack<boolean[]> callback) { |
|
|
|
int add = GetAddress(Address); |
|
|
|
if (add < 0) return; |
|
|
|
ModbusTcpHelper.get().readCoil(1, add, length, new ModbusCallback<ReadCoilsResponse>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(ReadCoilsResponse readCoilsResponse) { |
|
|
|
boolean[] data = readCoilsResponse.getBooleanData(); |
|
|
|
boolean[] result = Arrays.copyOfRange(data, 0, length); |
|
|
|
if (result.length == length) |
|
|
|
callback.onSuccess(result); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable tr) { |
|
|
|
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFinally() { |
|
|
|
} |
|
|
|
}); |
|
|
|
try { |
|
|
|
ReadCoilsResponse res = ModbusTcpHelper.get().syncReadCoil(1, add, length); |
|
|
|
boolean[] data = res.getBooleanData(); |
|
|
|
boolean[] result = Arrays.copyOfRange(data, 0, length); |
|
|
|
if (result.length == length) |
|
|
|
callback.onSuccess(result); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void ReadFloat(String Address, int length, IReadCallBack<float[]> callback) { |
|
|
|
int add = GetAddress(Address); |
|
|
|
if (add < 0) return; |
|
|
|
ModbusTcpHelper.get().readHoldingRegisters(1, add, length * 2, new ModbusCallback<ReadHoldingRegistersResponse>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(ReadHoldingRegistersResponse readHoldingRegistersResponse) { |
|
|
|
byte[] data = readHoldingRegistersResponse.getData(); |
|
|
|
float[] tempValues = new float[length]; |
|
|
|
for (int i = 0; i < length; i++) { |
|
|
|
byte[] tempData = new byte[4]; |
|
|
|
for (int m = 0; m < 4; m++) { |
|
|
|
tempData[m] = data[i * 4 + m]; |
|
|
|
} |
|
|
|
tempValues[i] = ByteBuffer.wrap(tempData).getFloat(); |
|
|
|
try { |
|
|
|
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length); |
|
|
|
byte[] data = res.getData(); |
|
|
|
float[] tempValues = new float[length]; |
|
|
|
for (int i = 0; i < length; i++) { |
|
|
|
byte[] tempData = new byte[4]; |
|
|
|
for (int m = 0; m < 4; m++) { |
|
|
|
tempData[m] = data[i * 4 + m]; |
|
|
|
} |
|
|
|
if (tempValues.length == length) |
|
|
|
callback.onSuccess(tempValues); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable tr) { |
|
|
|
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFinally() { |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
tempValues[i] = BytesToFloat(tempData, DataFormat.ABCD); |
|
|
|
} |
|
|
|
if (tempValues.length == length) |
|
|
|
callback.onSuccess(tempValues); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void ReadInt(String Address, int length, IReadCallBack<int[]> callback) { |
|
|
|
int add = GetAddress(Address); |
|
|
|
if (add < 0) return; |
|
|
|
try { |
|
|
|
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length); |
|
|
|
byte[] data = res.getData(); |
|
|
|
int[] tempValues = new int[length]; |
|
|
|
for (int i = 0; i < length; i++) { |
|
|
|
byte[] tempData = new byte[4]; |
|
|
|
for (int m = 0; m < 4; m++) { |
|
|
|
tempData[m] = data[i * 4 + m]; |
|
|
|
} |
|
|
|
tempValues[i] = BytesToInt(tempData, DataFormat.ABCD); |
|
|
|
} |
|
|
|
if (tempValues.length == length) |
|
|
|
callback.onSuccess(tempValues); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void WriteShort(String Address, short Value, IWriteCallBack callback) { |
|
|
@@ -226,59 +339,49 @@ public class ModbusTcpServer { |
|
|
|
if (add < 0) return; |
|
|
|
short[] send = new short[1]; |
|
|
|
send[0] = Value; |
|
|
|
ModbusTcpHelper.get().writeRegisters(1, add, send, new ModbusCallback<WriteRegistersResponse>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(WriteRegistersResponse writeRegistersResponse) { |
|
|
|
MessageLog.ShowInfo("WriteShort onSuccess,Address=" + Address + ",Value=" + Value); |
|
|
|
callback.onSuccess(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable tr) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString()); |
|
|
|
callback.onFailure(tr.toString()); |
|
|
|
try { |
|
|
|
Thread.sleep(3000); |
|
|
|
WriteShort(Address, Value, callback); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFinally() { |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(1, add, send); |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void WriteBool(String Address, boolean Value, IWriteCallBack callback) { |
|
|
|
int add = GetAddress(Address); |
|
|
|
if (add < 0) return; |
|
|
|
ModbusTcpHelper.get().writeCoil(1, add, Value, new ModbusCallback<WriteCoilResponse>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(WriteCoilResponse writeCoilResponse) { |
|
|
|
MessageLog.ShowInfo("WriteBool onSuccess,Address=" + Address + ",Value=" + Value); |
|
|
|
callback.onSuccess(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable tr) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString()); |
|
|
|
callback.onFailure(tr.toString()); |
|
|
|
try { |
|
|
|
Thread.sleep(3000); |
|
|
|
WriteBool(Address, Value, callback); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFinally() { |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteCoil(1, add, Value); |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void WriteFloat(String Address, float Value, IWriteCallBack callback) { |
|
|
@@ -286,30 +389,50 @@ public class ModbusTcpServer { |
|
|
|
if (add < 0) return; |
|
|
|
int intBits = Float.floatToRawIntBits(Value); |
|
|
|
short[] send = new short[]{(short) ((intBits >> 16) & 0xffff), (short) (intBits & 0xffff)}; |
|
|
|
ModbusTcpHelper.get().writeRegisters(1, add, send, new ModbusCallback<WriteRegistersResponse>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(WriteRegistersResponse writeRegistersResponse) { |
|
|
|
MessageLog.ShowInfo("WriteFloat onSuccess,Address=" + Address + ",Value=" + Value); |
|
|
|
callback.onSuccess(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable tr) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString()); |
|
|
|
callback.onFailure(tr.toString()); |
|
|
|
try { |
|
|
|
Thread.sleep(3000); |
|
|
|
WriteFloat(Address, Value, callback); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFinally() { |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(1, add, send); |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
public void WriteInt(String Address, int Value, IWriteCallBack callback) { |
|
|
|
int add = GetAddress(Address); |
|
|
|
if (add < 0) return; |
|
|
|
short[] send = IntToShorts(Value); |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(1, add, send); |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void WriteShort(String Address, short Value) { |
|
|
@@ -317,55 +440,38 @@ public class ModbusTcpServer { |
|
|
|
if (add < 0) return; |
|
|
|
short[] send = new short[1]; |
|
|
|
send[0] = Value; |
|
|
|
ModbusTcpHelper.get().writeRegisters(1, add, send, new ModbusCallback<WriteRegistersResponse>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(WriteRegistersResponse writeRegistersResponse) { |
|
|
|
MessageLog.ShowInfo("WriteShort onSuccess,Address=" + Address + ",Value=" + Value); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable tr) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString()); |
|
|
|
try { |
|
|
|
Thread.sleep(3000); |
|
|
|
WriteShort(Address, Value); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFinally() { |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(1, add, send); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void WriteBool(String Address, boolean Value) { |
|
|
|
int add = GetAddress(Address); |
|
|
|
if (add < 0) return; |
|
|
|
ModbusTcpHelper.get().writeCoil(1, add, Value, new ModbusCallback<WriteCoilResponse>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(WriteCoilResponse writeCoilResponse) { |
|
|
|
MessageLog.ShowInfo("WriteBool onSuccess,Address=" + Address + ",Value=" + Value); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable tr) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString()); |
|
|
|
try { |
|
|
|
Thread.sleep(3000); |
|
|
|
WriteBool(Address, Value); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFinally() { |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteCoil(1, add, Value); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void WriteFloat(String Address, float Value) { |
|
|
@@ -373,28 +479,38 @@ public class ModbusTcpServer { |
|
|
|
if (add < 0) return; |
|
|
|
int intBits = Float.floatToRawIntBits(Value); |
|
|
|
short[] send = new short[]{(short) ((intBits >> 16) & 0xffff), (short) (intBits & 0xffff)}; |
|
|
|
ModbusTcpHelper.get().writeRegisters(1, add, send, new ModbusCallback<WriteRegistersResponse>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(WriteRegistersResponse writeRegistersResponse) { |
|
|
|
MessageLog.ShowInfo("WriteFloat onSuccess,Address=" + Address + ",Value=" + Value); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable tr) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString()); |
|
|
|
try { |
|
|
|
Thread.sleep(3000); |
|
|
|
WriteFloat(Address, Value); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFinally() { |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(1, add, send); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
public void WriteInt(String Address, int Value) { |
|
|
|
int add = GetAddress(Address); |
|
|
|
if (add < 0) return; |
|
|
|
short[] send = IntToShorts(Value); |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(1, add, send); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |