diff --git a/GitVersion.yml b/GitVersion.yml
new file mode 100644
index 0000000..c92669d
--- /dev/null
+++ b/GitVersion.yml
@@ -0,0 +1,17 @@
+assembly-informational-format: '{NuGetVersion}'
+mode: ContinuousDeployment
+branches:
+ master:
+ tag: rc
+ increment: Minor
+ features?[/-]:
+ tag: rc-{BranchName}
+ increment: Minor
+ (pull|pull\-requests|pr)[/-]:
+ tag: rc-pr-{BranchName}
+ increment: Minor
+ hotfix(es)?[/-]:
+ tag: rc
+ increment: Patch
+ dev(elop)?(ment)?$:
+ tag: b
\ No newline at end of file
diff --git a/S7.Net.UnitTest/ConnectionRequestTest.cs b/S7.Net.UnitTest/ConnectionRequestTest.cs
index 4615c3a..db7cabf 100644
--- a/S7.Net.UnitTest/ConnectionRequestTest.cs
+++ b/S7.Net.UnitTest/ConnectionRequestTest.cs
@@ -75,7 +75,7 @@ namespace S7.Net.UnitTest
destTsap1, destTsap2, //Destination TASP
192, //Parameter Code (tpdu-size)
1, //Parameter Length
- 9 //TPDU Size (2^9 = 512)
+ 11 //TPDU Size (2^11 = 2048)
};
}
}
diff --git a/S7.Net.UnitTest/S7.Net.UnitTest.csproj b/S7.Net.UnitTest/S7.Net.UnitTest.csproj
index bfaa48e..cbe6c47 100644
--- a/S7.Net.UnitTest/S7.Net.UnitTest.csproj
+++ b/S7.Net.UnitTest/S7.Net.UnitTest.csproj
@@ -78,6 +78,8 @@
+
+
diff --git a/S7.Net.UnitTest/S7NetTestsAsync.cs b/S7.Net.UnitTest/S7NetTestsAsync.cs
index 48d9ae1..0adb91b 100644
--- a/S7.Net.UnitTest/S7NetTestsAsync.cs
+++ b/S7.Net.UnitTest/S7NetTestsAsync.cs
@@ -226,10 +226,8 @@ namespace S7.Net.UnitTest
IntVariable111 = 201
};
plc.WriteStruct(tc, DB2);
- Assert.AreEqual(ErrorCode.NoError, plc.LastErrorCode);
// Values that are read from a struct are stored in a new struct, returned by the funcion ReadStruct
TestLongStruct tc2 = (TestLongStruct)await plc.ReadStructAsync(typeof(TestLongStruct), DB2);
- Assert.AreEqual(ErrorCode.NoError, plc.LastErrorCode);
Assert.AreEqual(tc.IntVariable0, tc2.IntVariable0);
Assert.AreEqual(tc.IntVariable1, tc2.IntVariable1);
Assert.AreEqual(tc.IntVariable10, tc2.IntVariable10);
@@ -292,11 +290,9 @@ namespace S7.Net.UnitTest
IntVariable111 = 201
};
await plc.WriteClassAsync(tc, DB2);
- Assert.AreEqual(ErrorCode.NoError, plc.LastErrorCode);
// Values that are read from a struct are stored in a new struct, returned by the funcion ReadStruct
TestLongClass tc2 = new TestLongClass();
await plc.ReadClassAsync(tc2, DB2);
- Assert.AreEqual(ErrorCode.NoError, plc.LastErrorCode);
Assert.AreEqual(tc.IntVariable0, tc2.IntVariable0);
Assert.AreEqual(tc.IntVariable1, tc2.IntVariable1);
Assert.AreEqual(tc.IntVariable10, tc2.IntVariable10);
@@ -571,7 +567,7 @@ namespace S7.Net.UnitTest
[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
- public async Task Test_Async_ReadBytesThrowsExceptionIfPlcIsNotConnected()
+ public async Task Test_Async_ReadBytesReturnsNullIfPlcIsNotConnected()
{
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
{
@@ -831,11 +827,9 @@ namespace S7.Net.UnitTest
{
double test_value = 55.66;
await plc.WriteAsync("DB1.DBD0", test_value);
- Assert.AreEqual(plc.LastErrorCode, ErrorCode.NoError, "Write Double");
var helper = await plc.ReadAsync("DB1.DBD0");
double test_value2 = Conversion.ConvertToDouble((uint)helper);
- Assert.AreEqual(plc.LastErrorCode, ErrorCode.NoError, "Read Double");
Assert.AreEqual(test_value, test_value2, 0.01, "Compare Write/Read"); //Need delta here because S7 only has 32 bit reals
}
@@ -844,11 +838,9 @@ namespace S7.Net.UnitTest
{
float test_value = 55.6632f;
await plc.WriteAsync("DB1.DBD0", test_value);
- Assert.AreEqual(plc.LastErrorCode, ErrorCode.NoError, "Write Single");
var helper = await plc.ReadAsync("DB1.DBD0");
float test_value2 = Conversion.ConvertToFloat((uint)helper);
- Assert.AreEqual(plc.LastErrorCode, ErrorCode.NoError, "Read Single");
Assert.AreEqual(test_value, test_value2, "Compare Write/Read"); //No delta, datatype matches
}
diff --git a/S7.Net.UnitTest/S7NetTestsSync.cs b/S7.Net.UnitTest/S7NetTestsSync.cs
index 151b1bd..a3b01c9 100644
--- a/S7.Net.UnitTest/S7NetTestsSync.cs
+++ b/S7.Net.UnitTest/S7NetTestsSync.cs
@@ -81,8 +81,14 @@ namespace S7.Net.UnitTest
{
if (plc.IsConnected == false)
{
- var error = plc.Open();
- Assert.AreEqual(ErrorCode.NoError, error, "If you have s7 installed you must close s7oiehsx64 service.");
+ try
+ {
+ plc.Open();
+ }
+ catch (Exception e)
+ {
+ throw new Exception("If you have s7 installed you must close s7oiehsx64 service.", e);
+ }
}
}
@@ -257,10 +263,8 @@ namespace S7.Net.UnitTest
tc.IntVariable110 = 200;
tc.IntVariable111 = 201;
plc.WriteStruct(tc, DB2);
- Assert.AreEqual(ErrorCode.NoError, plc.LastErrorCode);
// Values that are read from a struct are stored in a new struct, returned by the funcion ReadStruct
TestLongStruct tc2 = (TestLongStruct)plc.ReadStruct(typeof(TestLongStruct), DB2);
- Assert.AreEqual(ErrorCode.NoError, plc.LastErrorCode);
Assert.AreEqual(tc.IntVariable0, tc2.IntVariable0);
Assert.AreEqual(tc.IntVariable1, tc2.IntVariable1);
Assert.AreEqual(tc.IntVariable10, tc2.IntVariable10);
@@ -321,11 +325,9 @@ namespace S7.Net.UnitTest
tc.IntVariable110 = 200;
tc.IntVariable111 = 201;
plc.WriteClass(tc, DB2);
- Assert.AreEqual(ErrorCode.NoError, plc.LastErrorCode);
// Values that are read from a struct are stored in a new struct, returned by the funcion ReadStruct
TestLongClass tc2 = new TestLongClass();
plc.ReadClass(tc2, DB2);
- Assert.AreEqual(ErrorCode.NoError, plc.LastErrorCode);
Assert.AreEqual(tc.IntVariable0, tc2.IntVariable0);
Assert.AreEqual(tc.IntVariable1, tc2.IntVariable1);
Assert.AreEqual(tc.IntVariable10, tc2.IntVariable10);
@@ -596,19 +598,14 @@ namespace S7.Net.UnitTest
}
- [TestMethod]
- public void T13_ReadBytesReturnsEmptyArrayIfPlcIsNotConnected()
+ [TestMethod, ExpectedException(typeof(PlcException))]
+ public void T13_ReadBytesThrowsIfPlcIsNotConnected()
{
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
{
Assert.IsFalse(notConnectedPlc.IsConnected);
-
- int expectedReadBytes = 0; // 0 bytes, because no connection was established
-
TestClass tc = new TestClass();
int actualReadBytes = notConnectedPlc.ReadClass(tc, DB2);
-
- Assert.AreEqual(expectedReadBytes, actualReadBytes);
}
}
@@ -642,8 +639,8 @@ namespace S7.Net.UnitTest
Assert.AreEqual(tc2.DWordVariable, tc2Generic.DWordVariable);
}
- [TestMethod]
- public void T15_ReadClassWithGenericReturnsNullIfPlcIsNotConnected()
+ [TestMethod, ExpectedException(typeof(PlcException))]
+ public void T15_ReadClassWithGenericThrowsIfPlcIsNotConnected()
{
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
{
@@ -684,8 +681,8 @@ namespace S7.Net.UnitTest
Assert.AreEqual(tc2Generic.DWordVariable, tc2GenericWithClassFactory.DWordVariable);
}
- [TestMethod]
- public void T17_ReadClassWithGenericAndClassFactoryReturnsNullIfPlcIsNotConnected()
+ [TestMethod, ExpectedException(typeof(PlcException))]
+ public void T17_ReadClassWithGenericAndClassFactoryThrowsIfPlcIsNotConnected()
{
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
{
@@ -697,8 +694,8 @@ namespace S7.Net.UnitTest
}
}
- [TestMethod]
- public void T18_ReadStructReturnsNullIfPlcIsNotConnected()
+ [TestMethod, ExpectedException(typeof(PlcException))]
+ public void T18_ReadStructThrowsIfPlcIsNotConnected()
{
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
{
@@ -739,8 +736,8 @@ namespace S7.Net.UnitTest
Assert.AreEqual(ts2.DWordVariable, ts2Generic.DWordVariable);
}
- [TestMethod]
- public void T20_ReadStructWithGenericReturnsNullIfPlcIsNotConnected()
+ [TestMethod, ExpectedException(typeof(PlcException))]
+ public void T20_ReadStructThrowsIfPlcIsNotConnected()
{
using (var notConnectedPlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 0))
{
@@ -883,11 +880,9 @@ namespace S7.Net.UnitTest
{
double test_value = 55.66;
plc.Write("DB1.DBD0", test_value);
- Assert.AreEqual(plc.LastErrorCode, ErrorCode.NoError, "Write Double");
var helper = plc.Read("DB1.DBD0");
double test_value2 = Conversion.ConvertToDouble((uint)helper);
- Assert.AreEqual(plc.LastErrorCode, ErrorCode.NoError, "Read Double");
Assert.AreEqual(test_value, test_value2, 0.01, "Compare Write/Read"); //Need delta here because S7 only has 32 bit reals
}
@@ -929,19 +924,17 @@ namespace S7.Net.UnitTest
Assert.AreEqual(tc.Bool1, tc2.Bool1);
}
- [TestMethod]
- public void T29_Read_Write_ExceptionHandlingWhenPlcIsNotReachable()
+ [TestMethod, ExpectedException(typeof(PlcException))]
+ public void T29_Read_Write_ThrowsWhenPlcIsNotReachable()
{
// leave plc Open
S7TestServer.Stop();
double test_value = 55.66;
plc.Write("DB1.DBD0", test_value);
- Assert.AreEqual(plc.LastErrorCode, ErrorCode.WriteData, "No Write Error.");
var helper = plc.Read("DB1.DBD0");
Assert.AreEqual(helper, null, "Value in Read.");
- Assert.AreEqual(plc.LastErrorCode, ErrorCode.ReadData, "No Read Error.");
}
[TestMethod]
@@ -949,11 +942,9 @@ namespace S7.Net.UnitTest
{
float test_value = 55.6632f;
plc.Write("DB1.DBD0", test_value);
- Assert.AreEqual(plc.LastErrorCode, ErrorCode.NoError, "Write Single");
var helper = plc.Read("DB1.DBD0");
float test_value2 = Conversion.ConvertToFloat((uint)helper);
- Assert.AreEqual(plc.LastErrorCode, ErrorCode.NoError, "Read Single");
Assert.AreEqual(test_value, test_value2, "Compare Write/Read"); //No delta, datatype matches
}
diff --git a/S7.Net.UnitTest/TypeTests/StringExTests.cs b/S7.Net.UnitTest/TypeTests/StringExTests.cs
new file mode 100644
index 0000000..164ba4f
--- /dev/null
+++ b/S7.Net.UnitTest/TypeTests/StringExTests.cs
@@ -0,0 +1,115 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using S7.Net.Types;
+
+namespace S7.Net.UnitTest.TypeTests
+{
+ [TestClass]
+ public class StringExTests
+ {
+ [TestMethod]
+ public void ReadEmptyStringWithZeroByteLength()
+ {
+ AssertFromByteArrayEquals("", 0, 0);
+ }
+
+ [TestMethod]
+ public void ReadEmptyStringWithOneByteLength()
+ {
+ AssertFromByteArrayEquals("", 1, 0, 0);
+ }
+
+ [TestMethod]
+ public void ReadEmptyStringWithOneByteGarbage()
+ {
+ AssertFromByteArrayEquals("", 1, 0, (byte) 'a');
+ }
+
+ [TestMethod]
+ public void ReadA()
+ {
+ AssertFromByteArrayEquals("A", 1, 1, (byte) 'A');
+ }
+
+ [TestMethod]
+ public void ReadAbc()
+ {
+ AssertFromByteArrayEquals("Abc", 1, 3, (byte) 'A', (byte) 'b', (byte) 'c');
+ }
+
+ [TestMethod]
+ public void WriteNullWithReservedLengthZero()
+ {
+ AssertToByteArrayEquals(null, 0, 0, 0);
+ }
+
+ [TestMethod]
+ public void WriteEmptyStringWithReservedLengthZero()
+ {
+ AssertToByteArrayEquals("", 0, 0, 0);
+ }
+
+ [TestMethod]
+ public void WriteAWithReservedLengthZero()
+ {
+ AssertToByteArrayEquals("A", 0, 0, 0);
+ }
+
+ [TestMethod]
+ public void WriteNullWithReservedLengthOne()
+ {
+ AssertToByteArrayEquals(null, 1, 1, 0);
+ }
+
+ [TestMethod]
+ public void WriteEmptyStringWithReservedLengthOne()
+ {
+ AssertToByteArrayEquals("", 1, 1, 0);
+ }
+
+ [TestMethod]
+ public void WriteAWithReservedLengthOne()
+ {
+ AssertToByteArrayEquals("A", 1, 1, 1, (byte) 'A');
+ }
+
+ [TestMethod]
+ public void WriteAWithReservedLengthTwo()
+ {
+ AssertToByteArrayEquals("A", 2, 2, 1, (byte) 'A');
+ }
+
+ [TestMethod]
+ public void WriteAbcWithReservedLengthOne()
+ {
+ AssertToByteArrayEquals("Abc", 1, 1, 1, (byte) 'A');
+ }
+
+ [TestMethod]
+ public void WriteAbcWithReservedLengthTwo()
+ {
+ AssertToByteArrayEquals("Abc", 2, 2, 2, (byte) 'A', (byte) 'b');
+ }
+
+ [TestMethod]
+ public void WriteAbcWithReservedLengthThree()
+ {
+ AssertToByteArrayEquals("Abc", 3, 3, 3, (byte) 'A', (byte) 'b', (byte) 'c');
+ }
+
+ [TestMethod]
+ public void WriteAbcWithReservedLengthFour()
+ {
+ AssertToByteArrayEquals("Abc", 4, 4, 3, (byte) 'A', (byte) 'b', (byte) 'c');
+ }
+
+ private static void AssertFromByteArrayEquals(string expected, params byte[] bytes)
+ {
+ Assert.AreEqual(expected, StringEx.FromByteArray(bytes));
+ }
+
+ private static void AssertToByteArrayEquals(string value, int reservedLength, params byte[] expected)
+ {
+ CollectionAssert.AreEqual(expected, StringEx.ToByteArray(value, reservedLength));
+ }
+ }
+}
diff --git a/S7.Net.UnitTest/TypeTests/StringTests.cs b/S7.Net.UnitTest/TypeTests/StringTests.cs
new file mode 100644
index 0000000..15a528b
--- /dev/null
+++ b/S7.Net.UnitTest/TypeTests/StringTests.cs
@@ -0,0 +1,85 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using S7.Net.Types;
+
+namespace S7.Net.UnitTest.TypeTests
+{
+ [TestClass]
+ public class StringTests
+ {
+ [TestMethod]
+ public void WriteNullWIthReservedLengthZero()
+ {
+ AssertToByteArrayEquals(null, 0);
+ }
+
+ [TestMethod]
+ public void WriteEmptyStringWithReservedLengthZero()
+ {
+ AssertToByteArrayEquals("", 0);
+ }
+
+ [TestMethod]
+ public void WriteAWithReservedLengthZero()
+ {
+ AssertToByteArrayEquals("A", 0);
+ }
+
+ [TestMethod]
+ public void WriteNullWithReservedLengthOne()
+ {
+ AssertToByteArrayEquals(null, 1, 0);
+ }
+
+ [TestMethod]
+ public void WriteEmptyStringWithReservedLengthOne()
+ {
+ AssertToByteArrayEquals("", 1, 0);
+ }
+
+ [TestMethod]
+ public void WriteAWithReservedLengthOne()
+ {
+ AssertToByteArrayEquals("A", 1, (byte) 'A');
+ }
+
+ [TestMethod]
+ public void WriteAWithReservedLengthTwo()
+ {
+ AssertToByteArrayEquals("A", 2, (byte) 'A', 0);
+ }
+
+ [TestMethod]
+ public void WriteAbcWithReservedLengthOne()
+ {
+ AssertToByteArrayEquals("Abc", 1, (byte) 'A');
+ }
+
+ [TestMethod]
+ public void WriteAbcWithReservedLengthTwo()
+ {
+ AssertToByteArrayEquals("Abc", 2, (byte) 'A', (byte) 'b');
+ }
+
+ [TestMethod]
+ public void WriteAbcWithReservedLengthThree()
+ {
+ AssertToByteArrayEquals("Abc", 3, (byte) 'A', (byte) 'b', (byte) 'c');
+ }
+
+ [TestMethod]
+ public void WriteAbcWithReservedLengthFour()
+ {
+ AssertToByteArrayEquals("Abc", 4, (byte) 'A', (byte) 'b', (byte) 'c', 0);
+ }
+
+ private static void AssertFromByteArrayEquals(string expected, params byte[] bytes)
+ {
+ Assert.AreEqual(expected, String.FromByteArray(bytes));
+ }
+
+ private static void AssertToByteArrayEquals(string value, int reservedLength, params byte[] expected)
+ {
+ CollectionAssert.AreEqual(expected, String.ToByteArray(value, reservedLength));
+ }
+ }
+}
diff --git a/S7.Net/PLC.cs b/S7.Net/PLC.cs
index 80cefba..284b81b 100644
--- a/S7.Net/PLC.cs
+++ b/S7.Net/PLC.cs
@@ -48,7 +48,15 @@ namespace S7.Net
//TODO: Fix This
get
{
- return Connect() == ErrorCode.NoError;
+ try
+ {
+ Connect();
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
}
}
@@ -72,16 +80,6 @@ namespace S7.Net
catch { return false; }
}
}
-
- ///
- /// Contains the last error registered when executing a function
- ///
- public string LastErrorString { get; private set; }
-
- ///
- /// Contains the last error code registered when executing a function
- ///
- public ErrorCode LastErrorCode { get; private set; }
///
/// Creates a PLC object with all the parameters needed for connections.
diff --git a/S7.Net/PLCAddress.cs b/S7.Net/PLCAddress.cs
new file mode 100644
index 0000000..e78015f
--- /dev/null
+++ b/S7.Net/PLCAddress.cs
@@ -0,0 +1,197 @@
+namespace S7.Net
+{
+ internal class PLCAddress
+ {
+ private DataType dataType;
+ private int dbNumber;
+ private int startByte;
+ private int bitNumber;
+ private VarType varType;
+
+ public DataType DataType
+ {
+ get => dataType;
+ set => dataType = value;
+ }
+
+ public int DbNumber
+ {
+ get => dbNumber;
+ set => dbNumber = value;
+ }
+
+ public int StartByte
+ {
+ get => startByte;
+ set => startByte = value;
+ }
+
+ public int BitNumber
+ {
+ get => bitNumber;
+ set => bitNumber = value;
+ }
+
+ public VarType VarType
+ {
+ get => varType;
+ set => varType = value;
+ }
+
+ public PLCAddress(string address)
+ {
+ Parse(address, out dataType, out dbNumber, out varType, out startByte, out bitNumber);
+ }
+
+ public static void Parse(string input, out DataType dataType, out int dbNumber, out VarType varType, out int address, out int bitNumber)
+ {
+ bitNumber = -1;
+ dbNumber = 0;
+
+ switch (input.Substring(0, 2))
+ {
+ case "DB":
+ string[] strings = input.Split(new char[] { '.' });
+ if (strings.Length < 2)
+ throw new InvalidAddressException("To few periods for DB address");
+
+ dataType = DataType.DataBlock;
+ dbNumber = int.Parse(strings[0].Substring(2));
+ address = int.Parse(strings[1].Substring(3));
+
+ string dbType = strings[1].Substring(0, 3);
+ switch (dbType)
+ {
+ case "DBB":
+ varType = VarType.Byte;
+ return;
+ case "DBW":
+ varType = VarType.Word;
+ return;
+ case "DBD":
+ varType = VarType.DWord;
+ return;
+ case "DBX":
+ bitNumber = int.Parse(strings[2]);
+ if (bitNumber > 7)
+ throw new InvalidAddressException("Bit can only be 0-7");
+ varType = VarType.Bit;
+ return;
+ default:
+ throw new InvalidAddressException();
+ }
+ case "EB":
+ // Input byte
+ dataType = DataType.Input;
+ dbNumber = 0;
+ address = int.Parse(input.Substring(2));
+ varType = VarType.Byte;
+ return;
+ case "EW":
+ // Input word
+ dataType = DataType.Input;
+ dbNumber = 0;
+ address = int.Parse(input.Substring(2));
+ varType = VarType.Word;
+ return;
+ case "ED":
+ // Input double-word
+ dataType = DataType.Input;
+ dbNumber = 0;
+ address = int.Parse(input.Substring(2));
+ varType = VarType.DWord;
+ return;
+ case "AB":
+ // Output byte
+ dataType = DataType.Output;
+ dbNumber = 0;
+ address = int.Parse(input.Substring(2));
+ varType = VarType.Byte;
+ return;
+ case "AW":
+ // Output word
+ dataType = DataType.Output;
+ dbNumber = 0;
+ address = int.Parse(input.Substring(2));
+ varType = VarType.Word;
+ return;
+ case "AD":
+ // Output double-word
+ dataType = DataType.Output;
+ dbNumber = 0;
+ address = int.Parse(input.Substring(2));
+ varType = VarType.DWord;
+ return;
+ case "MB":
+ // Memory byte
+ dataType = DataType.Memory;
+ dbNumber = 0;
+ address = int.Parse(input.Substring(2));
+ varType = VarType.Byte;
+ return;
+ case "MW":
+ // Memory word
+ dataType = DataType.Memory;
+ dbNumber = 0;
+ address = int.Parse(input.Substring(2));
+ varType = VarType.Word;
+ return;
+ case "MD":
+ // Memory double-word
+ dataType = DataType.Memory;
+ dbNumber = 0;
+ address = int.Parse(input.Substring(2));
+ varType = VarType.DWord;
+ return;
+ default:
+ switch (input.Substring(0, 1))
+ {
+ case "E":
+ case "I":
+ // Input
+ dataType = DataType.Input;
+ varType = VarType.Bit;
+ break;
+ case "A":
+ case "O":
+ // Output
+ dataType = DataType.Output;
+ varType = VarType.Bit;
+ break;
+ case "M":
+ // Memory
+ dataType = DataType.Memory;
+ varType = VarType.Byte;
+ break;
+ case "T":
+ // Timer
+ dataType = DataType.Timer;
+ dbNumber = 0;
+ address = int.Parse(input.Substring(1));
+ varType = VarType.Timer;
+ return;
+ case "Z":
+ case "C":
+ // Counter
+ dataType = DataType.Timer;
+ dbNumber = 0;
+ address = int.Parse(input.Substring(1));
+ varType = VarType.Counter;
+ return;
+ default:
+ throw new InvalidAddressException(string.Format("{0} is not a valid address", input.Substring(0, 1)));
+ }
+
+ string txt2 = input.Substring(1);
+ if (txt2.IndexOf(".") == -1)
+ throw new InvalidAddressException("To few periods for DB address");
+
+ address = int.Parse(txt2.Substring(0, txt2.IndexOf(".")));
+ bitNumber = int.Parse(txt2.Substring(txt2.IndexOf(".") + 1));
+ if (bitNumber > 7)
+ throw new InvalidAddressException("Bit can only be 0-7");
+ return;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/S7.Net/PLCHelpers.cs b/S7.Net/PLCHelpers.cs
index 2136b36..30aaaf0 100644
--- a/S7.Net/PLCHelpers.cs
+++ b/S7.Net/PLCHelpers.cs
@@ -5,167 +5,6 @@ using System.Linq;
namespace S7.Net
{
-
- internal class PLCAddress
- {
- public DataType dataType;
- public int DBNumber;
- public int Address;
- public int BitNumber;
- public VarType varType;
-
- public PLCAddress(string address)
- {
- ParseString(address);
- }
-
- private void ParseString(string address)
- {
- BitNumber = -1;
- switch (address.Substring(0, 2))
- {
- case "DB":
- string[] strings = address.Split(new char[] { '.' });
- if (strings.Length < 2)
- throw new InvalidAddressException("To few periods for DB address");
-
- dataType = DataType.DataBlock;
- DBNumber = int.Parse(strings[0].Substring(2));
- Address = int.Parse(strings[1].Substring(3));
-
- string dbType = strings[1].Substring(0, 3);
- switch (dbType)
- {
- case "DBB":
- varType = VarType.Byte;
- return;
- case "DBW":
- varType = VarType.Word;
- return;
- case "DBD":
- varType = VarType.DWord;
- return;
- case "DBX":
- BitNumber = int.Parse(strings[2]);
- if (BitNumber > 7)
- throw new InvalidAddressException("Bit can only be 0-7");
- varType = VarType.Bit;
- return;
- default:
- throw new InvalidAddressException();
- }
- case "EB":
- // Input byte
- dataType = DataType.Input;
- DBNumber = 0;
- Address = int.Parse(address.Substring(2));
- varType = VarType.Byte;
- return;
- case "EW":
- // Input word
- dataType = DataType.Input;
- DBNumber = 0;
- Address = int.Parse(address.Substring(2));
- varType = VarType.Word;
- return;
- case "ED":
- // Input double-word
- dataType = DataType.Input;
- DBNumber = 0;
- Address = int.Parse(address.Substring(2));
- varType = VarType.DWord;
- return;
- case "AB":
- // Output byte
- dataType = DataType.Output;
- DBNumber = 0;
- Address = int.Parse(address.Substring(2));
- varType = VarType.Byte;
- return;
- case "AW":
- // Output word
- dataType = DataType.Output;
- DBNumber = 0;
- Address = int.Parse(address.Substring(2));
- varType = VarType.Word;
- return;
- case "AD":
- // Output double-word
- dataType = DataType.Output;
- DBNumber = 0;
- Address = int.Parse(address.Substring(2));
- varType = VarType.DWord;
- return;
- case "MB":
- // Memory byte
- dataType = DataType.Memory;
- DBNumber = 0;
- Address = int.Parse(address.Substring(2));
- varType = VarType.Byte;
- return;
- case "MW":
- // Memory word
- dataType = DataType.Memory;
- DBNumber = 0;
- Address = int.Parse(address.Substring(2));
- varType = VarType.Word;
- return;
- case "MD":
- // Memory double-word
- dataType = DataType.Memory;
- DBNumber = 0;
- Address = int.Parse(address.Substring(2));
- varType = VarType.DWord;
- return;
- default:
- switch (address.Substring(0, 1))
- {
- case "E":
- case "I":
- // Input
- dataType = DataType.Input;
- break;
- case "A":
- case "O":
- // Output
- dataType = DataType.Output;
- break;
- case "M":
- // Memory
- dataType = DataType.Memory;
- break;
- case "T":
- // Timer
- dataType = DataType.Timer;
- DBNumber = 0;
- Address = int.Parse(address.Substring(1));
- varType = VarType.Timer;
- return;
- case "Z":
- case "C":
- // Counter
- dataType = DataType.Timer;
- DBNumber = 0;
- Address = int.Parse(address.Substring(1));
- varType = VarType.Counter;
- return;
- default:
- throw new InvalidAddressException(string.Format("{0} is not a valid address", address.Substring(0, 1)));
- }
-
- string txt2 = address.Substring(1);
- if (txt2.IndexOf(".") == -1)
- throw new InvalidAddressException("To few periods for DB address");
-
- Address = int.Parse(txt2.Substring(0, txt2.IndexOf(".")));
- BitNumber = int.Parse(txt2.Substring(txt2.IndexOf(".") + 1));
- if (BitNumber > 7)
- throw new InvalidAddressException("Bit can only be 0-7");
- return;
- }
- }
- }
-
public partial class Plc
{
///
@@ -312,15 +151,6 @@ namespace S7.Net
}
}
- ///
- /// Sets the to and to .
- ///
- public void ClearLastError()
- {
- LastErrorCode = ErrorCode.NoError;
- LastErrorString = string.Empty;
- }
-
///
/// Given a S7 (Bool, Word, DWord, etc.), it returns how many bytes to read.
///
@@ -367,7 +197,7 @@ namespace S7.Net
{
// check for Return Code = Success
if (s7data[offset] != 0xff)
- throw new Exception(ErrorCode.WrongNumberReceivedBytes.ToString());
+ throw new PlcException(ErrorCode.WrongNumberReceivedBytes);
// to Data bytes
offset += 4;
diff --git a/S7.Net/PlcAsynchronous.cs b/S7.Net/PlcAsynchronous.cs
index c829963..38cb7c6 100644
--- a/S7.Net/PlcAsynchronous.cs
+++ b/S7.Net/PlcAsynchronous.cs
@@ -14,10 +14,9 @@ namespace S7.Net
public partial class Plc
{
///
- /// Open a and connects to the PLC, sending all the corrected package
- /// and returning if the connection was successful () of it was wrong.
+ /// Connects to the PLC and performs a COTP ConnectionRequest and S7 CommunicationSetup.
///
- /// Returns ErrorCode.NoError if the connection was successful, otherwise check the ErrorCode
+ /// A task that represents the asynchronous open operation.
public async Task OpenAsync()
{
await ConnectAsync();
@@ -100,7 +99,7 @@ namespace S7.Net
public async Task