From d530f1e4226faa22330c8ff34cec51c5c14e038b Mon Sep 17 00:00:00 2001 From: Serge Camille Date: Tue, 18 Aug 2020 20:38:11 +0200 Subject: [PATCH] COTP TPDU: change byte array copy. This removes the binary reader, and fixes too things: 1. Properly set the data length (previous implementation requested too much, but that did not matter with BinaryReader) 2. Start reading Data after HeaderLength+1 offset, not always at 3. --- S7.Net/COTP.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/S7.Net/COTP.cs b/S7.Net/COTP.cs index 79d60fb..c4af73a 100644 --- a/S7.Net/COTP.cs +++ b/S7.Net/COTP.cs @@ -1,7 +1,6 @@ using System; using System.IO; using System.Threading.Tasks; -using System.Linq; namespace S7.Net { @@ -27,17 +26,17 @@ namespace S7.Net { TPkt = tPKT; - var br = new BinaryReader(new MemoryStream(tPKT.Data)); - HeaderLength = br.ReadByte(); + HeaderLength = tPKT.Data[0]; // Header length excluding this length byte if (HeaderLength >= 2) { - PDUType = br.ReadByte(); + PDUType = tPKT.Data[1]; if (PDUType == 0xf0) //DT Data { - var flags = br.ReadByte(); + var flags = tPKT.Data[2]; TPDUNumber = flags & 0x7F; LastDataUnit = (flags & 0x80) > 0; - Data = br.ReadBytes(tPKT.Length - HeaderLength - 4); //4 = TPKT Size + Data = new byte[tPKT.Data.Length - HeaderLength - 1]; // substract header length byte + header length. + Array.Copy(tPKT.Data, HeaderLength + 1, Data, 0, Data.Length); return; } //TODO: Handle other PDUTypes