fix:opcda协议批量读

This commit is contained in:
2026-03-19 13:32:14 +08:00
parent bb533bf8b2
commit 29b5a0fb82
4 changed files with 290 additions and 11 deletions

View File

@@ -53,6 +53,8 @@ public enum PointTypeFlagEnum {
* 整数
*/
INT((byte) 3, "int", "整数"),
WORD((byte) 8, "word", "整数"),
DWORD((byte) 9, "dword", "整数"),
/**
* 长整数

View File

@@ -86,8 +86,8 @@ public class OpcDaProtocolDriverImpl implements DriverCustomService {
}
@Override
public List<WResponse> batchWrite(DeviceBO device, List<WValue> wValue) {
return List.of();
public List<WResponse> batchWrite(DeviceBO device, List<WValue> wValues) {
return batchWriteValue(device, wValues);
}
/**
@@ -157,6 +157,20 @@ public class OpcDaProtocolDriverImpl implements DriverCustomService {
return res;
}
public List<WResponse> batchWriteValue(DeviceBO device, List<WValue> wValues) {
Server server = getConnector(device);
List<WResponse> list = new ArrayList<>();
for (WValue wValue : wValues) {
try {
Boolean writeOk = writeValue(server, device, wValue);
list.add(new WResponse(writeOk, wValue, writeOk ? "" : "写入失败!"));
} catch (Exception e) {
list.add(new WResponse(false, wValue, e.getMessage()));
}
}
return list;
}
/**
* 获取 OPC DA 服务器中的 Item 对象
* <p>
@@ -228,7 +242,7 @@ public class OpcDaProtocolDriverImpl implements DriverCustomService {
* @return boolean 返回写入操作是否成功
* @throws WritePointException 如果写入位号值时发生异常, 则抛出此异常
*/
private boolean writeValue(Server server, DeviceBO deviceBO, WValue wValue) {
private Boolean writeValue(Server server, DeviceBO deviceBO, WValue wValue) {
try {
Item item = getItem(server, deviceBO, wValue.getPoint());
return writeItem(item, wValue);
@@ -253,7 +267,7 @@ public class OpcDaProtocolDriverImpl implements DriverCustomService {
* @throws JIException 如果与 OPC DA 服务器通信时发生错误, 则抛出此异常
* @throws UnSupportException 如果写入值的数据类型不支持, 则抛出此异常
*/
private boolean writeItem(Item item, WValue wValue) throws JIException {
private Boolean writeItem(Item item, WValue wValue) throws JIException {
PointTypeFlagEnum valueType = PointTypeFlagEnum.ofCode(wValue.getType());
if (Objects.isNull(valueType)) {
throw new CommonException("Unsupported type of " + wValue.getType());
@@ -265,6 +279,8 @@ public class OpcDaProtocolDriverImpl implements DriverCustomService {
short shortValue = wValue.getValueByClass(Short.class);
writeResult = item.write(new JIVariant(shortValue, false));
break;
case DWORD:
case WORD:
case INT:
int intValue = wValue.getValueByClass(Integer.class);
writeResult = item.write(new JIVariant(intValue, false));
@@ -291,6 +307,6 @@ public class OpcDaProtocolDriverImpl implements DriverCustomService {
default:
break;
}
return writeResult > 0;
return writeResult == 0;
}
}