refactor: Extract WriteS7Header

This commit is contained in:
Michael Croes
2023-07-19 23:29:11 +02:00
parent 42194aa788
commit ebf3da6280

View File

@@ -20,6 +20,16 @@ namespace S7.Net
stream.Write(new byte[] { 0x02, 0xf0, 0x80 });
}
private static void WriteS7Header(System.IO.MemoryStream stream, byte messageType, int parameterLength, int dataLength)
{
stream.Write(0x32); // S7 protocol ID
stream.Write(messageType); // Message type
stream.Write(new byte[] { 0x00, 0x00 }); // Reserved
stream.Write(new byte[] { 0x00, 0x00 }); // PDU ref
stream.Write(Word.ToByteArray((ushort) parameterLength));
stream.Write(Word.ToByteArray((ushort) dataLength));
}
/// <summary>
/// Creates the header to read bytes from the PLC
/// </summary>
@@ -30,10 +40,9 @@ namespace S7.Net
// Header size 19, 12 bytes per item
WriteTpktHeader(stream, 19 + (12 * amount));
WriteDateHeader(stream);
stream.Write(new byte[] { 0x32, 0x01, 0x00, 0x00, 0x00, 0x00 });
//data part size
stream.Write(Word.ToByteArray((ushort)(2 + (amount * 12))));
stream.Write(new byte[] { 0x00, 0x00, 0x04 });
WriteS7Header(stream, 0x01, 2 + amount * 12, 0);
// Function code: read request
stream.Write(0x04);
//amount of requests
stream.WriteByte((byte)amount);
}