feat:s7读
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.plcs7.PlcS7ProtocolDriverImpl;
|
||||
import org.nl.iot.modular.iot.entity.IotConfig;
|
||||
import org.nl.iot.modular.iot.entity.IotConnect;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -463,4 +464,138 @@ public class ApiTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private PlcS7ProtocolDriverImpl plcS7ProtocolDriver;
|
||||
|
||||
@Test
|
||||
public void plcS7ReadTest() {
|
||||
// 构建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();
|
||||
|
||||
// 构建配置对象 - 读取DB1数据块的不同类型数据
|
||||
IotConfig config1 = IotConfig.builder()
|
||||
.id(1)
|
||||
.connectId(2)
|
||||
.alias("move")
|
||||
.aliasName("DB1布尔值")
|
||||
.registerAddress("DB1.DB0.0")
|
||||
.dataType("WORD")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试DB1布尔值读取")
|
||||
.build();
|
||||
|
||||
IotConfig config2 = IotConfig.builder()
|
||||
.id(2)
|
||||
.connectId(2)
|
||||
.alias("db1_int")
|
||||
.aliasName("DB1整数值")
|
||||
// .registerAddress("DB1.DB0.0")
|
||||
.registerAddress("DB1.DBD0")
|
||||
.dataType("DINT")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试DB1整数读取")
|
||||
.build();
|
||||
//
|
||||
// IotConfig config3 = IotConfig.builder()
|
||||
// .id(3)
|
||||
// .connectId(2)
|
||||
// .alias("db1_real")
|
||||
// .aliasName("DB1实数值")
|
||||
// .registerAddress("DB1.DBD4")
|
||||
// .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"));
|
||||
System.out.println("机架号: " + properties.getString("remote-rack"));
|
||||
System.out.println("插槽号: " + properties.getString("remote-slot"));
|
||||
|
||||
// 转换为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();
|
||||
|
||||
// // 测试读取布尔值
|
||||
// System.out.println("\n--- 测试读取布尔值 ---");
|
||||
// SiteBO siteBO1 = SiteBO.builder()
|
||||
// .deviceCode(connect.getCode())
|
||||
// .alias(config1.getAlias())
|
||||
// .aliasName(config1.getAliasName())
|
||||
// .registerAddress(config1.getRegisterAddress())
|
||||
// .dataType(config1.getDataType())
|
||||
// .readonly(config1.getReadonly())
|
||||
// .build();
|
||||
//
|
||||
// RValue result1 = plcS7ProtocolDriver.read(deviceBO, siteBO1);
|
||||
// System.out.println("地址: " + config1.getRegisterAddress());
|
||||
// System.out.println("数据类型: " + config1.getDataType());
|
||||
// System.out.println("读取结果: " + result1.getValue());
|
||||
|
||||
// 测试读取整数值
|
||||
System.out.println("\n--- 测试读取整数值 ---");
|
||||
SiteBO siteBO2 = SiteBO.builder()
|
||||
.deviceCode(connect.getCode())
|
||||
.alias(config2.getAlias())
|
||||
.aliasName(config2.getAliasName())
|
||||
.registerAddress(config2.getRegisterAddress())
|
||||
.dataType(config2.getDataType())
|
||||
.readonly(config2.getReadonly())
|
||||
.build();
|
||||
|
||||
RValue result2 = plcS7ProtocolDriver.read(deviceBO, siteBO2);
|
||||
System.out.println("地址: " + config2.getRegisterAddress());
|
||||
System.out.println("数据类型: " + config2.getDataType());
|
||||
System.out.println("读取结果: " + result2.getValue());
|
||||
|
||||
// 测试读取实数值
|
||||
// System.out.println("\n--- 测试读取实数值 ---");
|
||||
// SiteBO siteBO3 = SiteBO.builder()
|
||||
// .deviceCode(connect.getCode())
|
||||
// .alias(config3.getAlias())
|
||||
// .aliasName(config3.getAliasName())
|
||||
// .registerAddress(config3.getRegisterAddress())
|
||||
// .dataType(config3.getDataType())
|
||||
// .readonly(config3.getReadonly())
|
||||
// .build();
|
||||
//
|
||||
// RValue result3 = plcS7ProtocolDriver.read(deviceBO, siteBO3);
|
||||
// System.out.println("地址: " + config3.getRegisterAddress());
|
||||
// System.out.println("数据类型: " + config3.getDataType());
|
||||
// System.out.println("读取结果: " + result3.getValue());
|
||||
|
||||
System.out.println("\n========== PLC S7读取测试完成 ==========");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("PLC S7读取测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user