feat: plcs7协议(未测试)
This commit is contained in:
@@ -5,6 +5,7 @@ import org.junit.runner.RunWith;
|
||||
import org.nl.iot.core.driver.bo.AttributeBO;
|
||||
import org.nl.iot.core.driver.entity.RValue;
|
||||
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;
|
||||
@@ -152,8 +153,81 @@ public class ApiTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private PlcS7ProtocolDriverImpl plcS7ProtocolDriver;
|
||||
|
||||
@Test
|
||||
public void plcS7TestRead() {
|
||||
// 初始化驱动
|
||||
plcS7ProtocolDriver.initial();
|
||||
|
||||
// 构建驱动配置(连接配置)
|
||||
Map<String, AttributeBO> driverConfig = new HashMap<>();
|
||||
driverConfig.put("host", AttributeBO.builder().value("192.168.10.33").build());
|
||||
driverConfig.put("port", AttributeBO.builder().value("102").build());
|
||||
|
||||
// 构建点位配置
|
||||
Map<String, AttributeBO> pointConfig = new HashMap<>();
|
||||
pointConfig.put("dbNum", AttributeBO.builder().value("1").build()); // 数据块编号
|
||||
pointConfig.put("byteOffset", AttributeBO.builder().value("0").build()); // 字节偏移量
|
||||
pointConfig.put("bitOffset", AttributeBO.builder().value("0").build()); // 位偏移量
|
||||
pointConfig.put("blockSize", AttributeBO.builder().value("1").build()); // 数据块大小
|
||||
pointConfig.put("data_type", AttributeBO.builder().value("int").build()); // 数据类型
|
||||
|
||||
// 构建连接对象
|
||||
IotConnect connect = IotConnect.builder()
|
||||
.id(1)
|
||||
.code("PLC_S7_001")
|
||||
.host("192.168.10.33")
|
||||
.port(102)
|
||||
.protocol("plc-s7")
|
||||
.enabled(true)
|
||||
.description("测试PLC S7连接")
|
||||
.build();
|
||||
|
||||
// 构建配置对象
|
||||
IotConfig config = IotConfig.builder()
|
||||
.id(1)
|
||||
.connectId(1)
|
||||
.alias("pressure")
|
||||
.aliasName("压力传感器")
|
||||
.registerAddress("DB1.DBW0")
|
||||
.dataType("int")
|
||||
.readonly(true)
|
||||
.enabled(true)
|
||||
.description("测试PLC S7数据读取")
|
||||
.build();
|
||||
|
||||
try {
|
||||
System.out.println("=== PLC S7读取测试开始 ===");
|
||||
System.out.println("连接地址: " + driverConfig.get("host").getValue() + ":" + driverConfig.get("port").getValue());
|
||||
System.out.println("数据块: DB" + pointConfig.get("dbNum").getValue() +
|
||||
", 偏移: " + pointConfig.get("byteOffset").getValue() +
|
||||
", 类型: " + pointConfig.get("data_type").getValue());
|
||||
|
||||
// 调用read方法进行测试
|
||||
RValue result = plcS7ProtocolDriver.read(driverConfig, pointConfig, connect, config);
|
||||
|
||||
// 检查结果是否为null
|
||||
if (result != null) {
|
||||
System.out.println("\n✓ 读取成功!");
|
||||
System.out.println("连接信息: " + result.getConnect());
|
||||
System.out.println("配置信息: " + result.getConfig());
|
||||
System.out.println("读取值: " + result.getValue());
|
||||
} else {
|
||||
System.err.println("\n✗ 读取失败:返回结果为null");
|
||||
System.err.println("可能原因:");
|
||||
System.err.println("1. PLC设备未连接或IP地址不正确");
|
||||
System.err.println("2. 端口号配置错误(S7-300/400默认102)");
|
||||
System.err.println("3. 数据块(DB)不存在或无访问权限");
|
||||
System.err.println("4. 数据类型或偏移量配置错误");
|
||||
}
|
||||
|
||||
System.out.println("\n=== 测试完成 ===");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("\n✗ 测试异常: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user