+ * 该方法用于将指定类型的数据写入到 PLC S7 的指定点位。 + * 1. 根据类型字符串获取对应的 {@link AttributeTypeFlagEnum} 枚举值。 + * 2. 如果类型不支持, 抛出 {@link UnSupportException} 异常。 + * 3. 根据类型将字符串值转换为相应的 Java 类型。 + * 4. 使用 {@link S7Serializer} 将数据写入到 PLC S7 的指定数据块和字节偏移量位置。 + *
+ * 支持的数据类型包括: + * - INT: 整型 + * - LONG: 长整型 + * - FLOAT: 单精度浮点型 + * - DOUBLE: 双精度浮点型 + * - BOOLEAN: 布尔型 + * - STRING: 字符串 + * + * @param serializer S7 序列化器, 用于与 PLC S7 进行数据交互 + * @param plcS7PointVariable PLC S7 点位变量信息, 包含数据块编号, 字节偏移量等 + * @param type 数据类型字符串, 用于标识要写入的数据类型 + * @param value 要写入的字符串形式的数据值 + * @throws CommonException 如果数据类型不支持, 抛出此异常 + */ + private void store(S7Serializer serializer, PlcS7PointVariable plcS7PointVariable, String type, String value) { + AttributeTypeFlagEnum valueType = AttributeTypeFlagEnum.ofCode(type); + if (Objects.isNull(valueType)) { + throw new CommonException("Unsupported type of " + type); + } + AttributeBO attributeBOConfig = new AttributeBO(value); + + switch (valueType) { + case INT: + int intValue = attributeBOConfig.getValueByClass(Integer.class); + serializer.store(intValue, plcS7PointVariable.getDbNum(), plcS7PointVariable.getByteOffset()); + break; + case LONG: + long longValue = attributeBOConfig.getValueByClass(Long.class); + serializer.store(longValue, plcS7PointVariable.getDbNum(), plcS7PointVariable.getByteOffset()); + break; + case FLOAT: + float floatValue = attributeBOConfig.getValueByClass(Float.class); + serializer.store(floatValue, plcS7PointVariable.getDbNum(), plcS7PointVariable.getByteOffset()); + break; + case DOUBLE: + double doubleValue = attributeBOConfig.getValueByClass(Double.class); + serializer.store(doubleValue, plcS7PointVariable.getDbNum(), plcS7PointVariable.getByteOffset()); + break; + case BOOLEAN: + boolean booleanValue = attributeBOConfig.getValueByClass(Boolean.class); + serializer.store(booleanValue, plcS7PointVariable.getDbNum(), plcS7PointVariable.getByteOffset()); + break; + case STRING: + serializer.store(value, plcS7PointVariable.getDbNum(), plcS7PointVariable.getByteOffset()); + break; + default: + break; + } + } /** * 获取 PLC S7 连接器 *
diff --git a/nl-web-app/src/test/java/org/nl/ApiTest.java b/nl-web-app/src/test/java/org/nl/ApiTest.java
index 0a1f1df..871dae4 100644
--- a/nl-web-app/src/test/java/org/nl/ApiTest.java
+++ b/nl-web-app/src/test/java/org/nl/ApiTest.java
@@ -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