documentation, cleanup and formatting + output xml documentation.

This commit is contained in:
Michele Cattafesta
2016-08-26 13:06:34 +02:00
parent b4d99010fb
commit 7012205c3e
17 changed files with 383 additions and 97 deletions

View File

@@ -3,6 +3,9 @@ using System.Globalization;
namespace S7.Net
{
/// <summary>
/// Conversion methods to convert from Siemens numeric format to C# and back
/// </summary>
public static class Conversion
{
/// <summary>
@@ -25,6 +28,11 @@ namespace S7.Net
return ret;
}
/// <summary>
/// Converts a binary string to a byte. Can return null.
/// </summary>
/// <param name="txt"></param>
/// <returns></returns>
public static byte? BinStringToByte(this string txt)
{
int cnt = 0;

View File

@@ -1,51 +1,176 @@
namespace S7.Net
{
/// <summary>
/// Types of S7 cpu supported by the library
/// </summary>
public enum CpuType
{
/// <summary>
/// S7 200 cpu type
/// </summary>
S7200 = 0,
/// <summary>
/// S7 300 cpu type
/// </summary>
S7300 = 10,
/// <summary>
/// S7 400 cpu type
/// </summary>
S7400 = 20,
/// <summary>
/// S7 1200 cpu type
/// </summary>
S71200 = 30,
/// <summary>
/// S7 1500 cpu type
/// </summary>
S71500 = 40,
}
/// <summary>
/// Types of error code that can be set after a function is called
/// </summary>
public enum ErrorCode
{
/// <summary>
/// The function has been executed correctly
/// </summary>
NoError = 0,
/// <summary>
/// Wrong type of CPU error
/// </summary>
WrongCPU_Type = 1,
/// <summary>
/// Connection error
/// </summary>
ConnectionError = 2,
/// <summary>
/// Ip address not available
/// </summary>
IPAddressNotAvailable,
/// <summary>
/// Wrong format of the variable
/// </summary>
WrongVarFormat = 10,
/// <summary>
/// Wrong number of received bytes
/// </summary>
WrongNumberReceivedBytes = 11,
/// <summary>
/// Error on send data
/// </summary>
SendData = 20,
/// <summary>
/// Error on read data
/// </summary>
ReadData = 30,
/// <summary>
/// Error on write data
/// </summary>
WriteData = 50
}
/// <summary>
/// Types of memory area that can be read
/// </summary>
public enum DataType
{
/// <summary>
/// Input area memory
/// </summary>
Input = 129,
/// <summary>
/// Output area memory
/// </summary>
Output = 130,
/// <summary>
/// Merkers area memory (M0, M0.0, ...)
/// </summary>
Memory = 131,
/// <summary>
/// DB area memory (DB1, DB2, ...)
/// </summary>
DataBlock = 132,
/// <summary>
/// Timer area memory(T1, T2, ...)
/// </summary>
Timer = 29,
/// <summary>
/// Counter area memory (C1, C2, ...)
/// </summary>
Counter = 28
}
/// <summary>
/// Types
/// </summary>
public enum VarType
{
/// <summary>
/// S7 Bit variable type (bool)
/// </summary>
Bit,
/// <summary>
/// S7 Byte variable type (8 bits)
/// </summary>
Byte,
/// <summary>
/// S7 Word variable type (16 bits, 2 bytes)
/// </summary>
Word,
/// <summary>
/// S7 DWord variable type (32 bits, 4 bytes)
/// </summary>
DWord,
/// <summary>
/// S7 Int variable type (16 bits, 2 bytes)
/// </summary>
Int,
/// <summary>
/// DInt variable type (32 bits, 4 bytes)
/// </summary>
DInt,
/// <summary>
/// Real variable type (32 bits, 4 bytes)
/// </summary>
Real,
/// <summary>
/// String variable type (variable)
/// </summary>
String,
/// <summary>
/// Timer variable type
/// </summary>
Timer,
/// <summary>
/// Counter variable type
/// </summary>
Counter
}
}

View File

@@ -11,6 +11,9 @@ using Double = System.Double;
namespace S7.Net
{
/// <summary>
/// Creates an instance of S7.Net driver
/// </summary>
public class Plc : IDisposable
{
private Socket _mSocket; //TCP connection to device
@@ -343,7 +346,7 @@ 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="varCount">Number of
/// <param name="varCount"></param>
public object Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount)
{
int cntBytes = VarTypeToByteLength(varType, varCount);
@@ -1090,19 +1093,21 @@ namespace S7.Net
#region IDisposable members
/// <summary>
/// Releases all resources, disonnects from the plc and closes the socket
/// </summary>
public void Dispose()
{
if (_mSocket != null)
{
if (_mSocket.Connected)
{
//Close() performs a Dispose on the socket.
_mSocket.Shutdown(SocketShutdown.Both);
_mSocket.Close();
}
//((IDisposable)_mSocket).Dispose();
}
}
#endregion
#endregion
}
}

View File

@@ -43,6 +43,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<DocumentationFile>bin\Debug\S7.Net.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@@ -52,6 +53,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<DocumentationFile>bin\Release\S7.Net.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>

View File

@@ -2,8 +2,14 @@
namespace S7.Net.Types
{
/// <summary>
/// Contains the methods to read, set and reset bits inside bytes
/// </summary>
public static class Boolean
{
/// <summary>
/// Returns the value of a bit in a bit, given the address of the bit
/// </summary>
public static bool GetValue(byte value, int bit)
{
if ((value & (int)Math.Pow(2, bit)) != 0)
@@ -12,11 +18,17 @@ namespace S7.Net.Types
return false;
}
/// <summary>
/// Sets the value of a bit to 1 (true), given the address of the bit
/// </summary>
public static byte SetBit(byte value, int bit)
{
return (byte)(value | (byte)Math.Pow(2, bit));
}
/// <summary>
/// Resets the value of a bit to 0 (false), given the address of the bit
/// </summary>
public static byte ClearBit(byte value, int bit)
{
return (byte)(value & (byte)(~(byte)Math.Pow(2, bit)));

View File

@@ -1,16 +1,26 @@
namespace S7.Net.Types
using System;
namespace S7.Net.Types
{
/// <summary>
/// Contains the methods to convert from bytes to byte arrays
/// </summary>
public static class Byte
{
// publics
#region ToByteArray
/// <summary>
/// Converts a byte to byte array
/// </summary>
public static byte[] ToByteArray(byte value)
{
byte[] bytes = new byte[] { value};
return bytes;
}
#endregion
#region FromByteArray
/// <summary>
/// Converts a byte array to byte
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static byte FromByteArray(byte[] bytes)
{
if (bytes.Length != 1)
@@ -19,6 +29,6 @@
}
return bytes[0];
}
#endregion
}
}

View File

@@ -3,6 +3,9 @@ using System.Reflection;
namespace S7.Net.Types
{
/// <summary>
/// Contains the methods to convert a C# class to S7 data types
/// </summary>
public static class Class
{
/// <summary>

View File

@@ -2,10 +2,14 @@
namespace S7.Net.Types
{
/// <summary>
/// Contains the conversion methods to convert Counter from S7 plc to C# ushort (UInt16).
/// </summary>
public static class Counter
{
// publics
#region FromByteArray
/// <summary>
/// Converts a Counter (2 bytes) to ushort (UInt16)
/// </summary>
public static UInt16 FromByteArray(byte[] bytes)
{
if (bytes.Length != 2)
@@ -16,15 +20,18 @@ namespace S7.Net.Types
// bytes[1] -> LowByte
return FromBytes(bytes[1], bytes[0]);
}
#endregion
#region FromBytes
/// <summary>
/// Converts a Counter (2 bytes) to ushort (UInt16)
/// </summary>
public static UInt16 FromBytes(byte LoVal, byte HiVal)
{
return (UInt16)(HiVal * 256 + LoVal);
}
#endregion
#region ToByteArray
/// <summary>
/// Converts a ushort (UInt16) to word (2 bytes)
/// </summary>
public static byte[] ToByteArray(UInt16 value)
{
byte[] bytes = new byte[2];
@@ -41,6 +48,9 @@ namespace S7.Net.Types
return bytes;
}
/// <summary>
/// Converts an array of ushort (UInt16) to an array of bytes
/// </summary>
public static byte[] ToByteArray(UInt16[] value)
{
ByteArray arr = new ByteArray();
@@ -48,8 +58,10 @@ namespace S7.Net.Types
arr.Add(ToByteArray(val));
return arr.array;
}
#endregion
#region ToArray
/// <summary>
/// Converts an array of bytes to an array of ushort
/// </summary>
public static UInt16[] ToArray(byte[] bytes)
{
UInt16[] values = new UInt16[bytes.Length / 2];
@@ -60,6 +72,5 @@ namespace S7.Net.Types
return values;
}
#endregion
}
}

View File

@@ -2,10 +2,14 @@
namespace S7.Net.Types
{
/// <summary>
/// Contains the conversion methods to convert DInt from S7 plc to C# int (Int32).
/// </summary>
public static class DInt
{
// publics
#region FromByteArray
/// <summary>
/// Converts a S7 DInt (4 bytes) to int (Int32)
/// </summary>
public static Int32 FromByteArray(byte[] bytes)
{
if (bytes.Length != 4)
@@ -14,15 +18,18 @@ namespace S7.Net.Types
}
return FromBytes(bytes[3], bytes[2], bytes[1], bytes[0]);
}
#endregion
#region FromBytes
/// <summary>
/// Converts a S7 DInt (4 bytes) to int (Int32)
/// </summary>
public static Int32 FromBytes(byte v1, byte v2, byte v3, byte v4)
{
return (Int32)(v1 + v2 * Math.Pow(2, 8) + v3 * Math.Pow(2, 16) + v4 * Math.Pow(2, 24));
}
#endregion
#region ToByteArray
/// <summary>
/// Converts a int (Int32) to S7 DInt (4 bytes)
/// </summary>
public static byte[] ToByteArray(Int32 value)
{
byte[] bytes = new byte[4];
@@ -39,6 +46,9 @@ namespace S7.Net.Types
return bytes;
}
/// <summary>
/// Converts an array of int (Int32) to an array of bytes
/// </summary>
public static byte[] ToByteArray(Int32[] value)
{
ByteArray arr = new ByteArray();
@@ -46,8 +56,10 @@ namespace S7.Net.Types
arr.Add(ToByteArray(val));
return arr.array;
}
#endregion
#region ToArray
/// <summary>
/// Converts an array of S7 DInt to an array of int (Int32)
/// </summary>
public static Int32[] ToArray(byte[] bytes)
{
Int32[] values = new Int32[bytes.Length / 4];
@@ -58,9 +70,10 @@ namespace S7.Net.Types
return values;
}
#endregion
// conversion
/// <summary>
/// Converts from C# long (Int64) to C# int (Int32)
/// </summary>
public static Int32 CDWord(Int64 value)
{
if (value > Int32.MaxValue)

View File

@@ -2,23 +2,30 @@
namespace S7.Net.Types
{
/// <summary>
/// Contains the conversion methods to convert DWord from S7 plc to C#.
/// </summary>
public static class DWord
{
// publics
#region FromByteArray
/// <summary>
/// Converts a S7 DWord (4 bytes) to uint (UInt32)
/// </summary>
public static UInt32 FromByteArray(byte[] bytes)
{
return FromBytes(bytes[3], bytes[2], bytes[1], bytes[0]);
}
#endregion
#region FromBytes
/// <summary>
/// Converts a S7 DWord (4 bytes) to uint (UInt32)
/// </summary>
public static UInt32 FromBytes(byte v1, byte v2, byte v3, byte v4)
{
return (UInt32)(v1 + v2 * Math.Pow(2, 8) + v3 * Math.Pow(2, 16) + v4 * Math.Pow(2, 24));
}
#endregion
#region ToByteArray
/// <summary>
/// Converts a uint (UInt32) to S7 DWord (4 bytes)
/// </summary>
public static byte[] ToByteArray(UInt32 value)
{
byte[] bytes = new byte[4];
@@ -35,6 +42,9 @@ namespace S7.Net.Types
return bytes;
}
/// <summary>
/// Converts an array of uint (UInt32) to an array of S7 DWord (4 bytes)
/// </summary>
public static byte[] ToByteArray(UInt32[] value)
{
ByteArray arr = new ByteArray();
@@ -42,8 +52,10 @@ namespace S7.Net.Types
arr.Add(ToByteArray(val));
return arr.array;
}
#endregion
#region ToArray
/// <summary>
/// Converts an array of S7 DWord to an array of uint (UInt32)
/// </summary>
public static UInt32[] ToArray(byte[] bytes)
{
UInt32[] values = new UInt32[bytes.Length / 4];
@@ -54,6 +66,5 @@ namespace S7.Net.Types
return values;
}
#endregion
}
}

View File

@@ -1,17 +1,46 @@
namespace S7.Net.Types
{
/// <summary>
/// Create an instance of a memory block that can be read by using ReadMultipleVars
/// </summary>
public class DataItem
{
/// <summary>
/// Memory area to read
/// </summary>
public DataType DataType { get; set; }
/// <summary>
/// Type of data to be read (default is bytes)
/// </summary>
public VarType VarType { get; set; }
/// <summary>
/// Address of memory area to read (example: for DB1 this value is 1, for T45 this value is 45)
/// </summary>
public int DB { get; set; }
/// <summary>
/// Address of the first byte to read
/// </summary>
public int StartByteAdr { get; set; }
/// <summary>
/// Number of variables to read
/// </summary>
public int Count { get; set; }
/// <summary>
/// Contains the value of the memory area after the read has been executed
/// </summary>
public object Value { get; set; }
/// <summary>
/// Create an instance of DataItem
/// </summary>
public DataItem()
{
VarType = VarType.Byte;
Count = 1;
}
}

View File

@@ -2,10 +2,14 @@
namespace S7.Net.Types
{
/// <summary>
/// Contains the conversion methods to convert Real from S7 plc to C# double.
/// </summary>
public static class Double
{
// publics
#region FromByteArray
/// <summary>
/// Converts a S7 Real (4 bytes) to double
/// </summary>
public static double FromByteArray(byte[] bytes)
{
if (bytes.Length != 4)
@@ -45,8 +49,10 @@ namespace S7.Net.Types
return Math.Pow((-1), vz) * Math.Pow(2, (exd - 127)) * mantisse;
}
}
#endregion
#region FromDWord
/// <summary>
/// Converts a S7 DInt to double
/// </summary>
public static double FromDWord(Int32 value)
{
byte[] b = DInt.ToByteArray(value);
@@ -54,15 +60,20 @@ namespace S7.Net.Types
return d;
}
/// <summary>
/// Converts a S7 DWord to double
/// </summary>
public static double FromDWord(UInt32 value)
{
byte[] b = DWord.ToByteArray(value);
double d = FromByteArray(b);
return d;
}
#endregion
#region ToByteArray
/// <summary>
/// Converts a double to S7 Real (4 bytes)
/// </summary>
public static byte[] ToByteArray(double value)
{
double wert = (double)value;
@@ -109,6 +120,9 @@ namespace S7.Net.Types
return bytes;
}
/// <summary>
/// Converts an array of double to an array of bytes
/// </summary>
public static byte[] ToByteArray(double[] value)
{
ByteArray arr = new ByteArray();
@@ -116,8 +130,10 @@ namespace S7.Net.Types
arr.Add(ToByteArray(val));
return arr.array;
}
#endregion
#region ToArray
/// <summary>
/// Converts an array of S7 Real to an array of double
/// </summary>
public static double[] ToArray(byte[] bytes)
{
double[] values = new double[bytes.Length / 4];
@@ -128,10 +144,8 @@ namespace S7.Net.Types
return values;
}
#endregion
// privates
#region ValToBinString
private static string ValToBinString(byte value)
{
string txt = "";
@@ -145,8 +159,7 @@ namespace S7.Net.Types
}
return txt;
}
#endregion
#region BinStringToByte
private static byte? BinStringToByte(string txt)
{
int cnt = 0;
@@ -165,6 +178,5 @@ namespace S7.Net.Types
}
return null;
}
#endregion
}
}

View File

@@ -2,10 +2,14 @@
namespace S7.Net.Types
{
/// <summary>
/// Contains the conversion methods to convert Int from S7 plc to C#.
/// </summary>
public static class Int
{
// publics
#region FromByteArray
/// <summary>
/// Converts a S7 Int (2 bytes) to short (Int16)
/// </summary>
public static Int16 FromByteArray(byte[] bytes)
{
if (bytes.Length != 2)
@@ -16,15 +20,18 @@ namespace S7.Net.Types
// bytes[1] -> LowByte
return FromBytes(bytes[1], bytes[0]);
}
#endregion
#region FromBytes
/// <summary>
/// Converts a S7 Int (2 bytes) to short (Int16)
/// </summary>
public static Int16 FromBytes(byte LoVal, byte HiVal)
{
return (Int16)(HiVal * 256 + LoVal);
}
#endregion
#region ToByteArray
/// <summary>
/// Converts a short (Int16) to a S7 Int byte array (2 bytes)
/// </summary>
public static byte[] ToByteArray(Int16 value)
{
byte[] bytes = new byte[2];
@@ -41,6 +48,9 @@ namespace S7.Net.Types
return bytes;
}
/// <summary>
/// Converts an array of short (Int16) to a S7 Int byte array (2 bytes)
/// </summary>
public static byte[] ToByteArray(Int16[] value)
{
ByteArray arr = new ByteArray();
@@ -48,8 +58,10 @@ namespace S7.Net.Types
arr.Add(ToByteArray(val));
return arr.array;
}
#endregion
#region ToArray
/// <summary>
/// Converts an array of S7 Int to an array of short (Int16)
/// </summary>
public static Int16[] ToArray(byte[] bytes)
{
Int16[] values = new Int16[bytes.Length / 2];
@@ -60,9 +72,12 @@ namespace S7.Net.Types
return values;
}
#endregion
// conversion
/// <summary>
/// Converts a C# int value to a C# short value, to be used as word.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static Int16 CWord(int value)
{
if (value > 32767)

View File

@@ -1,9 +1,13 @@
namespace S7.Net.Types
{
/// <summary>
/// Contains the methods to convert from S7 strings to C# strings
/// </summary>
public static class String
{
// publics
#region ToByteArray
/// <summary>
/// Converts a string to S7 bytes
/// </summary>
public static byte[] ToByteArray(string value)
{
string txt = (string)value;
@@ -13,16 +17,17 @@
bytes[cnt] = (byte)Asc(ca[cnt].ToString());
return bytes;
}
#endregion
#region FromByteArray
/// <summary>
/// Converts S7 bytes to a string
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string FromByteArray(byte[] bytes)
{
return System.Text.Encoding.ASCII.GetString(bytes);
}
#endregion
// privates
private static int Asc(string s)
{
byte[] b = System.Text.Encoding.ASCII.GetBytes(s);

View File

@@ -5,6 +5,9 @@ using System.Reflection;
namespace S7.Net.Types
{
/// <summary>
/// Contains the method to convert a C# struct to S7 data types
/// </summary>
public static class Struct
{
/// <summary>

View File

@@ -2,10 +2,14 @@
namespace S7.Net.Types
{
/// <summary>
/// Converts the Timer data type to C# data type
/// </summary>
public static class Timer
{
// publics
#region FromByteArray
/// <summary>
/// Converts the timer bytes to a double
/// </summary>
public static double FromByteArray(byte[] bytes)
{
double wert = 0;
@@ -31,9 +35,10 @@ namespace S7.Net.Types
}
return wert;
}
#endregion
#region ToByteArray
/// <summary>
/// Converts a ushort (UInt16) to an array of bytes formatted as time
/// </summary>
public static byte[] ToByteArray(UInt16 value)
{
byte[] bytes = new byte[2];
@@ -50,6 +55,9 @@ namespace S7.Net.Types
return bytes;
}
/// <summary>
/// Converts an array of ushorts (Uint16) to an array of bytes formatted as time
/// </summary>
public static byte[] ToByteArray(UInt16[] value)
{
ByteArray arr = new ByteArray();
@@ -57,8 +65,12 @@ namespace S7.Net.Types
arr.Add(ToByteArray(val));
return arr.array;
}
#endregion
#region ToArray
/// <summary>
/// Converts an array of bytes formatted as time to an array of doubles
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static double[] ToArray(byte[] bytes)
{
double[] values = new double[bytes.Length / 2];
@@ -69,6 +81,5 @@ namespace S7.Net.Types
return values;
}
#endregion
}
}

View File

@@ -2,10 +2,14 @@
namespace S7.Net.Types
{
/// <summary>
/// Contains the conversion methods to convert Words from S7 plc to C#.
/// </summary>
public static class Word
{
// publics
#region FromByteArray
/// <summary>
/// Converts a word (2 bytes) to ushort (UInt16)
/// </summary>
public static UInt16 FromByteArray(byte[] bytes)
{
if (bytes.Length != 2)
@@ -16,31 +20,37 @@ namespace S7.Net.Types
// bytes[1] -> LowByte
return FromBytes(bytes[1], bytes[0]);
}
#endregion
#region FromBytes
/// <summary>
/// Converts a word (2 bytes) to ushort (UInt16)
/// </summary>
public static UInt16 FromBytes(byte LoVal, byte HiVal)
{
return (UInt16)(HiVal * 256 + LoVal);
return (UInt16) (HiVal*256 + LoVal);
}
#endregion
#region ToByteArray
/// <summary>
/// Converts a ushort (UInt16) to word (2 bytes)
/// </summary>
public static byte[] ToByteArray(UInt16 value)
{
byte[] bytes = new byte[2];
int x = 2;
long valLong = (long)((UInt16)value);
long valLong = (long) ((UInt16) value);
for (int cnt = 0; cnt < x; cnt++)
{
Int64 x1 = (Int64)Math.Pow(256, (cnt));
Int64 x1 = (Int64) Math.Pow(256, (cnt));
Int64 x3 = (Int64)(valLong / x1);
bytes[x - cnt - 1] = (byte)(x3 & 255);
valLong -= bytes[x - cnt - 1] * x1;
Int64 x3 = (Int64) (valLong/x1);
bytes[x - cnt - 1] = (byte) (x3 & 255);
valLong -= bytes[x - cnt - 1]*x1;
}
return bytes;
}
/// <summary>
/// Converts an array of ushort (UInt16) to an array of bytes
/// </summary>
public static byte[] ToByteArray(UInt16[] value)
{
ByteArray arr = new ByteArray();
@@ -48,18 +58,19 @@ namespace S7.Net.Types
arr.Add(ToByteArray(val));
return arr.array;
}
#endregion
#region ToArray
/// <summary>
/// Converts an array of bytes to an array of ushort
/// </summary>
public static UInt16[] ToArray(byte[] bytes)
{
UInt16[] values = new UInt16[bytes.Length / 2];
UInt16[] values = new UInt16[bytes.Length/2];
int counter = 0;
for (int cnt = 0; cnt < bytes.Length / 2; cnt++)
values[cnt] = FromByteArray(new byte[] { bytes[counter++], bytes[counter++] });
for (int cnt = 0; cnt < bytes.Length/2; cnt++)
values[cnt] = FromByteArray(new byte[] {bytes[counter++], bytes[counter++]});
return values;
}
#endregion
}
}