From 156b49ab9d08888853c44c8efe3710df4ed58ad8 Mon Sep 17 00:00:00 2001 From: rszn <645583145@qq.com> Date: Tue, 18 Jun 2024 13:44:04 +0800 Subject: [PATCH] =?UTF-8?q?decoder=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/Charger/Codec/Decoder.cs | 6 ++-- Service/Charger/Msg/ASDU.cs | 54 +++++++++++++++++++------------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/Service/Charger/Codec/Decoder.cs b/Service/Charger/Codec/Decoder.cs index 4afc699..7a58bf3 100644 --- a/Service/Charger/Codec/Decoder.cs +++ b/Service/Charger/Codec/Decoder.cs @@ -124,8 +124,8 @@ public class Decoder : ByteToMessageDecoder int removeIndex = delimiter.Capacity; - ushort cmd = byteBuffer.GetByte(14 + removeIndex); - ushort recordType = byteBuffer.GetByte(23 + removeIndex); + ushort cmd =data[14 + removeIndex]; + ushort recordType = data[23 + removeIndex]; byte[] bytes = new byte[data.Length - (23 + removeIndex)]; Array.Copy(data, 23 + removeIndex, bytes, 0, bytes.Length); ASDU asdu = cmd switch @@ -248,7 +248,7 @@ public class Decoder : ByteToMessageDecoder _ => new ASDU() }; - ASDU.ParseHeader(byteBuffer, asdu); + ASDU.ParseHeader(data, asdu); return asdu; } diff --git a/Service/Charger/Msg/ASDU.cs b/Service/Charger/Msg/ASDU.cs index b452977..1a9f948 100644 --- a/Service/Charger/Msg/ASDU.cs +++ b/Service/Charger/Msg/ASDU.cs @@ -46,29 +46,41 @@ public class ASDU : APCI } - public static void ParseHeader(IByteBuffer byteBuffer, ASDU asdu) + public static void ParseHeader(byte[] data, ASDU asdu) { - var start = ChargerConst.StartChar.Length - 1; + IByteBuffer byteBuffer = Unpooled.WrappedBuffer(data); + try + { + var start = ChargerConst.StartChar.Length - 1; - asdu.PackLen = byteBuffer.GetUnsignedShortLE(start + 1); - asdu.CtlArea = byteBuffer.GetUnsignedInt(start + 3); - asdu.DestAddr = new[] + asdu.PackLen = byteBuffer.GetUnsignedShortLE(start + 1); + asdu.CtlArea = byteBuffer.GetUnsignedInt(start + 3); + asdu.DestAddr = new[] + { + byteBuffer.GetByte(start + 7), + byteBuffer.GetByte(start + 8), + byteBuffer.GetByte(start + 9), + byteBuffer.GetByte(start + 10) + }; + asdu.SrcAddr = byteBuffer.GetUnsignedInt(start + 11); + asdu.FrameTypeNo = byteBuffer.GetByte(start + 15); + asdu.MsgBodyCount = byteBuffer.GetByte(start + 16); + asdu.TransReason = byteBuffer.GetUnsignedShortLE(start + 17); + asdu.PublicAddr = byteBuffer.GetUnsignedShortLE(start + 19); + asdu.MsgBodyAddr = new[] + { + byteBuffer.GetByte(start + 21), + byteBuffer.GetByte(start + 22), + byteBuffer.GetByte(start + 23) + }; + } + catch (Exception e) { - byteBuffer.GetByte(start + 7), - byteBuffer.GetByte(start + 8), - byteBuffer.GetByte(start + 9), - byteBuffer.GetByte(start + 10) - }; - asdu.SrcAddr = byteBuffer.GetUnsignedInt(start + 11); - asdu.FrameTypeNo = byteBuffer.GetByte(start + 15); - asdu.MsgBodyCount = byteBuffer.GetByte(start + 16); - asdu.TransReason = byteBuffer.GetUnsignedShortLE(start + 17); - asdu.PublicAddr = byteBuffer.GetUnsignedShortLE(start + 19); - asdu.MsgBodyAddr = new[] + throw e; + } + finally { - byteBuffer.GetByte(start + 21), - byteBuffer.GetByte(start + 22), - byteBuffer.GetByte(start + 23) - }; + byteBuffer.Release(); + } } -} \ No newline at end of file +}