Skip to content

Commit

Permalink
refactor(PLCHelpers): Extract WriteSzlRequestHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
mycroes committed Aug 29, 2023
1 parent 5e1ac8c commit eada47c
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions S7.Net/PLCHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,13 @@ private static void WriteUserDataHeader(System.IO.MemoryStream stream, int param
WriteS7Header(stream, s7MessageTypeUserData, parameterLength, dataLength);
}

private static void WriteSzlReadRequest(System.IO.MemoryStream stream, ushort szlId, ushort szlIndex)
private static void WriteSzlRequestHeader(System.IO.MemoryStream stream, byte functionGroup, byte subFunction, int dataLength)
{
WriteUserDataHeader(stream, 8, 8);
WriteUserDataHeader(stream, 8, dataLength);

// Parameter
const byte szlMethodRequest = 0x11;
const byte szlTypeRequest = 0b100;
const byte szlFunctionGroupCpuFunctions = 0b100;
const byte subFunctionReadSzl = 0x01;
const byte szlTypeRequest = 0x4;

// Parameter head
stream.Write(new byte[] { 0x00, 0x01, 0x12 });
Expand All @@ -73,11 +71,20 @@ private static void WriteSzlReadRequest(System.IO.MemoryStream stream, ushort sz
// Method
stream.WriteByte(szlMethodRequest);
// Type / function group
stream.WriteByte(szlTypeRequest << 4 | szlFunctionGroupCpuFunctions);
stream.WriteByte((byte)(szlTypeRequest << 4 | (functionGroup & 0x0f)));
// Subfunction
stream.WriteByte(subFunctionReadSzl);
stream.WriteByte(subFunction);
// Sequence number
stream.WriteByte(0);
}

private static void WriteSzlReadRequest(System.IO.MemoryStream stream, ushort szlId, ushort szlIndex)
{
// Parameter
const byte szlFunctionGroupCpuFunctions = 0b100;
const byte subFunctionReadSzl = 0x01;

WriteSzlRequestHeader(stream, szlFunctionGroupCpuFunctions, subFunctionReadSzl, 8);

// Data
const byte success = 0xff;
Expand Down Expand Up @@ -343,7 +350,7 @@ private static byte[] BuildReadRequestPackage(IList<DataItemAddress> dataItems)
private static byte[] BuildSzlReadRequestPackage(ushort szlId, ushort szlIndex)
{
var stream = new System.IO.MemoryStream();

WriteSzlReadRequest(stream, szlId, szlIndex);
stream.SetLength(stream.Position);

Expand Down

0 comments on commit eada47c

Please sign in to comment.