feat: plcs7协议(未测试)

This commit is contained in:
2026-03-05 17:16:34 +08:00
parent 21bdd8bda1
commit 626409626c
3 changed files with 158 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
package org.nl.iot.core.driver.protocol.plcs7;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.Getter;
@@ -10,6 +11,7 @@ import org.nl.common.exception.CommonException;
import org.nl.iot.core.driver.bo.AttributeBO;
import org.nl.iot.core.driver.entity.RValue;
import org.nl.iot.core.driver.entity.WValue;
import org.nl.iot.core.driver.enums.AttributeTypeFlagEnum;
import org.nl.iot.core.driver.protocol.plcs7.com.github.s7.PlcS7PointVariable;
import org.nl.iot.core.driver.protocol.plcs7.com.github.s7.api.S7Connector;
import org.nl.iot.core.driver.protocol.plcs7.com.github.s7.api.S7Serializer;
@@ -39,6 +41,8 @@ public class PlcS7ProtocolDriverImpl implements DriverCustomService {
* 仅供参考
*/
private Map<String, MyS7Connector> connectMap;
@PostConstruct
@Override
public void initial() {
connectMap = new ConcurrentHashMap<>(16);
@@ -81,9 +85,80 @@ public class PlcS7ProtocolDriverImpl implements DriverCustomService {
@Override
public Boolean write(Map<String, AttributeBO> driverConfig, Map<String, AttributeBO> pointConfig, IotConnect device, IotConfig point, WValue wValue) {
return null;
}
log.debug("Plc S7 Write, device: {}, value: {}", device, wValue);
MyS7Connector myS7Connector = getS7Connector(String.valueOf(device.getId()), driverConfig);
myS7Connector.lock.writeLock().lock();
S7Serializer serializer = S7SerializerFactory.buildSerializer(myS7Connector.getConnector());
PlcS7PointVariable plcs7PointVariable = getPointVariable(pointConfig, wValue.getType());
try {
store(serializer, plcs7PointVariable, wValue.getType(), wValue.getValue());
return true;
} catch (Exception e) {
log.error("Plc S7 Write Error: {}", e.getMessage());
return false;
} finally {
myS7Connector.lock.writeLock().unlock();
}
}
/**
* 向 PLC S7 写入数据
* <p>
* 该方法用于将指定类型的数据写入到 PLC S7 的指定点位。
* 1. 根据类型字符串获取对应的 {@link AttributeTypeFlagEnum} 枚举值。
* 2. 如果类型不支持, 抛出 {@link UnSupportException} 异常。
* 3. 根据类型将字符串值转换为相应的 Java 类型。
* 4. 使用 {@link S7Serializer} 将数据写入到 PLC S7 的指定数据块和字节偏移量位置。
* <p>
* 支持的数据类型包括:
* - 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 连接器
* <p>