修改直接读取PLC地址值的实现方法,修复BUG
This commit is contained in:
parent
b29d950d1e
commit
b62fb0e0cd
|
|
@ -94,7 +94,8 @@ public interface IPlcCommunication {
|
|||
/**
|
||||
* 读取指定名称的值 ---- 仅适用于 Siemens PLC DB表里的数据
|
||||
* @param name 名称
|
||||
* @param clazz 值类型
|
||||
* @return 值
|
||||
*/
|
||||
<T> Tuple2<String, Object> readWithName(String name);
|
||||
<T> Tuple2<String, T> readWithName(String name, Class<T> clazz);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -569,12 +569,13 @@ public class PlcSiemensCommunication implements IPlcCommunication {
|
|||
/**
|
||||
* 读取值从指定的DB地址名称
|
||||
* @param name 数据库名称
|
||||
* @param clazz 数据类型
|
||||
* @return 读取结果
|
||||
*/
|
||||
@Override
|
||||
public <T> Tuple2<String, Object> readWithName(String name) {
|
||||
public <T> Tuple2<String, T> readWithName(String name, Class<T> clazz) {
|
||||
// 获取T类型
|
||||
Type[] actualTypeArguments = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments();
|
||||
Type[] actualTypeArguments = ((ParameterizedType) clazz.getGenericSuperclass()).getActualTypeArguments();
|
||||
if(actualTypeArguments.length < 1) {
|
||||
return new Tuple2<>("数据类型异常", null);
|
||||
}
|
||||
|
|
@ -582,51 +583,65 @@ public class PlcSiemensCommunication implements IPlcCommunication {
|
|||
if (!(type instanceof Class)) {
|
||||
return new Tuple2<>("不支持的复杂泛型类型", null);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<T> tClass = (Class<T>) type;
|
||||
Tuple3<String, String, SiemensS7Net> plcData = getDbPlcData(name);
|
||||
if(AppStringUtils.isNotEmpty(plcData.item1)) {
|
||||
return new Tuple2<>(plcData.item1, null);
|
||||
}
|
||||
SiemensS7Net siemensS7Net = plcData.item3;
|
||||
if(tClass == Integer.class) {
|
||||
// 直接使用传入的clazz进行类型判断
|
||||
if(clazz == Integer.class) {
|
||||
OperateResultExOne<Integer> operateResult = siemensS7Net.ReadInt32(plcData.item2);
|
||||
if(!operateResult.IsSuccess) {
|
||||
return new Tuple2<>(operateResult.Message, null);
|
||||
}
|
||||
return new Tuple2<>(null, operateResult.Content);
|
||||
@SuppressWarnings("unchecked")
|
||||
T result = (T) operateResult.Content;
|
||||
return new Tuple2<>(null, result);
|
||||
}
|
||||
if(tClass == Long.class) {
|
||||
if(clazz == Long.class) {
|
||||
OperateResultExOne<Long> operateResult = siemensS7Net.ReadInt64(plcData.item2);
|
||||
if(!operateResult.IsSuccess) {
|
||||
return new Tuple2<>(operateResult.Message, null);
|
||||
}
|
||||
return new Tuple2<>(null, operateResult.Content);
|
||||
@SuppressWarnings("unchecked")
|
||||
T result = (T) operateResult.Content;
|
||||
return new Tuple2<>(null, result);
|
||||
}
|
||||
if(tClass == Short.class) {
|
||||
if(clazz == Short.class) {
|
||||
OperateResultExOne<Short> operateResult = siemensS7Net.ReadInt16(plcData.item2);
|
||||
if(!operateResult.IsSuccess) {
|
||||
return new Tuple2<>(operateResult.Message, null);
|
||||
}
|
||||
return new Tuple2<>(null, operateResult.Content);
|
||||
@SuppressWarnings("unchecked")
|
||||
T result = (T) operateResult.Content;
|
||||
return new Tuple2<>(null, result);
|
||||
}
|
||||
if(tClass == Byte.class) {
|
||||
if(clazz == Byte.class) {
|
||||
OperateResultExOne<Byte> operateResult = siemensS7Net.ReadByte(plcData.item2);
|
||||
if(!operateResult.IsSuccess) {
|
||||
return new Tuple2<>(operateResult.Message, null);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
T result = (T) operateResult.Content;
|
||||
return new Tuple2<>(null, result);
|
||||
}
|
||||
if(tClass == Double.class) {
|
||||
if(clazz == Double.class) {
|
||||
OperateResultExOne<Double> operateResult = siemensS7Net.ReadDouble(plcData.item2);
|
||||
if(!operateResult.IsSuccess) {
|
||||
return new Tuple2<>(operateResult.Message, null);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
T result = (T) operateResult.Content;
|
||||
return new Tuple2<>(null, result);
|
||||
}
|
||||
if(tClass == Float.class) {
|
||||
if(clazz == Float.class) {
|
||||
OperateResultExOne<Float> operateResult = siemensS7Net.ReadFloat(plcData.item2);
|
||||
if(!operateResult.IsSuccess) {
|
||||
return new Tuple2<>(operateResult.Message, null);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
T result = (T) operateResult.Content;
|
||||
return new Tuple2<>(null, result);
|
||||
}
|
||||
return new Tuple2<>("不支持的数据类型", null);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user