feat:opcda协议读写
This commit is contained in:
@@ -9,6 +9,7 @@ import org.nl.iot.core.driver.entity.RValue;
|
||||
import org.nl.iot.core.driver.entity.WResponse;
|
||||
import org.nl.iot.core.driver.entity.WValue;
|
||||
import org.nl.iot.core.driver.protocol.modbustcp.ModBusProtocolDriverImpl;
|
||||
import org.nl.iot.core.driver.protocol.opcda.OpcDaProtocolDriverImpl;
|
||||
import org.nl.iot.core.driver.protocol.opcua.OpcUaProtocolDriverImpl;
|
||||
import org.nl.iot.core.driver.protocol.plcs7.PlcS7ProtocolDriverImpl;
|
||||
import org.nl.iot.modular.iot.entity.IotConfig;
|
||||
@@ -38,6 +39,9 @@ public class ApiTest {
|
||||
@Autowired
|
||||
private OpcUaProtocolDriverImpl opcUaProtocolDriver;
|
||||
|
||||
@Autowired
|
||||
private OpcDaProtocolDriverImpl opcDaProtocolDriver;
|
||||
|
||||
@Test
|
||||
public void modbusTest() {
|
||||
// 构建连接对象
|
||||
@@ -1473,5 +1477,163 @@ public class ApiTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void opcDaReadTest() {
|
||||
// 构建OPC DA连接对象
|
||||
JSONObject properties = new JSONObject();
|
||||
properties.put("clsId", "{F8582CF2-88FB-11D0-B850-00C0F0104305}"); // 示例CLSID,需要根据实际OPC服务器修改
|
||||
properties.put("username", "administrator");
|
||||
properties.put("password", "P@ssw0rd.");
|
||||
|
||||
IotConnect connect = IotConnect.builder()
|
||||
.id(3)
|
||||
.code("B_CBJ01")
|
||||
.host("192.168.81.251")
|
||||
.port(0) // OPC DA不使用端口
|
||||
.properties(JSONObject.toJSONString(properties))
|
||||
.protocol("opc-da")
|
||||
.enabled(true)
|
||||
.description("测试OPC DA连接")
|
||||
.build();
|
||||
|
||||
// 构建配置对象 - 写入操作,设置为可写
|
||||
IotConfig config = IotConfig.builder()
|
||||
.id(2)
|
||||
.connectId(3)
|
||||
.alias("action1")
|
||||
.aliasName("标签2")
|
||||
.registerAddress("ns=4;s=|var|HCFA-PLC.Application.B_CBJ01.action1") // OPC DA标签地址
|
||||
.dataType("WORD")
|
||||
.readonly(false) // 设置为可写
|
||||
.enabled(true)
|
||||
.description("测试OPC DA写入")
|
||||
.build();
|
||||
|
||||
// 执行读取操作
|
||||
try {
|
||||
System.out.println("========== 开始测试OPC DA读取 ==========");
|
||||
System.out.println("连接信息: " + connect.getHost());
|
||||
System.out.println("CLSID: " + properties.getString("clsId"));
|
||||
System.out.println("标签地址: " + config.getRegisterAddress());
|
||||
System.out.println("数据类型: " + config.getDataType());
|
||||
|
||||
// 转换为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
|
||||
SiteBO siteBO = SiteBO.builder()
|
||||
.deviceCode("B_CBJ01")
|
||||
.alias(config.getAlias())
|
||||
.aliasName(config.getAliasName())
|
||||
.registerAddress(config.getRegisterAddress())
|
||||
.dataType(config.getDataType())
|
||||
.readonly(config.getReadonly())
|
||||
.build();
|
||||
|
||||
// 调用驱动读取数据
|
||||
RValue result = opcDaProtocolDriver.read(deviceBO, siteBO);
|
||||
|
||||
System.out.println("读取成功!");
|
||||
System.out.println("读取结果: " + result.getValue());
|
||||
System.out.println("========== OPC DA读取测试完成 ==========");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("OPC DA读取测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void opcDaWriteTest() {
|
||||
// 构建OPC DA连接对象
|
||||
JSONObject properties = new JSONObject();
|
||||
properties.put("clsId", "{F8582CF2-88FB-11D0-B850-00C0F0104305}"); // 示例CLSID,需要根据实际OPC服务器修改
|
||||
properties.put("username", "administrator");
|
||||
properties.put("password", "P@ssw0rd.");
|
||||
|
||||
IotConnect connect = IotConnect.builder()
|
||||
.id(3)
|
||||
.code("OPC_DA_001")
|
||||
.host("192.168.81.251")
|
||||
.port(0) // OPC DA不使用端口
|
||||
.properties(JSONObject.toJSONString(properties))
|
||||
.protocol("opc-da")
|
||||
.enabled(true)
|
||||
.description("测试OPC DA连接")
|
||||
.build();
|
||||
|
||||
// 构建配置对象 - 写入操作,设置为可写
|
||||
IotConfig config = IotConfig.builder()
|
||||
.id(2)
|
||||
.connectId(3)
|
||||
.alias("tag2")
|
||||
.aliasName("标签2")
|
||||
.registerAddress("B_CBJ01.B_CBJ01") // OPC DA标签地址
|
||||
.dataType("INT")
|
||||
.readonly(false) // 设置为可写
|
||||
.enabled(true)
|
||||
.description("测试OPC DA写入")
|
||||
.build();
|
||||
|
||||
// 执行写入操作
|
||||
try {
|
||||
System.out.println("========== 开始测试OPC DA写入 ==========");
|
||||
System.out.println("连接信息: " + connect.getHost());
|
||||
System.out.println("CLSID: " + properties.getString("clsId"));
|
||||
System.out.println("标签地址: " + config.getRegisterAddress());
|
||||
System.out.println("数据类型: " + config.getDataType());
|
||||
|
||||
// 转换为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
|
||||
SiteBO siteBO = SiteBO.builder()
|
||||
.deviceCode(connect.getCode())
|
||||
.alias(config.getAlias())
|
||||
.aliasName(config.getAliasName())
|
||||
.registerAddress(config.getRegisterAddress())
|
||||
.dataType(config.getDataType())
|
||||
.readonly(config.getReadonly())
|
||||
.build();
|
||||
|
||||
// 构建写入值对象
|
||||
String writeValue = "100"; // 要写入的值
|
||||
WValue wValue = WValue.builder()
|
||||
.point(siteBO)
|
||||
.value(writeValue)
|
||||
.type(config.getDataType())
|
||||
.build();
|
||||
|
||||
System.out.println("写入值: " + writeValue);
|
||||
|
||||
// 调用驱动写入数据
|
||||
Boolean result = opcDaProtocolDriver.write(deviceBO, wValue);
|
||||
|
||||
if (result != null && result) {
|
||||
System.out.println("写入成功!");
|
||||
System.out.println("写入结果: " + result);
|
||||
} else {
|
||||
System.out.println("写入失败!");
|
||||
}
|
||||
System.out.println("========== OPC DA写入测试完成 ==========");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("OPC DA写入测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user