mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2026-02-17 14:28:25 +08:00
Removed the Controls project and existing Test projects. Added new Test project and references to NUnit and Should projects for testing. Extracted IPlc interface to help test projects that reference this library. Also implemented the IDisposable interface on the Plc object so that references to mSocket would be cleaned up appropriately when the object is blown away.
64 lines
1.8 KiB
C#
64 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace S7.Types
|
|
{
|
|
public static class Counter
|
|
{
|
|
// publics
|
|
#region FromByteArray
|
|
public static UInt16 FromByteArray(byte[] bytes)
|
|
{
|
|
// bytes[0] -> HighByte
|
|
// bytes[1] -> LowByte
|
|
return FromBytes(bytes[1], bytes[0]);
|
|
}
|
|
#endregion
|
|
#region FromBytes
|
|
public static UInt16 FromBytes(byte LoVal, byte HiVal)
|
|
{
|
|
return (UInt16)(HiVal * 256 + LoVal);
|
|
}
|
|
#endregion
|
|
|
|
#region ToByteArray
|
|
public static byte[] ToByteArray(UInt16 value)
|
|
{
|
|
byte[] bytes = new byte[2];
|
|
int x = 2;
|
|
long valLong = (long)((UInt16)value);
|
|
for (int cnt = 0; cnt < x; 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;
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
public static byte[] ToByteArray(UInt16[] value)
|
|
{
|
|
ByteArray arr = new ByteArray();
|
|
foreach (UInt16 val in value)
|
|
arr.Add(ToByteArray(val));
|
|
return arr.array;
|
|
}
|
|
#endregion
|
|
#region ToArray
|
|
public static UInt16[] ToArray(byte[] bytes)
|
|
{
|
|
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++] });
|
|
|
|
return values;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|