mirror of
https://github.com/fbarresi/Sharp7.git
synced 2026-02-04 08:42:51 +00:00
style and security enhancement from codacy
This commit is contained in:
@@ -11,8 +11,7 @@ namespace Sharp7
|
|||||||
private int _ReadTimeout = 2000;
|
private int _ReadTimeout = 2000;
|
||||||
private int _WriteTimeout = 2000;
|
private int _WriteTimeout = 2000;
|
||||||
private int _ConnectTimeout = 1000;
|
private int _ConnectTimeout = 1000;
|
||||||
public int LastError = 0;
|
private int LastError;
|
||||||
|
|
||||||
public MsgSocket()
|
public MsgSocket()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -57,7 +56,8 @@ namespace Sharp7
|
|||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
LastError = S7Consts.errTCPConnectionFailed;
|
LastError = S7Consts.errTCPConnectionFailed;
|
||||||
};
|
}
|
||||||
|
|
||||||
PingSocket.Close();
|
PingSocket.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,7 +150,7 @@ namespace Sharp7
|
|||||||
LastError = 0;
|
LastError = 0;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
int BytesSent = TCPSocket.Send(Buffer, Size, SocketFlags.None);
|
TCPSocket.Send(Buffer, Size, SocketFlags.None);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
|||||||
145
Sharp7/S7.cs
145
Sharp7/S7.cs
@@ -8,7 +8,8 @@ namespace Sharp7
|
|||||||
{
|
{
|
||||||
#region [Help Functions]
|
#region [Help Functions]
|
||||||
|
|
||||||
private static Int64 bias = 621355968000000000; // "decimicros" between 0001-01-01 00:00:00 and 1970-01-01 00:00:00
|
private static Int64
|
||||||
|
bias = 621355968000000000; // "decimicros" between 0001-01-01 00:00:00 and 1970-01-01 00:00:00
|
||||||
|
|
||||||
private static int BCDtoByte(byte B)
|
private static int BCDtoByte(byte B)
|
||||||
{
|
{
|
||||||
@@ -39,6 +40,7 @@ namespace Sharp7
|
|||||||
}
|
}
|
||||||
|
|
||||||
#region Get/Set the bit at Pos.Bit
|
#region Get/Set the bit at Pos.Bit
|
||||||
|
|
||||||
public static bool GetBitAt(this byte[] buffer, int pos, int bit)
|
public static bool GetBitAt(this byte[] buffer, int pos, int bit)
|
||||||
{
|
{
|
||||||
byte[] Mask = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
|
byte[] Mask = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
|
||||||
@@ -46,6 +48,7 @@ namespace Sharp7
|
|||||||
if (bit > 7) bit = 7;
|
if (bit > 7) bit = 7;
|
||||||
return (buffer[pos] & Mask[bit]) != 0;
|
return (buffer[pos] & Mask[bit]) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetBitAt(ref byte[] buffer, int pos, int bit, bool value)
|
public static void SetBitAt(ref byte[] buffer, int pos, int bit, bool value)
|
||||||
{
|
{
|
||||||
byte[] Mask = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
|
byte[] Mask = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
|
||||||
@@ -57,47 +60,59 @@ namespace Sharp7
|
|||||||
else
|
else
|
||||||
buffer[pos] = (byte) (buffer[pos] & ~Mask[bit]);
|
buffer[pos] = (byte) (buffer[pos] & ~Mask[bit]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 8 bit signed value (S7 SInt) -128..127
|
#region Get/Set 8 bit signed value (S7 SInt) -128..127
|
||||||
|
|
||||||
public static int GetSIntAt(this byte[] buffer, int pos)
|
public static int GetSIntAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
int value = buffer[pos];
|
int value = buffer[pos];
|
||||||
if (value < 128)
|
if (value < 128)
|
||||||
return value;
|
return value;
|
||||||
else
|
|
||||||
return (int)(value - 256);
|
return (value - 256);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetSIntAt(this byte[] buffer, int pos, int value)
|
public static void SetSIntAt(this byte[] buffer, int pos, int value)
|
||||||
{
|
{
|
||||||
if (value < -128) value = -128;
|
if (value < -128) value = -128;
|
||||||
if (value > 127) value = 127;
|
if (value > 127) value = 127;
|
||||||
buffer[pos] = (byte) value;
|
buffer[pos] = (byte) value;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 16 bit signed value (S7 int) -32768..32767
|
#region Get/Set 16 bit signed value (S7 int) -32768..32767
|
||||||
|
|
||||||
public static int GetIntAt(this byte[] buffer, int pos)
|
public static int GetIntAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
return (short) ((buffer[pos] << 8) | buffer[pos + 1]);
|
return (short) ((buffer[pos] << 8) | buffer[pos + 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetIntAt(this byte[] buffer, int pos, Int16 value)
|
public static void SetIntAt(this byte[] buffer, int pos, Int16 value)
|
||||||
{
|
{
|
||||||
buffer[pos] = (byte) (value >> 8);
|
buffer[pos] = (byte) (value >> 8);
|
||||||
buffer[pos + 1] = (byte) (value & 0x00FF);
|
buffer[pos + 1] = (byte) (value & 0x00FF);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 32 bit signed value (S7 DInt) -2147483648..2147483647
|
#region Get/Set 32 bit signed value (S7 DInt) -2147483648..2147483647
|
||||||
|
|
||||||
public static int GetDIntAt(this byte[] buffer, int pos)
|
public static int GetDIntAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
result = buffer[pos]; result <<= 8;
|
result = buffer[pos];
|
||||||
result += buffer[pos + 1]; result <<= 8;
|
result <<= 8;
|
||||||
result += buffer[pos + 2]; result <<= 8;
|
result += buffer[pos + 1];
|
||||||
|
result <<= 8;
|
||||||
|
result += buffer[pos + 2];
|
||||||
|
result <<= 8;
|
||||||
result += buffer[pos + 3];
|
result += buffer[pos + 3];
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetDIntAt(this byte[] buffer, int pos, int value)
|
public static void SetDIntAt(this byte[] buffer, int pos, int value)
|
||||||
{
|
{
|
||||||
buffer[pos + 3] = (byte) (value & 0xFF);
|
buffer[pos + 3] = (byte) (value & 0xFF);
|
||||||
@@ -105,22 +120,32 @@ namespace Sharp7
|
|||||||
buffer[pos + 1] = (byte) ((value >> 16) & 0xFF);
|
buffer[pos + 1] = (byte) ((value >> 16) & 0xFF);
|
||||||
buffer[pos] = (byte) ((value >> 24) & 0xFF);
|
buffer[pos] = (byte) ((value >> 24) & 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 64 bit signed value (S7 LInt) -9223372036854775808..9223372036854775807
|
#region Get/Set 64 bit signed value (S7 LInt) -9223372036854775808..9223372036854775807
|
||||||
|
|
||||||
public static Int64 GetLIntAt(this byte[] buffer, int pos)
|
public static Int64 GetLIntAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
Int64 result;
|
Int64 result;
|
||||||
result = buffer[pos]; result <<= 8;
|
result = buffer[pos];
|
||||||
result += buffer[pos + 1]; result <<= 8;
|
result <<= 8;
|
||||||
result += buffer[pos + 2]; result <<= 8;
|
result += buffer[pos + 1];
|
||||||
result += buffer[pos + 3]; result <<= 8;
|
result <<= 8;
|
||||||
result += buffer[pos + 4]; result <<= 8;
|
result += buffer[pos + 2];
|
||||||
result += buffer[pos + 5]; result <<= 8;
|
result <<= 8;
|
||||||
result += buffer[pos + 6]; result <<= 8;
|
result += buffer[pos + 3];
|
||||||
|
result <<= 8;
|
||||||
|
result += buffer[pos + 4];
|
||||||
|
result <<= 8;
|
||||||
|
result += buffer[pos + 5];
|
||||||
|
result <<= 8;
|
||||||
|
result += buffer[pos + 6];
|
||||||
|
result <<= 8;
|
||||||
result += buffer[pos + 7];
|
result += buffer[pos + 7];
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetLIntAt(this byte[] buffer, int pos, Int64 value)
|
public static void SetLIntAt(this byte[] buffer, int pos, Int64 value)
|
||||||
{
|
{
|
||||||
buffer[pos + 7] = (byte) (value & 0xFF);
|
buffer[pos + 7] = (byte) (value & 0xFF);
|
||||||
@@ -132,41 +157,53 @@ namespace Sharp7
|
|||||||
buffer[pos + 1] = (byte) ((value >> 48) & 0xFF);
|
buffer[pos + 1] = (byte) ((value >> 48) & 0xFF);
|
||||||
buffer[pos] = (byte) ((value >> 56) & 0xFF);
|
buffer[pos] = (byte) ((value >> 56) & 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 8 bit unsigned value (S7 USInt) 0..255
|
#region Get/Set 8 bit unsigned value (S7 USInt) 0..255
|
||||||
|
|
||||||
public static byte GetUSIntAt(this byte[] buffer, int pos)
|
public static byte GetUSIntAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
return buffer[pos];
|
return buffer[pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetUSIntAt(this byte[] buffer, int pos, byte value)
|
public static void SetUSIntAt(this byte[] buffer, int pos, byte value)
|
||||||
{
|
{
|
||||||
buffer[pos] = value;
|
buffer[pos] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 16 bit unsigned value (S7 UInt) 0..65535
|
#region Get/Set 16 bit unsigned value (S7 UInt) 0..65535
|
||||||
|
|
||||||
public static UInt16 GetUIntAt(this byte[] buffer, int pos)
|
public static UInt16 GetUIntAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
return (UInt16) ((buffer[pos] << 8) | buffer[pos + 1]);
|
return (UInt16) ((buffer[pos] << 8) | buffer[pos + 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetUIntAt(this byte[] buffer, int pos, UInt16 value)
|
public static void SetUIntAt(this byte[] buffer, int pos, UInt16 value)
|
||||||
{
|
{
|
||||||
buffer[pos] = (byte) (value >> 8);
|
buffer[pos] = (byte) (value >> 8);
|
||||||
buffer[pos + 1] = (byte) (value & 0x00FF);
|
buffer[pos + 1] = (byte) (value & 0x00FF);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 32 bit unsigned value (S7 UDInt) 0..4294967296
|
#region Get/Set 32 bit unsigned value (S7 UDInt) 0..4294967296
|
||||||
|
|
||||||
public static UInt32 GetUDIntAt(this byte[] buffer, int pos)
|
public static UInt32 GetUDIntAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
UInt32 result;
|
UInt32 result;
|
||||||
result = buffer[pos]; result <<= 8;
|
result = buffer[pos];
|
||||||
result |= buffer[pos + 1]; result <<= 8;
|
result <<= 8;
|
||||||
result |= buffer[pos + 2]; result <<= 8;
|
result |= buffer[pos + 1];
|
||||||
|
result <<= 8;
|
||||||
|
result |= buffer[pos + 2];
|
||||||
|
result <<= 8;
|
||||||
result |= buffer[pos + 3];
|
result |= buffer[pos + 3];
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetUDIntAt(this byte[] buffer, int pos, UInt32 value)
|
public static void SetUDIntAt(this byte[] buffer, int pos, UInt32 value)
|
||||||
{
|
{
|
||||||
buffer[pos + 3] = (byte) (value & 0xFF);
|
buffer[pos + 3] = (byte) (value & 0xFF);
|
||||||
@@ -174,22 +211,32 @@ namespace Sharp7
|
|||||||
buffer[pos + 1] = (byte) ((value >> 16) & 0xFF);
|
buffer[pos + 1] = (byte) ((value >> 16) & 0xFF);
|
||||||
buffer[pos] = (byte) ((value >> 24) & 0xFF);
|
buffer[pos] = (byte) ((value >> 24) & 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 64 bit unsigned value (S7 ULint) 0..18446744073709551616
|
#region Get/Set 64 bit unsigned value (S7 ULint) 0..18446744073709551616
|
||||||
|
|
||||||
public static UInt64 GetULIntAt(this byte[] buffer, int pos)
|
public static UInt64 GetULIntAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
UInt64 result;
|
UInt64 result;
|
||||||
result = buffer[pos]; result <<= 8;
|
result = buffer[pos];
|
||||||
result |= buffer[pos + 1]; result <<= 8;
|
result <<= 8;
|
||||||
result |= buffer[pos + 2]; result <<= 8;
|
result |= buffer[pos + 1];
|
||||||
result |= buffer[pos + 3]; result <<= 8;
|
result <<= 8;
|
||||||
result |= buffer[pos + 4]; result <<= 8;
|
result |= buffer[pos + 2];
|
||||||
result |= buffer[pos + 5]; result <<= 8;
|
result <<= 8;
|
||||||
result |= buffer[pos + 6]; result <<= 8;
|
result |= buffer[pos + 3];
|
||||||
|
result <<= 8;
|
||||||
|
result |= buffer[pos + 4];
|
||||||
|
result <<= 8;
|
||||||
|
result |= buffer[pos + 5];
|
||||||
|
result <<= 8;
|
||||||
|
result |= buffer[pos + 6];
|
||||||
|
result <<= 8;
|
||||||
result |= buffer[pos + 7];
|
result |= buffer[pos + 7];
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetULintAt(this byte[] buffer, int pos, UInt64 value)
|
public static void SetULintAt(this byte[] buffer, int pos, UInt64 value)
|
||||||
{
|
{
|
||||||
buffer[pos + 7] = (byte) (value & 0xFF);
|
buffer[pos + 7] = (byte) (value & 0xFF);
|
||||||
@@ -201,59 +248,74 @@ namespace Sharp7
|
|||||||
buffer[pos + 1] = (byte) ((value >> 48) & 0xFF);
|
buffer[pos + 1] = (byte) ((value >> 48) & 0xFF);
|
||||||
buffer[pos] = (byte) ((value >> 56) & 0xFF);
|
buffer[pos] = (byte) ((value >> 56) & 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 8 bit word (S7 Byte) 16#00..16#FF
|
#region Get/Set 8 bit word (S7 Byte) 16#00..16#FF
|
||||||
|
|
||||||
public static byte GetByteAt(this byte[] buffer, int pos)
|
public static byte GetByteAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
return buffer[pos];
|
return buffer[pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetByteAt(this byte[] buffer, int pos, byte value)
|
public static void SetByteAt(this byte[] buffer, int pos, byte value)
|
||||||
{
|
{
|
||||||
buffer[pos] = value;
|
buffer[pos] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 16 bit word (S7 Word) 16#0000..16#FFFF
|
#region Get/Set 16 bit word (S7 Word) 16#0000..16#FFFF
|
||||||
|
|
||||||
public static UInt16 GetWordAt(this byte[] buffer, int pos)
|
public static UInt16 GetWordAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
return GetUIntAt(buffer, pos);
|
return GetUIntAt(buffer, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetWordAt(this byte[] buffer, int pos, UInt16 value)
|
public static void SetWordAt(this byte[] buffer, int pos, UInt16 value)
|
||||||
{
|
{
|
||||||
SetUIntAt(buffer, pos, value);
|
SetUIntAt(buffer, pos, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 32 bit word (S7 DWord) 16#00000000..16#FFFFFFFF
|
#region Get/Set 32 bit word (S7 DWord) 16#00000000..16#FFFFFFFF
|
||||||
|
|
||||||
public static UInt32 GetDWordAt(this byte[] buffer, int pos)
|
public static UInt32 GetDWordAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
return GetUDIntAt(buffer, pos);
|
return GetUDIntAt(buffer, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetDWordAt(this byte[] buffer, int pos, UInt32 value)
|
public static void SetDWordAt(this byte[] buffer, int pos, UInt32 value)
|
||||||
{
|
{
|
||||||
SetUDIntAt(buffer, pos, value);
|
SetUDIntAt(buffer, pos, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 64 bit word (S7 LWord) 16#0000000000000000..16#FFFFFFFFFFFFFFFF
|
#region Get/Set 64 bit word (S7 LWord) 16#0000000000000000..16#FFFFFFFFFFFFFFFF
|
||||||
|
|
||||||
public static UInt64 GetLWordAt(this byte[] buffer, int pos)
|
public static UInt64 GetLWordAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
return GetULIntAt(buffer, pos);
|
return GetULIntAt(buffer, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetLWordAt(this byte[] buffer, int pos, UInt64 value)
|
public static void SetLWordAt(this byte[] buffer, int pos, UInt64 value)
|
||||||
{
|
{
|
||||||
SetULintAt(buffer, pos, value);
|
SetULintAt(buffer, pos, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 32 bit floating point number (S7 Real) (Range of Single)
|
#region Get/Set 32 bit floating point number (S7 Real) (Range of Single)
|
||||||
|
|
||||||
public static Single GetRealAt(this byte[] buffer, int pos)
|
public static Single GetRealAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
UInt32 value = GetUDIntAt(buffer, pos);
|
UInt32 value = GetUDIntAt(buffer, pos);
|
||||||
byte[] bytes = BitConverter.GetBytes(value);
|
byte[] bytes = BitConverter.GetBytes(value);
|
||||||
return BitConverter.ToSingle(bytes, 0);
|
return BitConverter.ToSingle(bytes, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetRealAt(this byte[] buffer, int pos, Single value)
|
public static void SetRealAt(this byte[] buffer, int pos, Single value)
|
||||||
{
|
{
|
||||||
byte[] FloatArray = BitConverter.GetBytes(value);
|
byte[] FloatArray = BitConverter.GetBytes(value);
|
||||||
@@ -262,15 +324,18 @@ namespace Sharp7
|
|||||||
buffer[pos + 2] = FloatArray[1];
|
buffer[pos + 2] = FloatArray[1];
|
||||||
buffer[pos + 3] = FloatArray[0];
|
buffer[pos + 3] = FloatArray[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set 64 bit floating point number (S7 LReal) (Range of Double)
|
#region Get/Set 64 bit floating point number (S7 LReal) (Range of Double)
|
||||||
|
|
||||||
public static Double GetLRealAt(this byte[] buffer, int pos)
|
public static Double GetLRealAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
UInt64 value = GetULIntAt(buffer, pos);
|
UInt64 value = GetULIntAt(buffer, pos);
|
||||||
byte[] bytes = BitConverter.GetBytes(value);
|
byte[] bytes = BitConverter.GetBytes(value);
|
||||||
return BitConverter.ToDouble(bytes, 0);
|
return BitConverter.ToDouble(bytes, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetLRealAt(this byte[] buffer, int pos, Double value)
|
public static void SetLRealAt(this byte[] buffer, int pos, Double value)
|
||||||
{
|
{
|
||||||
byte[] FloatArray = BitConverter.GetBytes(value);
|
byte[] FloatArray = BitConverter.GetBytes(value);
|
||||||
@@ -283,9 +348,11 @@ namespace Sharp7
|
|||||||
buffer[pos + 6] = FloatArray[1];
|
buffer[pos + 6] = FloatArray[1];
|
||||||
buffer[pos + 7] = FloatArray[0];
|
buffer[pos + 7] = FloatArray[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set DateTime (S7 DATE_AND_TIME)
|
#region Get/Set DateTime (S7 DATE_AND_TIME)
|
||||||
|
|
||||||
public static DateTime GetDateTimeAt(this byte[] buffer, int pos)
|
public static DateTime GetDateTimeAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
int Year, Month, Day, Hour, Min, Sec, MSec;
|
int Year, Month, Day, Hour, Min, Sec, MSec;
|
||||||
@@ -311,6 +378,7 @@ namespace Sharp7
|
|||||||
return new DateTime(0);
|
return new DateTime(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetDateTimeAt(this byte[] buffer, int pos, DateTime value)
|
public static void SetDateTimeAt(this byte[] buffer, int pos, DateTime value)
|
||||||
{
|
{
|
||||||
int Year = value.Year;
|
int Year = value.Year;
|
||||||
@@ -336,9 +404,11 @@ namespace Sharp7
|
|||||||
buffer[pos + 6] = ByteToBCD(MsecH);
|
buffer[pos + 6] = ByteToBCD(MsecH);
|
||||||
buffer[pos + 7] = ByteToBCD(MsecL * 10 + Dow);
|
buffer[pos + 7] = ByteToBCD(MsecL * 10 + Dow);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set DATE (S7 DATE)
|
#region Get/Set DATE (S7 DATE)
|
||||||
|
|
||||||
public static DateTime GetDateAt(this byte[] buffer, int pos)
|
public static DateTime GetDateAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -350,6 +420,7 @@ namespace Sharp7
|
|||||||
return new DateTime(0);
|
return new DateTime(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetDateAt(this byte[] buffer, int pos, DateTime value)
|
public static void SetDateAt(this byte[] buffer, int pos, DateTime value)
|
||||||
{
|
{
|
||||||
SetIntAt(buffer, pos, (Int16) (value - new DateTime(1990, 1, 1)).Days);
|
SetIntAt(buffer, pos, (Int16) (value - new DateTime(1990, 1, 1)).Days);
|
||||||
@@ -358,6 +429,7 @@ namespace Sharp7
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set TOD (S7 TIME_OF_DAY)
|
#region Get/Set TOD (S7 TIME_OF_DAY)
|
||||||
|
|
||||||
public static DateTime GetTODAt(this byte[] buffer, int pos)
|
public static DateTime GetTODAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -369,14 +441,17 @@ namespace Sharp7
|
|||||||
return new DateTime(0);
|
return new DateTime(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetTODAt(this byte[] buffer, int pos, DateTime value)
|
public static void SetTODAt(this byte[] buffer, int pos, DateTime value)
|
||||||
{
|
{
|
||||||
TimeSpan Time = value.TimeOfDay;
|
TimeSpan Time = value.TimeOfDay;
|
||||||
SetDIntAt(buffer, pos, (Int32) Math.Round(Time.TotalMilliseconds));
|
SetDIntAt(buffer, pos, (Int32) Math.Round(Time.TotalMilliseconds));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set LTOD (S7 1500 LONG TIME_OF_DAY)
|
#region Get/Set LTOD (S7 1500 LONG TIME_OF_DAY)
|
||||||
|
|
||||||
public static DateTime GetLTODAt(this byte[] buffer, int pos)
|
public static DateTime GetLTODAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
// .NET Tick = 100 ns, S71500 Tick = 1 ns
|
// .NET Tick = 100 ns, S71500 Tick = 1 ns
|
||||||
@@ -389,14 +464,17 @@ namespace Sharp7
|
|||||||
return new DateTime(0);
|
return new DateTime(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetLTODAt(this byte[] buffer, int pos, DateTime value)
|
public static void SetLTODAt(this byte[] buffer, int pos, DateTime value)
|
||||||
{
|
{
|
||||||
TimeSpan Time = value.TimeOfDay;
|
TimeSpan Time = value.TimeOfDay;
|
||||||
SetLIntAt(buffer, pos, (Int64) Time.Ticks * 100);
|
SetLIntAt(buffer, pos, (Int64) Time.Ticks * 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region GET/SET LDT (S7 1500 Long Date and Time)
|
#region GET/SET LDT (S7 1500 Long Date and Time)
|
||||||
|
|
||||||
public static DateTime GetLDTAt(this byte[] buffer, int pos)
|
public static DateTime GetLDTAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -408,13 +486,16 @@ namespace Sharp7
|
|||||||
return new DateTime(0);
|
return new DateTime(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetLDTAt(this byte[] buffer, int pos, DateTime value)
|
public static void SetLDTAt(this byte[] buffer, int pos, DateTime value)
|
||||||
{
|
{
|
||||||
SetLIntAt(buffer, pos, (value.Ticks - bias) * 100);
|
SetLIntAt(buffer, pos, (value.Ticks - bias) * 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set DTL (S71200/1500 Date and Time)
|
#region Get/Set DTL (S71200/1500 Date and Time)
|
||||||
|
|
||||||
// Thanks to Johan Cardoen for GetDTLAt
|
// Thanks to Johan Cardoen for GetDTLAt
|
||||||
public static DateTime GetDTLAt(this byte[] buffer, int pos)
|
public static DateTime GetDTLAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
@@ -437,6 +518,7 @@ namespace Sharp7
|
|||||||
return new DateTime(0);
|
return new DateTime(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetDTLAt(this byte[] buffer, int pos, DateTime value)
|
public static void SetDTLAt(this byte[] buffer, int pos, DateTime value)
|
||||||
{
|
{
|
||||||
short Year = (short) value.Year;
|
short Year = (short) value.Year;
|
||||||
@@ -465,12 +547,14 @@ namespace Sharp7
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set String (S7 String)
|
#region Get/Set String (S7 String)
|
||||||
|
|
||||||
// Thanks to Pablo Agirre
|
// Thanks to Pablo Agirre
|
||||||
public static string GetStringAt(this byte[] buffer, int pos)
|
public static string GetStringAt(this byte[] buffer, int pos)
|
||||||
{
|
{
|
||||||
int size = (int) buffer[pos + 1];
|
int size = (int) buffer[pos + 1];
|
||||||
return Encoding.UTF8.GetString(buffer, pos + 2, size);
|
return Encoding.UTF8.GetString(buffer, pos + 2, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetStringAt(this byte[] buffer, int pos, int MaxLen, string value)
|
public static void SetStringAt(this byte[] buffer, int pos, int MaxLen, string value)
|
||||||
{
|
{
|
||||||
int size = value.Length;
|
int size = value.Length;
|
||||||
@@ -478,13 +562,16 @@ namespace Sharp7
|
|||||||
buffer[pos + 1] = (byte) size;
|
buffer[pos + 1] = (byte) size;
|
||||||
Encoding.UTF8.GetBytes(value, 0, size, buffer, pos + 2);
|
Encoding.UTF8.GetBytes(value, 0, size, buffer, pos + 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set Array of char (S7 ARRAY OF CHARS)
|
#region Get/Set Array of char (S7 ARRAY OF CHARS)
|
||||||
|
|
||||||
public static string GetCharsAt(this byte[] buffer, int pos, int Size)
|
public static string GetCharsAt(this byte[] buffer, int pos, int Size)
|
||||||
{
|
{
|
||||||
return Encoding.UTF8.GetString(buffer, pos, Size);
|
return Encoding.UTF8.GetString(buffer, pos, Size);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetCharsAt(this byte[] buffer, int pos, string value)
|
public static void SetCharsAt(this byte[] buffer, int pos, string value)
|
||||||
{
|
{
|
||||||
int MaxLen = buffer.Length - pos;
|
int MaxLen = buffer.Length - pos;
|
||||||
@@ -492,9 +579,11 @@ namespace Sharp7
|
|||||||
if (MaxLen > value.Length) MaxLen = value.Length;
|
if (MaxLen > value.Length) MaxLen = value.Length;
|
||||||
Encoding.UTF8.GetBytes(value, 0, MaxLen, buffer, pos);
|
Encoding.UTF8.GetBytes(value, 0, MaxLen, buffer, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set Counter
|
#region Get/Set Counter
|
||||||
|
|
||||||
public static int GetCounter(this ushort value)
|
public static int GetCounter(this ushort value)
|
||||||
{
|
{
|
||||||
return BCDtoByte((byte) value) * 100 + BCDtoByte((byte) (value >> 8));
|
return BCDtoByte((byte) value) * 100 + BCDtoByte((byte) (value >> 8));
|
||||||
@@ -514,6 +603,7 @@ namespace Sharp7
|
|||||||
{
|
{
|
||||||
buffer[pos] = ToCounter(value);
|
buffer[pos] = ToCounter(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Get/Set Timer
|
#region Get/Set Timer
|
||||||
@@ -536,9 +626,12 @@ namespace Sharp7
|
|||||||
}
|
}
|
||||||
|
|
||||||
Int32 a;
|
Int32 a;
|
||||||
a = buffer[pos + 0]; a <<= 8;
|
a = buffer[pos + 0];
|
||||||
a += buffer[pos + 1]; a <<= 8;
|
a <<= 8;
|
||||||
a += buffer[pos + 2]; a <<= 8;
|
a += buffer[pos + 1];
|
||||||
|
a <<= 8;
|
||||||
|
a += buffer[pos + 2];
|
||||||
|
a <<= 8;
|
||||||
a += buffer[pos + 3];
|
a += buffer[pos + 3];
|
||||||
TimeSpan sp = new TimeSpan(0, 0, 0, 0, a);
|
TimeSpan sp = new TimeSpan(0, 0, 0, 0, a);
|
||||||
|
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ namespace Sharp7
|
|||||||
#region [MultiRead/Write Helper]
|
#region [MultiRead/Write Helper]
|
||||||
private S7Client FClient;
|
private S7Client FClient;
|
||||||
private GCHandle[] Handles = new GCHandle[S7Client.MaxVars];
|
private GCHandle[] Handles = new GCHandle[S7Client.MaxVars];
|
||||||
private int Count = 0;
|
private int Count;
|
||||||
private S7Client.S7DataItem[] Items = new S7Client.S7DataItem[S7Client.MaxVars];
|
private S7Client.S7DataItem[] Items = new S7Client.S7DataItem[S7Client.MaxVars];
|
||||||
|
|
||||||
|
|
||||||
public int[] Results = new int[S7Client.MaxVars];
|
public int[] Results { get; } = new int[S7Client.MaxVars];
|
||||||
|
|
||||||
private bool AdjustWordLength(int Area, ref int WordLen, ref int Amount, ref int Start)
|
private bool AdjustWordLength(int Area, ref int WordLen, ref int Amount, ref int Start)
|
||||||
{
|
{
|
||||||
@@ -45,7 +45,7 @@ namespace Sharp7
|
|||||||
{
|
{
|
||||||
FClient = Client;
|
FClient = Client;
|
||||||
for (int c = 0; c < S7Client.MaxVars; c++)
|
for (int c = 0; c < S7Client.MaxVars; c++)
|
||||||
Results[c] = (int)S7Consts.errCliItemNotAvailable;
|
Results[c] = S7Consts.errCliItemNotAvailable;
|
||||||
}
|
}
|
||||||
~S7MultiVar()
|
~S7MultiVar()
|
||||||
{
|
{
|
||||||
@@ -80,32 +80,27 @@ namespace Sharp7
|
|||||||
Items[Count].Start = Start;
|
Items[Count].Start = Start;
|
||||||
Items[Count].Amount = Amount;
|
Items[Count].Amount = Amount;
|
||||||
GCHandle handle = GCHandle.Alloc(Buffer, GCHandleType.Pinned);
|
GCHandle handle = GCHandle.Alloc(Buffer, GCHandleType.Pinned);
|
||||||
#if WINDOWS_UWP || NETFX_CORE
|
|
||||||
if (IntPtr.Size == 4)
|
|
||||||
Items[Count].pData = (IntPtr)(handle.AddrOfPinnedObject().ToInt32() + Offset * Marshal.SizeOf<T>());
|
|
||||||
else
|
|
||||||
Items[Count].pData = (IntPtr)(handle.AddrOfPinnedObject().ToInt64() + Offset * Marshal.SizeOf<T>());
|
|
||||||
#else
|
|
||||||
if (IntPtr.Size == 4)
|
if (IntPtr.Size == 4)
|
||||||
Items[Count].pData = (IntPtr)(handle.AddrOfPinnedObject().ToInt32() + Offset * Marshal.SizeOf(typeof(T)));
|
Items[Count].pData = (IntPtr)(handle.AddrOfPinnedObject().ToInt32() + Offset * Marshal.SizeOf(typeof(T)));
|
||||||
else
|
else
|
||||||
Items[Count].pData = (IntPtr)(handle.AddrOfPinnedObject().ToInt64() + Offset * Marshal.SizeOf(typeof(T)));
|
Items[Count].pData = (IntPtr)(handle.AddrOfPinnedObject().ToInt64() + Offset * Marshal.SizeOf(typeof(T)));
|
||||||
#endif
|
|
||||||
Handles[Count] = handle;
|
Handles[Count] = handle;
|
||||||
Count++;
|
Count++;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Read()
|
public int Read()
|
||||||
{
|
{
|
||||||
int FunctionResult;
|
int FunctionResult;
|
||||||
int GlobalResult = (int)S7Consts.errCliFunctionRefused;
|
int GlobalResult;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (Count > 0)
|
if (Count > 0)
|
||||||
@@ -117,7 +112,7 @@ namespace Sharp7
|
|||||||
GlobalResult = FunctionResult;
|
GlobalResult = FunctionResult;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
GlobalResult = (int)S7Consts.errCliFunctionRefused;
|
GlobalResult = S7Consts.errCliFunctionRefused;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -129,7 +124,7 @@ namespace Sharp7
|
|||||||
public int Write()
|
public int Write()
|
||||||
{
|
{
|
||||||
int FunctionResult;
|
int FunctionResult;
|
||||||
int GlobalResult = (int)S7Consts.errCliFunctionRefused;
|
int GlobalResult;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (Count > 0)
|
if (Count > 0)
|
||||||
@@ -141,7 +136,7 @@ namespace Sharp7
|
|||||||
GlobalResult = FunctionResult;
|
GlobalResult = FunctionResult;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
GlobalResult = (int)S7Consts.errCliFunctionRefused;
|
GlobalResult = S7Consts.errCliFunctionRefused;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,17 +9,13 @@ namespace Sharp7
|
|||||||
#region S7Timer
|
#region S7Timer
|
||||||
TimeSpan pt;
|
TimeSpan pt;
|
||||||
TimeSpan et;
|
TimeSpan et;
|
||||||
bool input = false;
|
bool input ;
|
||||||
bool q = false;
|
bool q;
|
||||||
public S7Timer(byte[] buff, int position)
|
public S7Timer(byte[] buff, int position)
|
||||||
{
|
{
|
||||||
if (position + 12 < buff.Length)
|
if (position + 12 < buff.Length)
|
||||||
{
|
{
|
||||||
return;
|
SetTimer(new List<byte>(buff).GetRange(position, 12).ToArray());
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SetTimer(new List<byte>(buff).GetRange(position, 16).ToArray());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user