fix:opcda协议批量读
This commit is contained in:
@@ -1648,4 +1648,171 @@ public class ApiTest {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void opcDaBatchReadTest() {
|
||||
// 构建OPC DA连接对象
|
||||
JSONObject properties = new JSONObject();
|
||||
properties.put("clsId", "7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // Kepware OPC Server CLSID
|
||||
properties.put("username", "administrator");
|
||||
properties.put("password", "P@ssw0rd.");
|
||||
|
||||
IotConnect connect = IotConnect.builder()
|
||||
.id(4)
|
||||
.code("B_CBJ01")
|
||||
.host("192.168.81.251")
|
||||
.port(135) // OPC DA默认端口
|
||||
.properties(JSONObject.toJSONString(properties))
|
||||
.protocol("opc-da")
|
||||
.enabled(true)
|
||||
.description("测试OPC DA连接")
|
||||
.build();
|
||||
|
||||
// 构建多个配置对象进行批量读取
|
||||
IotConfig config1 = IotConfig.builder()
|
||||
.id(1)
|
||||
.connectId(4)
|
||||
.alias("action1")
|
||||
.aliasName("温度传感器")
|
||||
.registerAddress("Channel1.Device1.Temperature") // OPC DA标签地址格式
|
||||
.dataType("WORD")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试OPC DA温度读取")
|
||||
.build();
|
||||
|
||||
IotConfig config2 = IotConfig.builder()
|
||||
.id(2)
|
||||
.connectId(4)
|
||||
.alias("mode")
|
||||
.aliasName("压力传感器")
|
||||
.registerAddress("Channel1.Device1.Pressure")
|
||||
.dataType("WORD")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试OPC DA压力读取")
|
||||
.build();
|
||||
|
||||
IotConfig config3 = IotConfig.builder()
|
||||
.id(3)
|
||||
.connectId(4)
|
||||
.alias("material1")
|
||||
.aliasName("湿度传感器")
|
||||
.registerAddress("Channel1.Device1.Humidity")
|
||||
.dataType("STRING")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试OPC DA湿度读取")
|
||||
.build();
|
||||
|
||||
// IotConfig config4 = IotConfig.builder()
|
||||
// .id(4)
|
||||
// .connectId(4)
|
||||
// .alias("status")
|
||||
// .aliasName("设备状态")
|
||||
// .registerAddress("Channel1.Device1.Status")
|
||||
// .dataType("BOOLEAN")
|
||||
// .readonly(true)
|
||||
// .enabled(true)
|
||||
// .description("测试OPC DA状态读取")
|
||||
// .build();
|
||||
|
||||
// 执行批量读取操作
|
||||
try {
|
||||
System.out.println("========== 开始测试OPC DA批量读取 ==========");
|
||||
System.out.println("连接信息: " + connect.getHost() + ":" + connect.getPort());
|
||||
System.out.println("CLSID: " + properties.getString("clsId"));
|
||||
System.out.println("用户名: " + properties.getString("username"));
|
||||
|
||||
// 转换为DeviceBO
|
||||
DeviceBO deviceBO = DeviceBO.builder()
|
||||
.id(String.valueOf(connect.getId()))
|
||||
.code(connect.getCode())
|
||||
.properties(connect.getProperties())
|
||||
.host(connect.getHost())
|
||||
.port(connect.getPort())
|
||||
.protocol(connect.getProtocol())
|
||||
.build();
|
||||
|
||||
// 转换为SiteBO列表
|
||||
java.util.List<SiteBO> siteBOList = java.util.Arrays.asList(
|
||||
SiteBO.builder()
|
||||
.deviceCode("B_CBJ01.B_CBJ01")
|
||||
.alias(config1.getAlias())
|
||||
.aliasName(config1.getAliasName())
|
||||
.registerAddress(config1.getRegisterAddress())
|
||||
.dataType(config1.getDataType())
|
||||
.readonly(config1.getReadonly())
|
||||
.build(),
|
||||
SiteBO.builder()
|
||||
.deviceCode("B_CBJ01.B_CBJ01")
|
||||
.alias(config2.getAlias())
|
||||
.aliasName(config2.getAliasName())
|
||||
.registerAddress(config2.getRegisterAddress())
|
||||
.dataType(config2.getDataType())
|
||||
.readonly(config2.getReadonly())
|
||||
.build(),
|
||||
SiteBO.builder()
|
||||
.deviceCode("B_CBJ01.B_CBJ01")
|
||||
.alias(config3.getAlias())
|
||||
.aliasName(config3.getAliasName())
|
||||
.registerAddress(config3.getRegisterAddress())
|
||||
.dataType(config3.getDataType())
|
||||
.readonly(config3.getReadonly())
|
||||
.build()
|
||||
// SiteBO.builder()
|
||||
// .deviceCode(connect.getCode())
|
||||
// .alias(config4.getAlias())
|
||||
// .aliasName(config4.getAliasName())
|
||||
// .registerAddress(config4.getRegisterAddress())
|
||||
// .dataType(config4.getDataType())
|
||||
// .readonly(config4.getReadonly())
|
||||
// .build()
|
||||
);
|
||||
|
||||
System.out.println("批量读取标签数量: " + siteBOList.size());
|
||||
for (SiteBO site : siteBOList) {
|
||||
System.out.println(" - " + site.getAliasName() + " (" + site.getAlias() + "): " +
|
||||
site.getRegisterAddress() + " [" + site.getDataType() + "]");
|
||||
}
|
||||
|
||||
// 调用驱动批量读取数据
|
||||
List<RValue> result = opcDaProtocolDriver.batchRead(deviceBO, siteBOList);
|
||||
|
||||
System.out.println("批量读取完成!");
|
||||
System.out.println("读取结果:");
|
||||
|
||||
// 输出每个标签的读取结果
|
||||
for (RValue rValue : result) {
|
||||
SiteBO site = rValue.getSiteBO();
|
||||
String value = rValue.getValue();
|
||||
String exceptionMsg = rValue.getExceptionMessage();
|
||||
|
||||
if (site != null) {
|
||||
System.out.println(" - " + site.getAliasName() + " (" + site.getAlias() + "): " +
|
||||
(value != null ? value : "null") +
|
||||
(exceptionMsg != null && !exceptionMsg.isEmpty() ? " [" + exceptionMsg + "]" : ""));
|
||||
} else {
|
||||
System.out.println(" - 未知标签: " +
|
||||
(value != null ? value : "null") +
|
||||
(exceptionMsg != null && !exceptionMsg.isEmpty() ? " [" + exceptionMsg + "]" : ""));
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("========== OPC DA批量读取测试完成 ==========");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("OPC DA批量读取测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
|
||||
// 提供故障排除建议
|
||||
System.err.println("\n========== 故障排除建议 ==========");
|
||||
System.err.println("1. 检查OPC DA服务器是否正在运行");
|
||||
System.err.println("2. 验证CLSID是否正确: " + properties.getString("clsId"));
|
||||
System.err.println("3. 确认标签地址格式是否正确");
|
||||
System.err.println("4. 检查DCOM配置和防火墙设置");
|
||||
System.err.println("5. 验证用户权限和密码");
|
||||
System.err.println("6. 建议先使用OPC客户端工具(如Matrikon OPC Explorer)测试连接");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user