feat:s7写操作
This commit is contained in:
@@ -506,6 +506,7 @@ public class ApiTest {
|
||||
.aliasName("DB1整数值")
|
||||
// .registerAddress("DB1.DB0.0")
|
||||
.registerAddress("DB1.DBD0")
|
||||
// .registerAddress("DB1:1")
|
||||
.dataType("DINT")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
@@ -573,7 +574,8 @@ public class ApiTest {
|
||||
System.out.println("地址: " + config2.getRegisterAddress());
|
||||
System.out.println("数据类型: " + config2.getDataType());
|
||||
System.out.println("读取结果: " + result2.getValue());
|
||||
|
||||
System.out.println("异常信息: " + result2.getExceptionMessage());
|
||||
|
||||
// 测试读取实数值
|
||||
// System.out.println("\n--- 测试读取实数值 ---");
|
||||
// SiteBO siteBO3 = SiteBO.builder()
|
||||
@@ -598,4 +600,362 @@ public class ApiTest {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void plcS7BatchReadTest() {
|
||||
// 构建PLC S7连接对象
|
||||
JSONObject properties = new JSONObject();
|
||||
properties.put("remote-rack", "0");
|
||||
properties.put("remote-slot", "0");
|
||||
properties.put("controller-type", "S7_1200");
|
||||
|
||||
IotConnect connect = IotConnect.builder()
|
||||
.id(2)
|
||||
.code("PLC_S7_001")
|
||||
.host("192.168.81.251")
|
||||
.port(102)
|
||||
.properties(JSONObject.toJSONString(properties))
|
||||
.protocol("plc-s7")
|
||||
.enabled(true)
|
||||
.description("测试PLC S7连接")
|
||||
.build();
|
||||
|
||||
// 构建多个配置对象进行批量读取
|
||||
IotConfig config1 = IotConfig.builder()
|
||||
.id(1)
|
||||
.connectId(2)
|
||||
.alias("db1_bool")
|
||||
.aliasName("DB1布尔值")
|
||||
.registerAddress("DB1.DB0.0")
|
||||
.dataType("BOOL")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试DB1布尔值读取")
|
||||
.build();
|
||||
|
||||
IotConfig config2 = IotConfig.builder()
|
||||
.id(2)
|
||||
.connectId(2)
|
||||
.alias("db1_int")
|
||||
.aliasName("DB1整数值")
|
||||
.registerAddress("DB1:0")
|
||||
.dataType("INT")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试DB1整数读取")
|
||||
.build();
|
||||
|
||||
IotConfig config3 = IotConfig.builder()
|
||||
.id(3)
|
||||
.connectId(2)
|
||||
.alias("db1_real")
|
||||
.aliasName("DB1实数值")
|
||||
.registerAddress("DB1:2")
|
||||
.dataType("REAL")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试DB1实数读取")
|
||||
.build();
|
||||
|
||||
// 执行批量读取操作
|
||||
try {
|
||||
System.out.println("========== 开始测试PLC S7批量读取 ==========");
|
||||
System.out.println("连接信息: " + connect.getHost() + ":" + connect.getPort());
|
||||
System.out.println("控制器类型: " + properties.getString("controller-type"));
|
||||
|
||||
// 转换为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(connect.getCode())
|
||||
// .alias(config1.getAlias())
|
||||
// .aliasName(config1.getAliasName())
|
||||
// .registerAddress(config1.getRegisterAddress())
|
||||
// .dataType(config1.getDataType())
|
||||
// .readonly(config1.getReadonly())
|
||||
// .build(),
|
||||
SiteBO.builder()
|
||||
.deviceCode(connect.getCode())
|
||||
.alias(config2.getAlias())
|
||||
.aliasName(config2.getAliasName())
|
||||
.registerAddress(config2.getRegisterAddress())
|
||||
.dataType(config2.getDataType())
|
||||
.readonly(config2.getReadonly())
|
||||
.build(),
|
||||
SiteBO.builder()
|
||||
.deviceCode(connect.getCode())
|
||||
.alias(config3.getAlias())
|
||||
.aliasName(config3.getAliasName())
|
||||
.registerAddress(config3.getRegisterAddress())
|
||||
.dataType(config3.getDataType())
|
||||
.readonly(config3.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 = plcS7ProtocolDriver.batchRead(deviceBO, siteBOList);
|
||||
|
||||
System.out.println("批量读取完成!");
|
||||
System.out.println("读取结果:");
|
||||
|
||||
// 输出每个点位的读取结果
|
||||
for (RValue rValue : result) {
|
||||
SiteBO site = rValue.getSiteBO();
|
||||
String value = rValue.getValue();
|
||||
System.out.println(" - " + site.getAliasName() + " (" + site.getAlias() + "): " +
|
||||
value + (value == null ? " [" + rValue.getExceptionMessage() + "]" : ""));
|
||||
}
|
||||
|
||||
System.out.println("========== PLC S7批量读取测试完成 ==========");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("PLC S7批量读取测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void plcS7WriteTest() {
|
||||
// 构建PLC S7连接对象
|
||||
JSONObject properties = new JSONObject();
|
||||
properties.put("remote-rack", "0");
|
||||
properties.put("remote-slot", "0");
|
||||
properties.put("controller-type", "S7_1200");
|
||||
|
||||
IotConnect connect = IotConnect.builder()
|
||||
.id(2)
|
||||
.code("PLC_S7_001")
|
||||
.host("192.168.81.251")
|
||||
.port(102)
|
||||
.properties(JSONObject.toJSONString(properties))
|
||||
.protocol("plc-s7")
|
||||
.enabled(true)
|
||||
.description("测试PLC S7连接")
|
||||
.build();
|
||||
|
||||
// 构建配置对象 - 写入操作
|
||||
IotConfig config = IotConfig.builder()
|
||||
.id(1)
|
||||
.connectId(2)
|
||||
.alias("db1_output")
|
||||
.aliasName("DB1输出值")
|
||||
.registerAddress("DB1.DBW10")
|
||||
.dataType("INT")
|
||||
.readonly(false) // 设置为可写
|
||||
.enabled(true)
|
||||
.description("测试PLC S7写入操作")
|
||||
.build();
|
||||
|
||||
// 执行写入操作
|
||||
try {
|
||||
System.out.println("========== 开始测试PLC S7写入 ==========");
|
||||
System.out.println("连接信息: " + connect.getHost() + ":" + connect.getPort());
|
||||
System.out.println("控制器类型: " + properties.getString("controller-type"));
|
||||
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 = "123"; // 要写入的值
|
||||
WValue wValue = WValue.builder()
|
||||
.point(siteBO)
|
||||
.value(writeValue)
|
||||
.type(config.getDataType())
|
||||
.build();
|
||||
|
||||
System.out.println("写入值: " + writeValue);
|
||||
|
||||
// 调用驱动写入数据
|
||||
Boolean result = plcS7ProtocolDriver.write(deviceBO, wValue);
|
||||
|
||||
if (result != null && result) {
|
||||
System.out.println("写入成功!");
|
||||
System.out.println("写入结果: " + result);
|
||||
} else {
|
||||
System.out.println("写入失败!");
|
||||
}
|
||||
System.out.println("========== PLC S7写入测试完成 ==========");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("PLC S7写入测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void plcS7BatchWriteTest() {
|
||||
// 构建PLC S7连接对象
|
||||
JSONObject properties = new JSONObject();
|
||||
properties.put("remote-rack", "0");
|
||||
properties.put("remote-slot", "0");
|
||||
properties.put("controller-type", "S7_1200");
|
||||
|
||||
IotConnect connect = IotConnect.builder()
|
||||
.id(2)
|
||||
.code("PLC_S7_001")
|
||||
.host("192.168.81.251")
|
||||
.port(102)
|
||||
.properties(JSONObject.toJSONString(properties))
|
||||
.protocol("plc-s7")
|
||||
.enabled(true)
|
||||
.description("测试PLC S7连接")
|
||||
.build();
|
||||
|
||||
// 构建多个配置对象进行批量写入
|
||||
IotConfig config1 = IotConfig.builder()
|
||||
.id(1)
|
||||
.connectId(2)
|
||||
.alias("db1_output1")
|
||||
.aliasName("DB1输出值1")
|
||||
.registerAddress("DB1.DBW10")
|
||||
.dataType("INT")
|
||||
.readonly(false) // 设置为可写
|
||||
.enabled(true)
|
||||
.description("测试批量写入1")
|
||||
.build();
|
||||
|
||||
IotConfig config2 = IotConfig.builder()
|
||||
.id(2)
|
||||
.connectId(2)
|
||||
.alias("db1_output2")
|
||||
.aliasName("DB1输出值2")
|
||||
.registerAddress("DB1.DBW12")
|
||||
.dataType("INT")
|
||||
.readonly(false) // 设置为可写
|
||||
.enabled(true)
|
||||
.description("测试批量写入2")
|
||||
.build();
|
||||
|
||||
IotConfig config3 = IotConfig.builder()
|
||||
.id(3)
|
||||
.connectId(2)
|
||||
.alias("db1_output3")
|
||||
.aliasName("DB1输出值3")
|
||||
.registerAddress("DB1.DBD14")
|
||||
.dataType("REAL")
|
||||
.readonly(false) // 设置为可写
|
||||
.enabled(true)
|
||||
.description("测试批量写入3")
|
||||
.build();
|
||||
|
||||
// 执行批量写入操作
|
||||
try {
|
||||
System.out.println("========== 开始测试PLC S7批量写入 ==========");
|
||||
System.out.println("连接信息: " + connect.getHost() + ":" + connect.getPort());
|
||||
System.out.println("控制器类型: " + properties.getString("controller-type"));
|
||||
|
||||
// 转换为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并构建WValue列表
|
||||
java.util.List<WValue> wValueList = java.util.Arrays.asList(
|
||||
WValue.builder()
|
||||
.point(SiteBO.builder()
|
||||
.deviceCode(connect.getCode())
|
||||
.alias(config1.getAlias())
|
||||
.aliasName(config1.getAliasName())
|
||||
.registerAddress(config1.getRegisterAddress())
|
||||
.dataType(config1.getDataType())
|
||||
.readonly(config1.getReadonly())
|
||||
.build())
|
||||
.value("100") // 写入值1
|
||||
.type(config1.getDataType())
|
||||
.build(),
|
||||
WValue.builder()
|
||||
.point(SiteBO.builder()
|
||||
.deviceCode(connect.getCode())
|
||||
.alias(config2.getAlias())
|
||||
.aliasName(config2.getAliasName())
|
||||
.registerAddress(config2.getRegisterAddress())
|
||||
.dataType(config2.getDataType())
|
||||
.readonly(config2.getReadonly())
|
||||
.build())
|
||||
.value("200") // 写入值2
|
||||
.type(config2.getDataType())
|
||||
.build(),
|
||||
WValue.builder()
|
||||
.point(SiteBO.builder()
|
||||
.deviceCode(connect.getCode())
|
||||
.alias(config3.getAlias())
|
||||
.aliasName(config3.getAliasName())
|
||||
.registerAddress(config3.getRegisterAddress())
|
||||
.dataType(config3.getDataType())
|
||||
.readonly(config3.getReadonly())
|
||||
.build())
|
||||
.value("25.5") // 写入值3
|
||||
.type(config3.getDataType())
|
||||
.build()
|
||||
);
|
||||
|
||||
System.out.println("批量写入点位数量: " + wValueList.size());
|
||||
for (WValue wValue : wValueList) {
|
||||
SiteBO site = wValue.getPoint();
|
||||
System.out.println(" - " + site.getAliasName() + " (" + site.getAlias() + "): " +
|
||||
site.getRegisterAddress() + " [" + site.getDataType() + "] = " + wValue.getValue());
|
||||
}
|
||||
|
||||
// 调用驱动批量写入数据
|
||||
List<WResponse> result = plcS7ProtocolDriver.batchWrite(deviceBO, wValueList);
|
||||
|
||||
System.out.println("批量写入完成!");
|
||||
System.out.println("写入结果:");
|
||||
|
||||
// 输出每个点位的写入结果
|
||||
for (WResponse wResponse : result) {
|
||||
WValue wValue = wResponse.getWValue();
|
||||
SiteBO site = wValue.getPoint();
|
||||
Boolean success = wResponse.getIsOk();
|
||||
System.out.println(" - " + site.getAliasName() + " (" + site.getAlias() + "): " +
|
||||
"写入值=" + wValue.getValue() + " 结果=" + (success ? "成功" : "失败"));
|
||||
}
|
||||
|
||||
System.out.println("========== PLC S7批量写入测试完成 ==========");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("PLC S7批量写入测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user