|
|
@@ -0,0 +1,632 @@ |
|
|
|
package com.bonait.bnframework.common.modbus; |
|
|
|
|
|
|
|
import com.bonait.bnframework.business.ConfigData; |
|
|
|
import com.bonait.bnframework.common.constant.ConfigName; |
|
|
|
import com.bonait.bnframework.common.helper.DataFormat; |
|
|
|
import com.bonait.bnframework.common.helper.I.IReadCallBack; |
|
|
|
import com.bonait.bnframework.common.helper.I.IWriteCallBack; |
|
|
|
import com.bonait.bnframework.common.helper.MessageLog; |
|
|
|
import com.licheedev.modbus4android.ModbusCallback; |
|
|
|
import com.licheedev.modbus4android.ModbusParam; |
|
|
|
import com.licheedev.modbus4android.ModbusRespException; |
|
|
|
import com.licheedev.modbus4android.param.SerialParam; |
|
|
|
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 java.io.UnsupportedEncodingException; |
|
|
|
import java.nio.ByteBuffer; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
|
|
|
|
public class ModbusRTUServer { |
|
|
|
//region 单例模式 |
|
|
|
private static volatile ModbusRTUServer instance = null; |
|
|
|
|
|
|
|
public static ModbusRTUServer get() { |
|
|
|
ModbusRTUServer manager = instance; |
|
|
|
if (manager == null) { |
|
|
|
synchronized (ModbusRTUServer.class) { |
|
|
|
manager = instance; |
|
|
|
if (manager == null) { |
|
|
|
manager = new ModbusRTUServer(); |
|
|
|
instance = manager; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return manager; |
|
|
|
} |
|
|
|
private ModbusRTUServer() { |
|
|
|
} |
|
|
|
//endregion |
|
|
|
|
|
|
|
|
|
|
|
static ModbusParam param; |
|
|
|
|
|
|
|
/**适用于三菱PLC,只有X,Y,M,D区*/ |
|
|
|
public static int GetAddress(String address){ |
|
|
|
if (address == null) return -1; |
|
|
|
address = address.toUpperCase().trim(); |
|
|
|
|
|
|
|
try { |
|
|
|
if (address.length() < 2) return Integer.parseInt(address); |
|
|
|
|
|
|
|
String res = address.substring(1); |
|
|
|
if (res == null) return -1; |
|
|
|
|
|
|
|
int startAddress=Integer.parseInt(res) ; |
|
|
|
//如果是M区地址。 |
|
|
|
if (address.contains("M")) { |
|
|
|
return startAddress + 4000; |
|
|
|
} else if (address.contains("D")) { |
|
|
|
return startAddress; |
|
|
|
}else if(address.contains("X")){ |
|
|
|
return Integer.parseInt(res,8); |
|
|
|
}else if(address.contains("Y")){ |
|
|
|
return Integer.parseInt(res,8)+300; |
|
|
|
}else{ |
|
|
|
return Integer.parseInt(address); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
return -1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** 连接PLC*/ |
|
|
|
public void ConnectPLC() |
|
|
|
{ |
|
|
|
int baudRate=ConfigName.getInstance().SerialBaudRate; |
|
|
|
int dataBits=ConfigName.getInstance().SerialDataBits; |
|
|
|
int parity=ConfigName.getInstance().SerialParity; |
|
|
|
int stopBits=ConfigName.getInstance().SerialStopBits; |
|
|
|
//String com="COM0"; |
|
|
|
|
|
|
|
param = SerialParam.create("/dev/ttyCOM1", baudRate) // 串口地址和波特率 |
|
|
|
.setDataBits(dataBits) // 数据位 |
|
|
|
.setParity(parity) // 校验位 |
|
|
|
.setStopBits(stopBits) // 停止位 |
|
|
|
.setTimeout(3000).setRetries(0); // 不重试 |
|
|
|
|
|
|
|
ModbusTcpHelper.get().init(param, new ModbusCallback<ModbusMaster>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(ModbusMaster modbusMaster) { |
|
|
|
//ToastUtils.info("串口 ["+com+"] 连接成功"); |
|
|
|
|
|
|
|
//MessageLog.ShowInfo("串口 ["+com+"] 连接成功"); |
|
|
|
ConfigName.getInstance().SerialIsConnect = true; |
|
|
|
ConfigData.getInstance().PLC_Init(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable tr) { |
|
|
|
ConfigName.getInstance().SerialIsConnect = false; |
|
|
|
//ToastUtils.info("串口 ["+com+"] 连接失败:" + tr.getMessage()); |
|
|
|
//MessageLog.ShowError("串口 ["+com+"] 连接失败:" + tr.getMessage()); |
|
|
|
} |
|
|
|
@Override |
|
|
|
public void onFinally() { |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//region 数据类型转换 |
|
|
|
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; |
|
|
|
} |
|
|
|
|
|
|
|
public static String GetString(short[] src, int start, int len) throws UnsupportedEncodingException { |
|
|
|
short[] temp = new short[len]; |
|
|
|
for (int i = 0; i < len; i++) { |
|
|
|
temp[i] = src[i + start]; |
|
|
|
} |
|
|
|
byte[] bytesTemp = shorts2Bytes(temp); |
|
|
|
for (int i = 0; i < bytesTemp.length; i++) { |
|
|
|
byte b = bytesTemp[i]; |
|
|
|
} |
|
|
|
String str = new String(bytesTemp, "UTF-8"); |
|
|
|
return str; |
|
|
|
} |
|
|
|
|
|
|
|
public static byte[] shorts2Bytes(short[] data) { |
|
|
|
byte[] byteValue = new byte[data.length * 2]; |
|
|
|
for (int i = 0; i < data.length; i++) { |
|
|
|
byteValue[i * 2] = (byte) (data[i] & 0xff); |
|
|
|
byteValue[i * 2 + 1] = (byte) ((data[i] & 0xff00) >> 8); |
|
|
|
} |
|
|
|
return byteValue; |
|
|
|
} |
|
|
|
//endregion |
|
|
|
|
|
|
|
//region 读取及写入方法 |
|
|
|
|
|
|
|
/**写入字符串*/ |
|
|
|
public void WriteString(int slaveAddress,String Address, int length, IWriteCallBack callback) { |
|
|
|
int add = GetAddress(Address); |
|
|
|
if (add < 0) return; |
|
|
|
short[] send = new short[length]; |
|
|
|
for (short item : send) { |
|
|
|
item = 0; |
|
|
|
} |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, add, send); |
|
|
|
if (callback != null) |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/**写入字符串*/ |
|
|
|
public void WriteString(int slaveAddress,int Address, short[] values, IWriteCallBack callback) { |
|
|
|
if (slaveAddress<0) return; |
|
|
|
if (Address < 0) return; |
|
|
|
if (values ==null || values.length==0) return; |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, values); |
|
|
|
if (callback != null) |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteString onFailure,Address=" + Address + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteString onFailure,Address=" + Address + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteString onFailure,Address=" + Address + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteString onFailure,Address=" + Address + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteString onFailure,Address=" + Address + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**根据地址批量读取16位寄存器*/ |
|
|
|
public void ReadShorts(int slaveAddress,int Address, int length, IReadCallBack<short[]> callback) { |
|
|
|
//int add = GetAddress(Address); |
|
|
|
if (Address < 0) return; |
|
|
|
try { |
|
|
|
ReadHoldingRegistersResponse res = ModbusTcpHelper.get() |
|
|
|
.syncReadHoldingRegisters(slaveAddress, Address, length); |
|
|
|
short[] data = res.getShortData(); |
|
|
|
if (data.length == length) { |
|
|
|
if (callback != null) 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 ReadBools(int slaveAddress,String Address, int length, IReadCallBack<boolean[]> callback) { |
|
|
|
int add = GetAddress(Address); |
|
|
|
if (add < 0) return; |
|
|
|
try { |
|
|
|
ReadCoilsResponse res = ModbusTcpHelper.get() |
|
|
|
.syncReadCoil(slaveAddress, add, length); |
|
|
|
boolean[] data = res.getBooleanData(); |
|
|
|
boolean[] result = Arrays.copyOfRange(data, 0, length); |
|
|
|
if (result.length == length) { |
|
|
|
if (callback != null) 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()); |
|
|
|
}catch (Exception e){ |
|
|
|
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void ReadFloats(int slaveAddress,int Address, int length, IReadCallBack<float[]> callback) { |
|
|
|
//int add = GetAddress(Address); |
|
|
|
if (Address < 0) return; |
|
|
|
try { |
|
|
|
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(slaveAddress, Address, length*2); |
|
|
|
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]; |
|
|
|
} |
|
|
|
tempValues[i] = BytesToFloat(tempData, DataFormat.BADC); |
|
|
|
} |
|
|
|
if (tempValues.length == length) { |
|
|
|
if (callback != null) 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 ReadInts(int slaveAddress,int Address, int length,DataFormat dataFormat, IReadCallBack<int[]> callback) { |
|
|
|
//int add = GetAddress(Address); |
|
|
|
if (Address < 0) return; |
|
|
|
try { |
|
|
|
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(slaveAddress, Address, length * 2); |
|
|
|
byte[] data = res.getData(); |
|
|
|
int[] tempValues = new int[length]; |
|
|
|
if(data.length>=4) |
|
|
|
{ |
|
|
|
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); |
|
|
|
} |
|
|
|
}else |
|
|
|
{ |
|
|
|
MessageLog.ShowError("ReadInt onFailure,Address=" ); |
|
|
|
|
|
|
|
} |
|
|
|
if (callback != null) 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(int slaveAddress,int Address, short Value, IWriteCallBack callback) { |
|
|
|
//int add = GetAddress(Address); |
|
|
|
if (Address < 0) return; |
|
|
|
short[] send = new short[1]; |
|
|
|
send[0] = Value; |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send); |
|
|
|
if (callback != null) |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void WriteShorts(int slaveAddress,int Address, short[] Value, IWriteCallBack callback) { |
|
|
|
//int add = GetAddress(Address); |
|
|
|
if (Address < 0) return; |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, Value); |
|
|
|
if (callback != null) |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void WriteBool(int slaveAddress,int Address, boolean Value, IWriteCallBack callback) { |
|
|
|
//int add = GetAddress(Address); |
|
|
|
if (Address < 0) return; |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteCoil(slaveAddress, Address, Value); |
|
|
|
if (callback != null) |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void WriteBools(int slaveAddress,int Address, boolean[] Value, IWriteCallBack callback) { |
|
|
|
//int add = GetAddress(Address); |
|
|
|
if (Address < 0) return; |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteCoils(slaveAddress, Address, Value); |
|
|
|
if (callback != null) |
|
|
|
callback.onSuccess(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
public void WriteFloat(int slaveAddress,int Address, float Value, IWriteCallBack callback) { |
|
|
|
//int add = GetAddress(Address); |
|
|
|
if (Address < 0) return; |
|
|
|
int intBits = Float.floatToRawIntBits(Value); |
|
|
|
short[] send = IntToShorts(intBits); |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send); |
|
|
|
|
|
|
|
if (callback != null) callback.onSuccess(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void WriteInt(int slaveAddress,int Address, int Value, IWriteCallBack callback) { |
|
|
|
//int add = GetAddress(Address); |
|
|
|
if (Address < 0) return; |
|
|
|
short[] send = IntToShorts(Value); |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send); |
|
|
|
if (callback != null) callback.onSuccess(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusTransportException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusInitException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} catch (ModbusRespException e) { |
|
|
|
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void WriteShort(int slaveAddress,int Address, short Value) { |
|
|
|
//int add = GetAddress(Address); |
|
|
|
if (Address < 0) return; |
|
|
|
short[] send = new short[1]; |
|
|
|
send[0] = Value; |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, 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(int slaveAddress,int Address, boolean Value) { |
|
|
|
//int add = GetAddress(Address); |
|
|
|
if (Address < 0) return; |
|
|
|
|
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteCoil(slaveAddress, Address, 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(int slaveAddress,int Address, float Value) { |
|
|
|
//int add = GetAddress(Address); |
|
|
|
if (Address < 0) return; |
|
|
|
int intBits = Float.floatToRawIntBits(Value); |
|
|
|
short[] send = new short[]{(short) ((intBits >> 16) & 0xffff), (short) (intBits & 0xffff)}; |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, 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(int slaveAddress,int Address, int Value) { |
|
|
|
//int add = GetAddress(Address); |
|
|
|
if (Address < 0) return; |
|
|
|
short[] send = IntToShorts(Value); |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, 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()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//endregion |
|
|
|
} |