8 Commits

Author SHA1 Message Date
Michele Cattafesta
f6a2e11045 0.1.8 2018-02-05 20:07:11 +00:00
Michele Cattafesta
8005304827 Merge pull request #97 from GS770/master
Added Bit to Types
2017-12-14 23:00:19 +00:00
shen.jz
ef5e060948 Added Bit to Types
get bool or BitArray from byte array
2017-12-14 10:38:10 +08:00
Michele Cattafesta
3178d2aa09 Merge pull request #96 from GS770/master
Get bit of read bytes
2017-12-13 08:57:52 +00:00
shen.jz
6a2bc708a9 Get bit of read bytes 2017-12-12 15:56:18 +08:00
Michele Cattafesta
ea84cd414f Fix UWP build 2017-09-14 18:26:49 +01:00
Michele Cattafesta
ad98783888 Added one more test 2017-09-14 18:20:06 +01:00
Michele Cattafesta
cddb826053 Fix V0.1.6 bit write to DB don't work #91 2017-09-14 18:16:35 +01:00
14 changed files with 151 additions and 17 deletions

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">C:\Users\shade\Documents\GitHub\s7netplus\S7.Net.Core\project.lock.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\shade\.nuget\packages\</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">ProjectJson</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">4.3.1</NuGetToolVersion>
</PropertyGroup>
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
</PropertyGroup>
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<Import Project="$(NuGetPackageRoot)microsoft.net.native.sharedlibrary-x86\1.7.0\build\Microsoft.Net.Native.SharedLibrary-x86.props" Condition="Exists('$(NuGetPackageRoot)microsoft.net.native.sharedlibrary-x86\1.7.0\build\Microsoft.Net.Native.SharedLibrary-x86.props')" />
<Import Project="$(NuGetPackageRoot)microsoft.net.native.sharedlibrary-x64\1.7.0\build\Microsoft.Net.Native.SharedLibrary-x64.props" Condition="Exists('$(NuGetPackageRoot)microsoft.net.native.sharedlibrary-x64\1.7.0\build\Microsoft.Net.Native.SharedLibrary-x64.props')" />
<Import Project="$(NuGetPackageRoot)microsoft.net.native.sharedlibrary-arm\1.7.0\build\Microsoft.Net.Native.SharedLibrary-arm.props" Condition="Exists('$(NuGetPackageRoot)microsoft.net.native.sharedlibrary-arm\1.7.0\build\Microsoft.Net.Native.SharedLibrary-arm.props')" />
<Import Project="$(NuGetPackageRoot)microsoft.net.native.compiler\1.7.0\build\Microsoft.Net.Native.Compiler.props" Condition="Exists('$(NuGetPackageRoot)microsoft.net.native.compiler\1.7.0\build\Microsoft.Net.Native.Compiler.props')" />
</ImportGroup>
</Project>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
</PropertyGroup>
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<Import Project="$(NuGetPackageRoot)microsoft.net.native.sharedlibrary-x86\1.7.0\build\Microsoft.Net.Native.SharedLibrary-x86.targets" Condition="Exists('$(NuGetPackageRoot)microsoft.net.native.sharedlibrary-x86\1.7.0\build\Microsoft.Net.Native.SharedLibrary-x86.targets')" />
<Import Project="$(NuGetPackageRoot)microsoft.net.native.sharedlibrary-x64\1.7.0\build\Microsoft.Net.Native.SharedLibrary-x64.targets" Condition="Exists('$(NuGetPackageRoot)microsoft.net.native.sharedlibrary-x64\1.7.0\build\Microsoft.Net.Native.SharedLibrary-x64.targets')" />
<Import Project="$(NuGetPackageRoot)microsoft.net.native.sharedlibrary-arm\1.7.0\build\Microsoft.Net.Native.SharedLibrary-arm.targets" Condition="Exists('$(NuGetPackageRoot)microsoft.net.native.sharedlibrary-arm\1.7.0\build\Microsoft.Net.Native.SharedLibrary-arm.targets')" />
<Import Project="$(NuGetPackageRoot)microsoft.net.native.compiler\1.7.0\build\Microsoft.Net.Native.Compiler.targets" Condition="Exists('$(NuGetPackageRoot)microsoft.net.native.compiler\1.7.0\build\Microsoft.Net.Native.Compiler.targets')" />
</ImportGroup>
</Project>

View File

@@ -1,9 +1,9 @@
{
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0"
"Microsoft.NETCore.UniversalWindowsPlatform": "5.4.0"
},
"frameworks": {
"uap10.0": {}
"uap10.0.10240": {}
},
"runtimes": {
"win10-arm": {},

View File

@@ -6,7 +6,7 @@ namespace S7.Net.UnitTest.Helpers
class S7TestServer
{
static S7Server Server;
static private byte[] DB1 = new byte[512]; // Our DB1
static private byte[] DB1 = new byte[1024]; // Our DB1
static private byte[] DB2 = new byte[64000]; // Our DB2
static private byte[] DB3 = new byte[1024]; // Our DB3

View File

@@ -455,6 +455,18 @@ namespace S7.Net.UnitTest
plc.Write("DB1.DBX0.7", 0);
boolVariable = (bool)plc.Read("DB1.DBX0.7");
Assert.IsFalse(boolVariable);
plc.Write("DB1.DBX658.0", 1);
boolVariable = (bool)plc.Read("DB1.DBX658.0");
Assert.IsTrue(boolVariable);
plc.Write("DB1.DBX658.7", 1);
boolVariable = (bool)plc.Read("DB1.DBX658.7");
Assert.IsTrue(boolVariable);
plc.Write("DB2.DBX9658.0", 1);
boolVariable = (bool)plc.Read("DB2.DBX9658.0");
Assert.IsTrue(boolVariable);
}
[TestMethod]
@@ -664,8 +676,8 @@ namespace S7.Net.UnitTest
Assert.AreEqual(expectedReadBytes, actualReadBytes);
}
[TestMethod]
[TestMethod]
public void T22_ReadClassWithArray()
{
Assert.IsTrue(plc.IsConnected, "Before executing this test, the plc must be connected. Check constructor.");

View File

@@ -125,7 +125,7 @@ namespace S7.Net
catch (SocketException sex)
{
// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
if (sex.ErrorCode == CONNECTION_TIMED_OUT_ERROR_CODE)
if (sex.SocketErrorCode == SocketError.TimedOut)
{
LastErrorCode = ErrorCode.IPAddressNotAvailable;
}
@@ -352,13 +352,14 @@ namespace S7.Net
/// <param name="db">Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc.</param>
/// <param name="startByteAdr">Start byte address. If you want to read DB1.DBW200, this is 200.</param>
/// <param name="varType">Type of the variable/s that you are reading</param>
/// <param name="bitAdr">Address of bit. If you want to read DB1.DBX200.6, set 6 to this parameter.</param>
/// <param name="varCount"></param>
public object Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount)
public object Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount, byte bitAdr = 0)
{
int cntBytes = VarTypeToByteLength(varType, varCount);
byte[] bytes = ReadBytes(dataType, db, startByteAdr, cntBytes);
return ParseBytes(varType, bytes, varCount);
return ParseBytes(varType, bytes, varCount, bitAdr);
}
/// <summary>
@@ -1136,7 +1137,7 @@ namespace S7.Net
package.Add(Types.Word.ToByteArray((ushort)varCount));
package.Add(Types.Word.ToByteArray((ushort)(db)));
package.Add((byte)dataType);
var overflow = (int)(startByteAdr * 8 + bitAdr / 0xffffU); // handles words with address bigger than 8191
var overflow = (int)(startByteAdr * 8 / 0xffffU); // handles words with address bigger than 8191
package.Add((byte)overflow);
package.Add(Types.Word.ToByteArray((ushort)(startByteAdr * 8 + bitAdr)));
package.Add(new byte[] { 0, 0x03 }); //ending 0x03 is used for writing a sinlge bit
@@ -1169,8 +1170,9 @@ namespace S7.Net
/// <param name="varType"></param>
/// <param name="bytes"></param>
/// <param name="varCount"></param>
/// <param name="bitAdr"></param>
/// <returns></returns>
private object ParseBytes(VarType varType, byte[] bytes, int varCount)
private object ParseBytes(VarType varType, byte[] bytes, int varCount, byte bitAdr = 0)
{
if (bytes == null) return null;
@@ -1219,7 +1221,13 @@ namespace S7.Net
else
return Types.Counter.ToArray(bytes);
case VarType.Bit:
return null; //TODO
if (varCount == 1)
if (bitAdr > 7)
return null;
else
return Types.Bit.FromByte(bytes[0], bitAdr);
else
return Types.Bit.ToBitArray(bytes);
default:
return null;
}

View File

@@ -80,6 +80,7 @@
<Compile Include="Enums.cs" />
<Compile Include="PLC.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Types\Bit.cs" />
<Compile Include="Types\Boolean.cs" />
<Compile Include="Types\Byte.cs" />
<Compile Include="Types\ByteArray.cs" />

29
S7.Net/Types/Bit.cs Normal file
View File

@@ -0,0 +1,29 @@
using System;
using System.Collections;
namespace S7.Net.Types
{
/// <summary>
/// Contains the conversion methods to convert Bit from S7 plc to C#.
/// </summary>
public static class Bit
{
/// <summary>
/// Converts a Bit to bool
/// </summary>
public static bool FromByte(byte v, byte bitAdr)
{
BitArray bitArr = new BitArray(new byte[] { v });
return bitArr[bitAdr];
}
/// <summary>
/// Converts an array of bytes to a BitArray
/// </summary>
public static BitArray ToBitArray(byte[] bytes)
{
BitArray bitArr = new BitArray(bytes);
return bitArr;
}
}
}

Binary file not shown.

View File

@@ -343,7 +343,7 @@
<param name="count">Byte count, if you want to read 120 bytes, set this to 120.</param>
<returns>Returns the bytes in an array</returns>
</member>
<member name="M:S7.Net.Plc.Read(S7.Net.DataType,System.Int32,System.Int32,S7.Net.VarType,System.Int32)">
<member name="M:S7.Net.Plc.Read(S7.Net.DataType,System.Int32,System.Int32,S7.Net.VarType,System.Int32,System.Byte)">
<summary>
Read and decode a certain number of bytes of the "VarType" provided.
This can be used to read multiple consecutive variables of the same type (Word, DWord, Int, etc).
@@ -353,6 +353,7 @@
<param name="db">Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc.</param>
<param name="startByteAdr">Start byte address. If you want to read DB1.DBW200, this is 200.</param>
<param name="varType">Type of the variable/s that you are reading</param>
<param name="bitAdr">Address of bit. If you want to read DB1.DBX200.6, set 6 to this parameter.</param>
<param name="varCount"></param>
</member>
<member name="M:S7.Net.Plc.Read(System.String)">
@@ -520,13 +521,14 @@
<param name="value">Bytes to write. The lenght of this parameter can't be higher than 200. If you need more, use recursion.</param>
<returns>NoError if it was successful, or the error is specified</returns>
</member>
<member name="M:S7.Net.Plc.ParseBytes(S7.Net.VarType,System.Byte[],System.Int32)">
<member name="M:S7.Net.Plc.ParseBytes(S7.Net.VarType,System.Byte[],System.Int32,System.Byte)">
<summary>
Given a S7 variable type (Bool, Word, DWord, etc.), it converts the bytes in the appropriate C# format.
</summary>
<param name="varType"></param>
<param name="bytes"></param>
<param name="varCount"></param>
<param name="bitAdr"></param>
<returns></returns>
</member>
<member name="M:S7.Net.Plc.VarTypeToByteLength(S7.Net.VarType,System.Int32)">
@@ -542,6 +544,21 @@
Releases all resources, disonnects from the plc and closes the socket
</summary>
</member>
<member name="T:S7.Net.Types.Bit">
<summary>
Contains the conversion methods to convert Bit from S7 plc to C#.
</summary>
</member>
<member name="M:S7.Net.Types.Bit.FromByte(System.Byte,System.Byte)">
<summary>
Converts a Bit to bool
</summary>
</member>
<member name="M:S7.Net.Types.Bit.ToBitArray(System.Byte[])">
<summary>
Converts an array of bytes to a BitArray
</summary>
</member>
<member name="T:S7.Net.Types.Boolean">
<summary>
Contains the methods to read, set and reset bits inside bytes

Binary file not shown.

View File

@@ -343,7 +343,7 @@
<param name="count">Byte count, if you want to read 120 bytes, set this to 120.</param>
<returns>Returns the bytes in an array</returns>
</member>
<member name="M:S7.Net.Plc.Read(S7.Net.DataType,System.Int32,System.Int32,S7.Net.VarType,System.Int32)">
<member name="M:S7.Net.Plc.Read(S7.Net.DataType,System.Int32,System.Int32,S7.Net.VarType,System.Int32,System.Byte)">
<summary>
Read and decode a certain number of bytes of the "VarType" provided.
This can be used to read multiple consecutive variables of the same type (Word, DWord, Int, etc).
@@ -353,6 +353,7 @@
<param name="db">Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc.</param>
<param name="startByteAdr">Start byte address. If you want to read DB1.DBW200, this is 200.</param>
<param name="varType">Type of the variable/s that you are reading</param>
<param name="bitAdr">Address of bit. If you want to read DB1.DBX200.6, set 6 to this parameter.</param>
<param name="varCount"></param>
</member>
<member name="M:S7.Net.Plc.Read(System.String)">
@@ -520,13 +521,14 @@
<param name="value">Bytes to write. The lenght of this parameter can't be higher than 200. If you need more, use recursion.</param>
<returns>NoError if it was successful, or the error is specified</returns>
</member>
<member name="M:S7.Net.Plc.ParseBytes(S7.Net.VarType,System.Byte[],System.Int32)">
<member name="M:S7.Net.Plc.ParseBytes(S7.Net.VarType,System.Byte[],System.Int32,System.Byte)">
<summary>
Given a S7 variable type (Bool, Word, DWord, etc.), it converts the bytes in the appropriate C# format.
</summary>
<param name="varType"></param>
<param name="bytes"></param>
<param name="varCount"></param>
<param name="bitAdr"></param>
<returns></returns>
</member>
<member name="M:S7.Net.Plc.VarTypeToByteLength(S7.Net.VarType,System.Int32)">
@@ -542,6 +544,21 @@
Releases all resources, disonnects from the plc and closes the socket
</summary>
</member>
<member name="T:S7.Net.Types.Bit">
<summary>
Contains the conversion methods to convert Bit from S7 plc to C#.
</summary>
</member>
<member name="M:S7.Net.Types.Bit.FromByte(System.Byte,System.Byte)">
<summary>
Converts a Bit to bool
</summary>
</member>
<member name="M:S7.Net.Types.Bit.ToBitArray(System.Byte[])">
<summary>
Converts an array of bytes to a BitArray
</summary>
</member>
<member name="T:S7.Net.Types.Boolean">
<summary>
Contains the methods to read, set and reset bits inside bytes

Binary file not shown.

View File

@@ -343,7 +343,7 @@
<param name="count">Byte count, if you want to read 120 bytes, set this to 120.</param>
<returns>Returns the bytes in an array</returns>
</member>
<member name="M:S7.Net.Plc.Read(S7.Net.DataType,System.Int32,System.Int32,S7.Net.VarType,System.Int32)">
<member name="M:S7.Net.Plc.Read(S7.Net.DataType,System.Int32,System.Int32,S7.Net.VarType,System.Int32,System.Byte)">
<summary>
Read and decode a certain number of bytes of the "VarType" provided.
This can be used to read multiple consecutive variables of the same type (Word, DWord, Int, etc).
@@ -353,6 +353,7 @@
<param name="db">Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc.</param>
<param name="startByteAdr">Start byte address. If you want to read DB1.DBW200, this is 200.</param>
<param name="varType">Type of the variable/s that you are reading</param>
<param name="bitAdr">Address of bit. If you want to read DB1.DBX200.6, set 6 to this parameter.</param>
<param name="varCount"></param>
</member>
<member name="M:S7.Net.Plc.Read(System.String)">
@@ -520,13 +521,14 @@
<param name="value">Bytes to write. The lenght of this parameter can't be higher than 200. If you need more, use recursion.</param>
<returns>NoError if it was successful, or the error is specified</returns>
</member>
<member name="M:S7.Net.Plc.ParseBytes(S7.Net.VarType,System.Byte[],System.Int32)">
<member name="M:S7.Net.Plc.ParseBytes(S7.Net.VarType,System.Byte[],System.Int32,System.Byte)">
<summary>
Given a S7 variable type (Bool, Word, DWord, etc.), it converts the bytes in the appropriate C# format.
</summary>
<param name="varType"></param>
<param name="bytes"></param>
<param name="varCount"></param>
<param name="bitAdr"></param>
<returns></returns>
</member>
<member name="M:S7.Net.Plc.VarTypeToByteLength(S7.Net.VarType,System.Int32)">
@@ -542,6 +544,21 @@
Releases all resources, disonnects from the plc and closes the socket
</summary>
</member>
<member name="T:S7.Net.Types.Bit">
<summary>
Contains the conversion methods to convert Bit from S7 plc to C#.
</summary>
</member>
<member name="M:S7.Net.Types.Bit.FromByte(System.Byte,System.Byte)">
<summary>
Converts a Bit to bool
</summary>
</member>
<member name="M:S7.Net.Types.Bit.ToBitArray(System.Byte[])">
<summary>
Converts an array of bytes to a BitArray
</summary>
</member>
<member name="T:S7.Net.Types.Boolean">
<summary>
Contains the methods to read, set and reset bits inside bytes