From 6bf292df470f759dd6bce4d8f8bd14d39582650f Mon Sep 17 00:00:00 2001 From: weizhihong Date: Wed, 8 Mar 2023 09:45:20 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90AFC=20=E7=BD=91=E5=85=B3=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E9=80=BB=E8=BE=91=E5=A4=84=E7=90=86=EF=BC=88=E7=BD=91?= =?UTF-8?q?=E5=85=B3=E9=83=A8=E7=BD=B2=E6=88=90=E5=8A=9F=E5=90=8E=E5=88=A0?= =?UTF-8?q?=E9=99=A4=EF=BC=89=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../real/afctransfer/AfcMessageHandler.java | 72 ++++++++++++++----- .../real/afctransfer/AfcTransferServer.java | 2 +- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/afctransfer/AfcMessageHandler.java b/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/afctransfer/AfcMessageHandler.java index b00e9c876..fd2234b3f 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/afctransfer/AfcMessageHandler.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/afctransfer/AfcMessageHandler.java @@ -2,17 +2,18 @@ package club.joylink.rtss.simulation.cbtc.device.real.afctransfer; import club.joylink.rtss.simulation.cbtc.device.real.modbustcp.PlcGatewayConnectManager; import club.joylink.rtss.simulation.cbtc.device.real.modbustcp.handler.ModbusMessageHandler2; +import com.digitalpetri.modbus.FunctionCode; import com.digitalpetri.modbus.codec.ModbusTcpPayload; -import io.netty.channel.Channel; +import com.digitalpetri.modbus.requests.*; +import com.digitalpetri.modbus.responses.*; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.util.internal.shaded.org.jctools.queues.MpscArrayQueue; -import io.netty.util.internal.shaded.org.jctools.queues.MpscChunkedArrayQueue; -import io.netty.util.internal.shaded.org.jctools.queues.atomic.MpscAtomicArrayQueue; import lombok.extern.slf4j.Slf4j; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Semaphore; +import java.nio.charset.StandardCharsets; import java.util.concurrent.locks.ReentrantLock; /** @@ -54,17 +55,50 @@ public class AfcMessageHandler extends SimpleChannelInboundHandler future = modbusMessageHandler.sendModbusMessage(channel, query.getPayload()); + // TODO 网关调试通后放开注释 +// Channel channel = plcGatewayConnectManager.getChannel(GatewayName); +// if(channel == null) { +// log.warn("AFC网关不在线,不发送数据"); +// return; +// } +// // 发送消息到网关 +// CompletableFuture future = modbusMessageHandler.sendModbusMessage(channel, query.getPayload()); try { // 获取网关消息并回写给AFC客户端 - ModbusTcpPayload payload = future.get(); - query.getChannel().writeAndFlush(payload); +// ModbusTcpPayload payload = future.get(); +// query.getChannel().writeAndFlush(payload); + ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!", StandardCharsets.UTF_8); + ModbusResponse modbusResponse = null; + FunctionCode functionCode = query.getPayload().getModbusPdu().getFunctionCode(); + switch (functionCode) { + case ReadCoils: + modbusResponse = new ReadCoilsResponse(buf); + break; + case ReadDiscreteInputs: + modbusResponse = new ReadDiscreteInputsResponse(buf); + break; + case ReadHoldingRegisters: + modbusResponse = new ReadHoldingRegistersResponse(buf); + break; + case ReadInputRegisters: + modbusResponse = new ReadInputRegistersResponse(buf); + break; + case WriteSingleCoil: + modbusResponse = new WriteSingleCoilResponse(0, 19); + break; + case WriteSingleRegister: + modbusResponse = new WriteSingleRegisterResponse(0, 19); + break; + case WriteMultipleCoils: + modbusResponse = new WriteMultipleCoilsResponse(0, 19); + break; + case WriteMultipleRegisters: + modbusResponse = new WriteMultipleRegistersResponse(0, 19); + break; + default: + modbusResponse = new ReadCoilsResponse(buf); + } + query.getChannel().writeAndFlush(modbusResponse); } catch (Exception e) { log.error("获取AFC网关响应异常", e); } @@ -83,11 +117,11 @@ public class AfcMessageHandler extends SimpleChannelInboundHandler