| | |
| | | package com.mes.plc.client.impl; |
| | | |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.mes.connect.modbus.ModbusTcpClient; |
| | | import com.mes.device.entity.DeviceConfig; |
| | | import com.mes.plc.client.PlcClient; |
| | |
| | | // 从站地址 |
| | | private final int unitId; |
| | | |
| | | // 设备配置 |
| | | private final DeviceConfig device; |
| | | |
| | | // Modbus客户端实例 |
| | | private ModbusTcpClient modbusClient; |
| | | |
| | |
| | | // 超时时间(毫秒) |
| | | private int timeout = 5000; |
| | | |
| | | // ObjectMapper用于JSON解析 |
| | | private final ObjectMapper objectMapper = new ObjectMapper(); |
| | | |
| | | // 地址映射缓存:字段名 -> Modbus地址 |
| | | private Map<String, String> addressMappingCache; |
| | | |
| | | /** |
| | | * 构造函数 |
| | | * |
| | | * @param device 设备配置 |
| | | */ |
| | | public ModbusPlcClient(DeviceConfig device) { |
| | | this.device = device; |
| | | this.plcIp = device.getPlcIp(); |
| | | this.plcPort = device.getPlcPort() != null ? device.getPlcPort() : 502; |
| | | |
| | |
| | | */ |
| | | private Map<String, Object> parseExtraParams(String extraParamsJson) { |
| | | if (extraParamsJson == null || extraParamsJson.isEmpty()) { |
| | | return null; |
| | | return new HashMap<>(); |
| | | } |
| | | |
| | | try { |
| | |
| | | return Collections.emptyMap(); |
| | | } |
| | | |
| | | if (fields == null || fields.length == 0) { |
| | | return readAllData(); |
| | | } |
| | | |
| | | try { |
| | | // TODO: 实现Modbus读取指定字段数据 |
| | | // 这里暂时返回空Map,后续完善 |
| | |
| | | return Collections.emptyMap(); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public boolean writeData(Map<String, Object> data) { |
| | | if (!isConnected() && !connect()) { |
| | | log.error("Modbus PLC未连接,无法写入数据: {}:{}", this.plcIp, this.plcPort); |
| | | return false; |
| | | } |
| | | |
| | | if (data == null || data.isEmpty()) { |
| | | log.warn("写入数据为空,跳过操作: deviceId={}", device.getId()); |
| | | return true; |
| | | } |
| | | |
| | | try { |
| | |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public boolean isConnected() { |
| | | try { |
| | |
| | | @Override |
| | | public void setTimeout(int timeout) { |
| | | this.timeout = timeout; |
| | | // ModbusTcpClient不支持直接设置超时,这里仅记录超时时间 |
| | | } |
| | | } |