Skip to content

Commit

Permalink
Merge pull request S7NetPlus#196 from mycroes/invalid-data-on-open
Browse files Browse the repository at this point in the history
Invalid data on open
  • Loading branch information
mycroes authored Mar 14, 2019
2 parents 0fd193e + ce35978 commit e623b53
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 13 deletions.
2 changes: 1 addition & 1 deletion S7.Net.UnitTest/ConnectionRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static byte[] MakeConnectionRequest(byte sourceTsap1, byte sourceTsap2,
destTsap1, destTsap2, //Destination TASP
192, //Parameter Code (tpdu-size)
1, //Parameter Length
11 //TPDU Size (2^11 = 2048)
10 //TPDU Size (2^11 = 2048)
};
}
}
Expand Down
3 changes: 3 additions & 0 deletions S7.Net/COTP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class COTP
/// </summary>
public class TPDU
{
public TPKT TPkt { get; }
public byte HeaderLength;
public byte PDUType;
public int TPDUNumber;
Expand All @@ -24,6 +25,8 @@ public class TPDU

public TPDU(TPKT tPKT)
{
TPkt = tPKT;

var br = new BinaryReader(new MemoryStream(tPKT.Data));
HeaderLength = br.ReadByte();
if (HeaderLength >= 2)
Expand Down
5 changes: 5 additions & 0 deletions S7.Net/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public enum CpuType
/// </summary>
S7200 = 0,

/// <summary>
/// Siemens Logo 0BA8
/// </summary>
Logo0BA8 = 1,

/// <summary>
/// S7 300 cpu type
/// </summary>
Expand Down
43 changes: 43 additions & 0 deletions S7.Net/InvalidDataException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;

namespace S7.Net
{
#if NET_FULL
[Serializable]
#endif
public class InvalidDataException : Exception
{
public byte[] ReceivedData { get; }
public int ErrorIndex { get; }
public byte ExpectedValue { get; }

public InvalidDataException(string message, byte[] receivedData, int errorIndex, byte expectedValue)
: base(FormatMessage(message, receivedData, errorIndex, expectedValue))
{
ReceivedData = receivedData;
ErrorIndex = errorIndex;
ExpectedValue = expectedValue;
}

#if NET_FULL
protected InvalidDataException(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context)
{
ReceivedData = (byte[]) info.GetValue(nameof(ReceivedData), typeof(byte[]));
ErrorIndex = info.GetInt32(nameof(ErrorIndex));
ExpectedValue = info.GetByte(nameof(ExpectedValue));
}
#endif

private static string FormatMessage(string message, byte[] receivedData, int errorIndex, byte expectedValue)
{
if (errorIndex >= receivedData.Length)
throw new ArgumentOutOfRangeException(nameof(errorIndex),
$"{nameof(errorIndex)} {errorIndex} is outside the bounds of {nameof(receivedData)} with length {receivedData.Length}.");

return $"{message} Invalid data received. Expected '{expectedValue}' at index {errorIndex}, " +
$"but received {receivedData[errorIndex]}. See the {nameof(ReceivedData)} property " +
"for the full message received.";
}
}
}
2 changes: 1 addition & 1 deletion S7.Net/PLCHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private int VarTypeToByteLength(VarType varType, int varCount = 1)
private byte[] GetS7ConnectionSetup()
{
return new byte[] { 3, 0, 0, 25, 2, 240, 128, 50, 1, 0, 0, 255, 255, 0, 8, 0, 0, 240, 0, 0, 3, 0, 3,
7, 128 //Try 1920 PDU Size. Same as libnodave.
3, 192 // Use 960 PDU size
};
}

Expand Down
13 changes: 8 additions & 5 deletions S7.Net/PlcAsynchronous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ public async Task OpenAsync()
var response = await COTP.TPDU.ReadAsync(stream);
if (response.PDUType != 0xd0) //Connect Confirm
{
throw new WrongNumberOfBytesException("Waiting for COTP connect confirm");
throw new InvalidDataException("Error reading Connection Confirm", response.TPkt.Data, 1, 0x0d);
}

await stream.WriteAsync(GetS7ConnectionSetup(), 0, 25);

var s7data = await COTP.TSDU.ReadAsync(stream);
if (s7data == null || s7data[1] != 0x03) //Check for S7 Ack Data
{
throw new WrongNumberOfBytesException("Waiting for S7 connection setup");
}
if (s7data == null)
throw new WrongNumberOfBytesException("No data received in response to Communication Setup");

//Check for S7 Ack Data
if (s7data[1] != 0x03)
throw new InvalidDataException("Error reading Communication Setup response", s7data, 1, 0x03);

MaxPDUSize = (short)(s7data[18] * 256 + s7data[19]);
}

Expand Down
13 changes: 8 additions & 5 deletions S7.Net/PlcSynchronous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ public void Open()
var response = COTP.TPDU.Read(stream);
if (response.PDUType != 0xd0) //Connect Confirm
{
throw new WrongNumberOfBytesException("Waiting for COTP connect confirm");
throw new InvalidDataException("Error reading Connection Confirm", response.TPkt.Data, 1, 0x0d);
}

stream.Write(GetS7ConnectionSetup(), 0, 25);

var s7data = COTP.TSDU.Read(stream);
if (s7data == null || s7data[1] != 0x03) //Check for S7 Ack Data
{
throw new WrongNumberOfBytesException("Waiting for S7 connection setup");
}
if (s7data == null)
throw new WrongNumberOfBytesException("No data received in response to Communication Setup");

//Check for S7 Ack Data
if (s7data[1] != 0x03)
throw new InvalidDataException("Error reading Communication Setup response", s7data, 1, 0x03);

MaxPDUSize = (short)(s7data[18] * 256 + s7data[19]);
}
catch (Exception exc)
Expand Down
10 changes: 9 additions & 1 deletion S7.Net/Protocol/ConnectionRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static byte[] GetCOTPConnectionRequest(CpuType cpu, Int16 rack, Int16 slo
3, 0, //Destination TASP
192, //Parameter Code (tpdu-size)
1, //Parameter Length
11 //TPDU Size (2^11 = 2048)
10 //TPDU Size (2^10 = 1024)
};

switch (cpu)
Expand All @@ -34,6 +34,14 @@ public static byte[] GetCOTPConnectionRequest(CpuType cpu, Int16 rack, Int16 slo
bSend1[17] = 0x10;
bSend1[18] = 0x00;
break;
case CpuType.Logo0BA8:
// These values are taken from NodeS7, it's not verified if these are
// exact requirements to connect to the Logo0BA8.
bSend1[13] = 0x01;
bSend1[14] = 0x00;
bSend1[17] = 0x01;
bSend1[18] = 0x02;
break;
case CpuType.S71200:
case CpuType.S7300:
case CpuType.S7400:
Expand Down

0 comments on commit e623b53

Please sign in to comment.