diff --git a/S7.Net.UnitTest/S7NetTestsSync.cs b/S7.Net.UnitTest/S7NetTestsSync.cs
index 377506b..c84c1a7 100644
--- a/S7.Net.UnitTest/S7NetTestsSync.cs
+++ b/S7.Net.UnitTest/S7NetTestsSync.cs
@@ -933,7 +933,14 @@ namespace S7.Net.UnitTest
S7TestServer.Stop();
var unreachablePlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 2);
- Assert.IsFalse(unreachablePlc.IsAvailable);
+ try
+ {
+ unreachablePlc.Open();
+ }
+ catch
+ {
+ }
+ Assert.IsFalse(unreachablePlc.IsConnected);
}
[TestMethod]
@@ -944,7 +951,8 @@ namespace S7.Net.UnitTest
S7TestServer.Start(TestServerPort);
var reachablePlc = CreatePlc();
- Assert.IsTrue(reachablePlc.IsAvailable);
+ reachablePlc.Open();
+ Assert.IsTrue(reachablePlc.IsConnected);
}
[TestMethod]
diff --git a/S7.Net.UnitTest/TypeTests/S7StringTests.cs b/S7.Net.UnitTest/TypeTests/S7StringTests.cs
index e1cdc91..8c23135 100644
--- a/S7.Net.UnitTest/TypeTests/S7StringTests.cs
+++ b/S7.Net.UnitTest/TypeTests/S7StringTests.cs
@@ -117,13 +117,24 @@ namespace S7.Net.UnitTest.TypeTests
AssertToByteArrayAndBackEquals("Abc", 4, 4, 3, (byte) 'A', (byte) 'b', (byte) 'c', 0);
}
+ [TestMethod]
+ public void OddS7StringByteLength()
+ {
+ AssertVarTypeToByteLength(VarType.S7String, 1, 4);
+ }
+
+ [TestMethod]
+ public void EvenS7StringByteLength()
+ {
+ AssertVarTypeToByteLength(VarType.S7String, 2, 4);
+ }
+
private static void AssertFromByteArrayEquals(string expected, params byte[] bytes)
{
var convertedString = S7String.FromByteArray(bytes);
Assert.AreEqual(expected, convertedString);
}
-
private static void AssertToByteArrayAndBackEquals(string value, int reservedLength, params byte[] expected)
{
var convertedData = S7String.ToByteArray(value, reservedLength);
@@ -131,5 +142,11 @@ namespace S7.Net.UnitTest.TypeTests
var convertedBack = S7String.FromByteArray(convertedData);
Assert.AreEqual(value, convertedBack);
}
+
+ private void AssertVarTypeToByteLength(VarType varType, int count, int expectedByteLength)
+ {
+ var byteLength = Plc.VarTypeToByteLength(varType, count);
+ Assert.AreEqual(expectedByteLength, byteLength);
+ }
}
}
diff --git a/S7.Net.UnitTest/TypeTests/S7WStringTests.cs b/S7.Net.UnitTest/TypeTests/S7WStringTests.cs
index 877777c..119678c 100644
--- a/S7.Net.UnitTest/TypeTests/S7WStringTests.cs
+++ b/S7.Net.UnitTest/TypeTests/S7WStringTests.cs
@@ -122,6 +122,17 @@ namespace S7.Net.UnitTest.TypeTests
Assert.AreEqual(expected, convertedString);
}
+ [TestMethod]
+ public void OddS7WStringByteLength()
+ {
+ AssertVarTypeToByteLength(VarType.S7WString, 1, 6);
+ }
+
+ [TestMethod]
+ public void EvenS7WStringByteLength()
+ {
+ AssertVarTypeToByteLength(VarType.S7WString, 2, 8);
+ }
private static void AssertToByteArrayAndBackEquals(string value, int reservedLength, params byte[] expected)
{
@@ -130,5 +141,11 @@ namespace S7.Net.UnitTest.TypeTests
var convertedBack = S7WString.FromByteArray(convertedData);
Assert.AreEqual(value, convertedBack);
}
+
+ private void AssertVarTypeToByteLength(VarType varType, int count, int expectedByteLength)
+ {
+ var byteLength = Plc.VarTypeToByteLength(varType, count);
+ Assert.AreEqual(expectedByteLength, byteLength);
+ }
}
}
diff --git a/S7.Net/PLC.cs b/S7.Net/PLC.cs
index f3a497e..8882a13 100644
--- a/S7.Net/PLC.cs
+++ b/S7.Net/PLC.cs
@@ -75,47 +75,26 @@ namespace S7.Net
if (tcpClient != null) tcpClient.SendTimeout = writeTimeout;
}
}
-
- ///
- /// Returns true if a connection to the PLC can be established
- ///
- public bool IsAvailable
- {
- //TODO: Fix This
- get
- {
- try
- {
- OpenAsync().GetAwaiter().GetResult();
- return true;
- }
- catch
- {
- return false;
- }
- }
- }
///
- /// Checks if the socket is connected and polls the other peer (the PLC) to see if it's connected.
- /// This is the variable that you should continously check to see if the communication is working
- /// See also: http://stackoverflow.com/questions/2661764/how-to-check-if-a-socket-is-connected-disconnected-in-c
+ /// Gets a value indicating whether a connection to the PLC has been established.
///
- public bool IsConnected
- {
- get
- {
- try
- {
- if (tcpClient == null)
- return false;
-
- //TODO: Actually check communication by sending an empty TPDU
- return tcpClient.Connected;
- }
- catch { return false; }
- }
- }
+ ///
+ /// The property gets the connection state of the Client socket as
+ /// of the last I/O operation. When it returns false, the Client socket was either
+ /// never connected, or is no longer connected.
+ ///
+ ///
+ /// Because the property only reflects the state of the connection
+ /// as of the most recent operation, you should attempt to send or receive a message to
+ /// determine the current state. After the message send fails, this property no longer
+ /// returns true. Note that this behavior is by design. You cannot reliably test the
+ /// state of the connection because, in the time between the test and a send/receive, the
+ /// connection could have been lost. Your code should assume the socket is connected, and
+ /// gracefully handle failed transmissions.
+ ///
+ ///
+ public bool IsConnected => tcpClient?.Connected ?? false;
///
/// Creates a PLC object with all the parameters needed for connections.
diff --git a/S7.Net/PLCHelpers.cs b/S7.Net/PLCHelpers.cs
index 4c4f8ac..07611cb 100644
--- a/S7.Net/PLCHelpers.cs
+++ b/S7.Net/PLCHelpers.cs
@@ -190,7 +190,9 @@ namespace S7.Net
case VarType.String:
return varCount;
case VarType.S7String:
- return varCount + 2;
+ return ((varCount + 2) & 1) == 1 ? (varCount + 3) : (varCount + 2);
+ case VarType.S7WString:
+ return (varCount * 2) + 4;
case VarType.Word:
case VarType.Timer:
case VarType.Int:
diff --git a/S7.Net/PlcAsynchronous.cs b/S7.Net/PlcAsynchronous.cs
index 42b61a7..de8f0c8 100644
--- a/S7.Net/PlcAsynchronous.cs
+++ b/S7.Net/PlcAsynchronous.cs
@@ -104,7 +104,7 @@ namespace S7.Net
{
//This works up to MaxPDUSize-1 on SNAP7. But not MaxPDUSize-0.
var maxToRead = Math.Min(count, MaxPDUSize - 18);
- await ReadBytesWithSingleRequestAsync(dataType, db, startByteAdr + index, resultBytes, index, maxToRead, cancellationToken);
+ await ReadBytesWithSingleRequestAsync(dataType, db, startByteAdr + index, resultBytes, index, maxToRead, cancellationToken).ConfigureAwait(false);
count -= maxToRead;
index += maxToRead;
}
@@ -127,7 +127,7 @@ namespace S7.Net
public async Task