Skip to content

Commit

Permalink
Add Optimaisations
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman committed Jan 30, 2024
1 parent 370bd8c commit 403743f
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 307 deletions.
46 changes: 8 additions & 38 deletions src/S7PlcRx/PlcTypes/DInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,54 +55,24 @@ public static int CDWord(long value)
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <returns>int array.</returns>
public static int[] ToArray(byte[] bytes)
{
var values = new int[bytes.Length / 4];

var counter = 0;
for (var cnt = 0; cnt < bytes.Length / 4; cnt++)
{
values[cnt] = FromByteArray([bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++]]);
}

return values;
}
public static int[] ToArray(byte[] bytes) => TypeConverter.ToArray(bytes, FromByteArray);

/// <summary>
/// To the byte array.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A byte array.</returns>
public static byte[] ToByteArray(int value)
{
var bytes = new byte[4];
const int x = 4;
long valLong = value;
for (var cnt = 0; cnt < x; cnt++)
{
var x1 = (long)Math.Pow(256, cnt);

var x3 = valLong / x1;
bytes[x - cnt - 1] = (byte)(x3 & 255);
valLong -= bytes[x - cnt - 1] * x1;
}

return bytes;
}
public static byte[] ToByteArray(int value) => [
(byte)((value >> 24) & 255),
(byte)((value >> 16) & 255),
(byte)((value >> 8) & 255),
(byte)(value & 255),
];

/// <summary>
/// To the byte array.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A byte array.</returns>
public static byte[] ToByteArray(int[] value)
{
var arr = new ByteArray();
foreach (var val in value)
{
arr.Add(ToByteArray(val));
}

return arr.Array;
}
public static byte[] ToByteArray(int[] value) => TypeConverter.ToByteArray(value, ToByteArray);
}
40 changes: 8 additions & 32 deletions src/S7PlcRx/PlcTypes/DWord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,48 +40,24 @@ public static uint FromBytes(byte v1, byte v2, byte v3, byte v4) =>
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <returns>A uint array.</returns>
public static uint[] ToArray(byte[] bytes)
{
var values = new uint[bytes.Length / 4];

var counter = 0;
for (var cnt = 0; cnt < bytes.Length / 4; cnt++)
{
values[cnt] = FromByteArray([bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++]]);
}

return values;
}
public static uint[] ToArray(byte[] bytes) => TypeConverter.ToArray(bytes, FromByteArray);

/// <summary>
/// To the byte array.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A byte array.</returns>
public static byte[] ToByteArray(uint value)
{
var bytes = new byte[4];
bytes[0] = (byte)((value >> 24) & 0xFF);
bytes[1] = (byte)((value >> 16) & 0xFF);
bytes[2] = (byte)((value >> 8) & 0xFF);
bytes[3] = (byte)(value & 0xFF);

return bytes;
}
public static byte[] ToByteArray(uint value) => [
(byte)((value >> 24) & 0xFF),
(byte)((value >> 16) & 0xFF),
(byte)((value >> 8) & 0xFF),
(byte)(value & 0xFF),
];

/// <summary>
/// To the byte array.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A byte array.</returns>
public static byte[] ToByteArray(uint[] value)
{
var arr = new ByteArray();
foreach (var val in value)
{
arr.Add(ToByteArray(val));
}

return arr.Array;
}
public static byte[] ToByteArray(uint[] value) => TypeConverter.ToByteArray(value, ToByteArray);
}
41 changes: 3 additions & 38 deletions src/S7PlcRx/PlcTypes/Int.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,54 +53,19 @@ public static short CWord(int value)
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <returns>A short.</returns>
public static short[] ToArray(byte[] bytes)
{
var values = new short[bytes.Length / 2];

var counter = 0;
for (var cnt = 0; cnt < bytes.Length / 2; cnt++)
{
values[cnt] = FromByteArray([bytes[counter++], bytes[counter++]]);
}

return values;
}
public static short[] ToArray(byte[] bytes) => TypeConverter.ToArray(bytes, FromByteArray);

/// <summary>
/// To the byte array.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A byte array.</returns>
public static byte[] ToByteArray(short value)
{
var bytes = new byte[2];
const int x = 2;
long valLong = value;
for (var cnt = 0; cnt < x; cnt++)
{
var x1 = (long)Math.Pow(256, cnt);

var x3 = valLong / x1;
bytes[x - cnt - 1] = (byte)(x3 & 255);
valLong -= bytes[x - cnt - 1] * x1;
}

return bytes;
}
public static byte[] ToByteArray(short value) => [(byte)((value >> 8) & 255), (byte)(value & 255)];

/// <summary>
/// To the byte array.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A byte array.</returns>
public static byte[] ToByteArray(short[] value)
{
var arr = new ByteArray();
foreach (var val in value)
{
arr.Add(ToByteArray(val));
}

return arr.Array;
}
public static byte[] ToByteArray(short[] value) => TypeConverter.ToByteArray(value, ToByteArray);
}
161 changes: 15 additions & 146 deletions src/S7PlcRx/PlcTypes/LReal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,87 +26,42 @@ internal static class LReal
/// <exception cref="System.ArgumentNullException">bytes.</exception>
public static double FromByteArray(byte[] bytes, int start)
{
if (bytes == null)
if (bytes.Length != 8)
{
throw new ArgumentNullException(nameof(bytes));
throw new ArgumentException("Wrong number of bytes. Bytes array must contain 8 bytes.");
}

var v1 = bytes[start];
var v2 = bytes[start + 1];
var v3 = bytes[start + 2];
var v4 = bytes[start + 3];
var buffer = bytes;

if (v1 + v2 + v3 + v4 == 0)
// sps uses bigending so we have to reverse if platform needs
if (BitConverter.IsLittleEndian)
{
return 0d;
Array.Reverse(buffer);
}

// form the string
var txt = ValToBinString(v1) + ValToBinString(v2) + ValToBinString(v3) + ValToBinString(v4);

// first sign
var vz = int.Parse(txt.Substring(0, 1));
var exd = txt.Substring(1, 8).BinStringToInt32();
var ma = txt.Substring(9, 23);
var mantisse = 1d;
var faktor = 1d;

// All which is the number of the
for (var cnt = 0; cnt <= 22; cnt++)
{
faktor /= 2.0;

// corresponds to 2^-y
if (ma.Substring(cnt, 1) == "1")
{
mantisse += faktor;
}
}

return Math.Pow(-1, vz) * Math.Pow(2, exd - 127) * mantisse;
return BitConverter.ToDouble(buffer, start);
}

/// <summary>
/// Froms the d word.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A double.</returns>
public static double FromDWord(int value)
{
var b = DInt.ToByteArray(value);
return (double)FromByteArray(b);
}
public static double FromDWord(int value) => FromByteArray(DInt.ToByteArray(value));

/// <summary>
/// Froms the d word.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A double.</returns>
public static double FromDWord(uint value)
{
var b = DWord.ToByteArray(value);
var d = FromByteArray(b);
return d;
}
public static double FromDWord(uint value) => FromByteArray(DWord.ToByteArray(value));

/// <summary>
/// To the array.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <returns>A double.</returns>
public static double[] ToArray(byte[] bytes)
{
var length = bytes?.Length;
var values = new double[length!.Value / 4];

var counter = 0;
for (var cnt = 0; cnt < bytes!.Length / 4; cnt++)
{
values[cnt] = FromByteArray([bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++]]);
}

return values;
}
public static double[] ToArray(byte[] bytes) => TypeConverter.ToArray(bytes, FromByteArray);

/// <summary>
/// To the byte array.
Expand All @@ -115,48 +70,12 @@ public static double[] ToArray(byte[] bytes)
/// <returns>A byte.</returns>
public static byte[] ToByteArray(double value)
{
var bytes = new byte[4];
if (value != 0f)
{
string binString;
if (value < 0)
{
value *= -1;
binString = "1";
}
else
{
binString = "0";
}

var exponent = (int)Math.Floor((double)Math.Log(value) / Math.Log(2.0));
value = (value / Math.Pow(2, exponent)) - 1;
var bytes = BitConverter.GetBytes(value);

binString += ValToBinString((byte)(exponent + 127));
for (var cnt = 1; cnt <= 23; cnt++)
{
if (!(value - Math.Pow(2, -cnt) < 0))
{
value -= Math.Pow(2, -cnt);
binString += "1";
}
else
{
binString += "0";
}
}

bytes[0] = (byte)BinStringToByte(binString.Substring(0, 8))!;
bytes[1] = (byte)BinStringToByte(binString.Substring(8, 8))!;
bytes[2] = (byte)BinStringToByte(binString.Substring(16, 8))!;
bytes[3] = (byte)BinStringToByte(binString.Substring(24, 8))!;
}
else
// sps uses big endian so we have to check if platform is same
if (BitConverter.IsLittleEndian)
{
bytes[0] = 0;
bytes[1] = 0;
bytes[2] = 0;
bytes[3] = 0;
Array.Reverse(bytes);
}

return bytes;
Expand All @@ -167,55 +86,5 @@ public static byte[] ToByteArray(double value)
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A byte.</returns>
public static byte[] ToByteArray(double[] value)
{
var arr = new ByteArray();
for (var i = 0; i < value?.Length; i++)
{
var val = value[i];
arr.Add(ToByteArray(val));
}

return arr.Array;
}

private static byte? BinStringToByte(string txt)
{
var ret = 0;

if (txt.Length == 8)
{
int cnt;
for (cnt = 7; cnt >= 0; cnt += -1)
{
if (int.Parse(txt.Substring(cnt, 1)) == 1)
{
ret += (int)Math.Pow(2, txt.Length - 1 - cnt);
}
}

return (byte)ret;
}

return null;
}

private static string ValToBinString(byte value)
{
var txt = string.Empty;

for (var cnt = 7; cnt >= 0; cnt += -1)
{
if ((value & (byte)Math.Pow(2, cnt)) > 0)
{
txt += "1";
}
else
{
txt += "0";
}
}

return txt;
}
public static byte[] ToByteArray(double[] value) => TypeConverter.ToByteArray(value, ToByteArray);
}
Loading

0 comments on commit 403743f

Please sign in to comment.