From 70506e7dba1436788c39501662bc3a341494e220 Mon Sep 17 00:00:00 2001 From: Michael Croes Date: Wed, 10 Oct 2018 21:41:52 +0200 Subject: [PATCH] Throw InvalidDataException in Open(Async) --- S7.Net/PlcAsynchronous.cs | 13 ++++++++----- S7.Net/PlcSynchronous.cs | 13 ++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/S7.Net/PlcAsynchronous.cs b/S7.Net/PlcAsynchronous.cs index 1c28503..fe3c552 100644 --- a/S7.Net/PlcAsynchronous.cs +++ b/S7.Net/PlcAsynchronous.cs @@ -25,16 +25,19 @@ namespace S7.Net 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]); } diff --git a/S7.Net/PlcSynchronous.cs b/S7.Net/PlcSynchronous.cs index 4c30636..bdc40bf 100644 --- a/S7.Net/PlcSynchronous.cs +++ b/S7.Net/PlcSynchronous.cs @@ -23,16 +23,19 @@ namespace S7.Net 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)