|
|
@@ -21,10 +21,12 @@ import com.serotonin.modbus4j.exception.ModbusTransportException; |
|
|
|
import com.serotonin.modbus4j.msg.ReadCoilsResponse; |
|
|
|
import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
import java.nio.ByteBuffer; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
|
|
public class ModbusTcpServer { |
|
|
@@ -66,8 +68,7 @@ public class ModbusTcpServer { |
|
|
|
return -1; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else if (address.toUpperCase().contains("I") && address.length() >= 4) { |
|
|
|
} else if (address.toUpperCase().contains("I") && address.length() >= 4) { |
|
|
|
String[] res = address.substring(1).split("[.]"); |
|
|
|
if (res != null && res.length == 2) { |
|
|
|
try { |
|
|
@@ -80,8 +81,7 @@ public class ModbusTcpServer { |
|
|
|
return -1; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else if ((address.toUpperCase().contains("VW") || address.toUpperCase().contains("VD")) && address.length() >= 3) { |
|
|
|
} else if ((address.toUpperCase().contains("VW") || address.toUpperCase().contains("VD")) && address.length() >= 3) { |
|
|
|
String res = address.substring(2); |
|
|
|
if (res != null) { |
|
|
|
try { |
|
|
@@ -132,6 +132,20 @@ public class ModbusTcpServer { |
|
|
|
* @param port 设备端口号 |
|
|
|
*/ |
|
|
|
public void Connect(String host, int port) { |
|
|
|
|
|
|
|
int status = 1; |
|
|
|
while (status == 1) { |
|
|
|
try { |
|
|
|
//status为0则代表通,为1则代表不通。 |
|
|
|
status = Runtime.getRuntime().exec("ping " + host).waitFor(); |
|
|
|
if (status == 1) Thread.sleep(1000); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowInfo("设备 " + host + " 网络验证失败"); |
|
|
|
} catch (IOException e) { |
|
|
|
MessageLog.ShowInfo("设备 " + host + " 网络验证失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
param = TcpParam.create(host, port) |
|
|
|
.setTimeout(1000) |
|
|
|
.setRetries(0) |
|
|
@@ -148,7 +162,7 @@ public class ModbusTcpServer { |
|
|
|
BusinessServer.Get().Init(); |
|
|
|
//3.设置自动模式 |
|
|
|
DeviceData.Get().setHandOrAutoSwitch(true, null); |
|
|
|
DeviceData.Get().setCleaningMode(false,null);//关闭自动清洗模式 |
|
|
|
DeviceData.Get().setCleaningMode(false, null);//关闭自动清洗模式 |
|
|
|
//4.启动设备服务 |
|
|
|
DeviceServer.Get().Init(); |
|
|
|
//5.启动日志服务 |
|
|
@@ -259,11 +273,9 @@ public class ModbusTcpServer { |
|
|
|
return res; |
|
|
|
} |
|
|
|
|
|
|
|
public static String GetString(short[] src, int start, int len) throws UnsupportedEncodingException |
|
|
|
{ |
|
|
|
public static String GetString(short[] src, int start, int len) throws UnsupportedEncodingException { |
|
|
|
short[] temp = new short[len]; |
|
|
|
for (int i = 0; i < len; i++) |
|
|
|
{ |
|
|
|
for (int i = 0; i < len; i++) { |
|
|
|
temp[i] = src[i + start]; |
|
|
|
} |
|
|
|
byte[] bytesTemp = shorts2Bytes(temp); |
|
|
@@ -274,7 +286,7 @@ public class ModbusTcpServer { |
|
|
|
return str; |
|
|
|
} |
|
|
|
|
|
|
|
public static byte [] shorts2Bytes(short [] data){ |
|
|
|
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); |
|
|
@@ -295,14 +307,14 @@ public class ModbusTcpServer { |
|
|
|
try { |
|
|
|
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length); |
|
|
|
byte[] data = res.getData(); |
|
|
|
byte[] tempData=new byte[6]; |
|
|
|
tempData[0]=data[1]; |
|
|
|
tempData[1]=data[0]; |
|
|
|
tempData[2]=data[3]; |
|
|
|
tempData[3]=data[2]; |
|
|
|
tempData[4]=data[5]; |
|
|
|
tempData[5]=data[4]; |
|
|
|
if(callback!=null) callback.onSuccess(tempData); |
|
|
|
byte[] tempData = new byte[6]; |
|
|
|
tempData[0] = data[1]; |
|
|
|
tempData[1] = data[0]; |
|
|
|
tempData[2] = data[3]; |
|
|
|
tempData[3] = data[2]; |
|
|
|
tempData[4] = data[5]; |
|
|
|
tempData[5] = data[4]; |
|
|
|
if (callback != null) callback.onSuccess(tempData); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
@@ -315,6 +327,7 @@ public class ModbusTcpServer { |
|
|
|
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*** |
|
|
|
*读取扫码数据 |
|
|
|
* @param Address the address |
|
|
@@ -327,26 +340,23 @@ public class ModbusTcpServer { |
|
|
|
try { |
|
|
|
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length); |
|
|
|
byte[] data = res.getData(); |
|
|
|
byte[] data1= Arrays.copyOfRange(data, 0, 18); |
|
|
|
byte[] data1 = Arrays.copyOfRange(data, 0, 18); |
|
|
|
//36 GUID 18 Number |
|
|
|
String id=""; |
|
|
|
String id = ""; |
|
|
|
try { |
|
|
|
byte[] tempdata=new byte[18]; |
|
|
|
for (int i=0;i<data1.length;i++) |
|
|
|
{ |
|
|
|
if(i%2==0) |
|
|
|
{ |
|
|
|
|
|
|
|
tempdata[i+1]=data1[i]; |
|
|
|
}else |
|
|
|
{ |
|
|
|
tempdata[i-1]=data1[i]; |
|
|
|
byte[] tempdata = new byte[18]; |
|
|
|
for (int i = 0; i < data1.length; i++) { |
|
|
|
if (i % 2 == 0) { |
|
|
|
|
|
|
|
tempdata[i + 1] = data1[i]; |
|
|
|
} else { |
|
|
|
tempdata[i - 1] = data1[i]; |
|
|
|
} |
|
|
|
} |
|
|
|
id = new String(tempdata, "UTF-8").trim(); |
|
|
|
} catch (UnsupportedEncodingException ex) { |
|
|
|
} |
|
|
|
if(callback!=null) callback.onSuccess(id); |
|
|
|
if (callback != null) callback.onSuccess(id); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
|
} catch (ExecutionException e) { |
|
|
@@ -364,39 +374,40 @@ public class ModbusTcpServer { |
|
|
|
int add = GetAddress(Address); |
|
|
|
if (add < 0) return; |
|
|
|
short[] send = new short[length]; |
|
|
|
for (short item:send) |
|
|
|
{ |
|
|
|
item=0; |
|
|
|
for (short item : send) { |
|
|
|
item = 0; |
|
|
|
} |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(1, add, send); |
|
|
|
if(callback!=null) |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void ReadShort(String Address, int length, IReadCallBack<short[]> callback) { |
|
|
|
int add = GetAddress(Address); |
|
|
|
if (add < 0) return; |
|
|
|
try { |
|
|
|
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length); |
|
|
|
short[] data = res.getShortData(); |
|
|
|
if (data.length == length){ |
|
|
|
if(callback!=null) callback.onSuccess(data);} |
|
|
|
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) { |
|
|
@@ -417,9 +428,8 @@ public class ModbusTcpServer { |
|
|
|
ReadCoilsResponse res = ModbusTcpHelper.get().syncReadCoil(1, add, length); |
|
|
|
boolean[] data = res.getBooleanData(); |
|
|
|
boolean[] result = Arrays.copyOfRange(data, 0, length); |
|
|
|
if (result.length == length) |
|
|
|
{ |
|
|
|
if(callback!=null)callback.onSuccess(result); |
|
|
|
if (result.length == length) { |
|
|
|
if (callback != null) callback.onSuccess(result); |
|
|
|
} |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
@@ -448,9 +458,8 @@ public class ModbusTcpServer { |
|
|
|
} |
|
|
|
tempValues[i] = BytesToFloat(tempData, DataFormat.ABCD); |
|
|
|
} |
|
|
|
if (tempValues.length == length) |
|
|
|
{ |
|
|
|
if(callback!=null)callback.onSuccess(tempValues); |
|
|
|
if (tempValues.length == length) { |
|
|
|
if (callback != null) callback.onSuccess(tempValues); |
|
|
|
} |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
@@ -469,7 +478,7 @@ public class ModbusTcpServer { |
|
|
|
int add = GetAddress(Address); |
|
|
|
if (add < 0) return; |
|
|
|
try { |
|
|
|
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length*2); |
|
|
|
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length * 2); |
|
|
|
byte[] data = res.getData(); |
|
|
|
int[] tempValues = new int[length]; |
|
|
|
for (int i = 0; i < length; i++) { |
|
|
@@ -485,9 +494,8 @@ public class ModbusTcpServer { |
|
|
|
tempValues[i] = BytesToInt(tempData, DataFormat.BADC); |
|
|
|
|
|
|
|
} |
|
|
|
if (tempValues.length == length) |
|
|
|
{ |
|
|
|
if(callback!=null)callback.onSuccess(tempValues); |
|
|
|
if (tempValues.length == length) { |
|
|
|
if (callback != null) callback.onSuccess(tempValues); |
|
|
|
} |
|
|
|
} catch (InterruptedException e) { |
|
|
|
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); |
|
|
@@ -509,23 +517,23 @@ public class ModbusTcpServer { |
|
|
|
send[0] = Value; |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(1, add, send); |
|
|
|
if(callback!=null) |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@@ -534,23 +542,23 @@ public class ModbusTcpServer { |
|
|
|
if (add < 0) return; |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteCoil(1, add, Value); |
|
|
|
if (callback!=null) |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@@ -562,22 +570,22 @@ public class ModbusTcpServer { |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(1, add, send); |
|
|
|
|
|
|
|
if(callback!=null)callback.onSuccess(); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@@ -587,22 +595,22 @@ public class ModbusTcpServer { |
|
|
|
short[] send = IntToShorts(Value); |
|
|
|
try { |
|
|
|
ModbusTcpHelper.get().syncWriteRegisters(1, add, send); |
|
|
|
if(callback!=null)callback.onSuccess(); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
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()); |
|
|
|
if (callback != null) callback.onFailure(e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|