mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2026-02-17 14:28:25 +08:00
- Adds true support for 64bit double / LReal datatype. - Set old Types.Single and Types.Double to obselete. Both class names use .NET types instead of S7 type names, contrary to all other types. - Remove already obsoleted conversion from DWord to Real. Why is this even necessary? For users caring about converting from DWord, they can still convert to single. But unless we get LWord support, there won't be a direct conversion to double/LReal. - Adjust unit tests by removing rounding, testing directly double read/writes. There is quite a bit of breaking changes at least in the automated Struct and object functions which automatically translate .NET types to appropriate S7 types. My consideration was that if we ever want to support 64bit types, there is no way against breaking those existing incorrect conversions from 64bit .NET double to 32 bit S7 Real variables.
69 lines
2.0 KiB
C#
69 lines
2.0 KiB
C#
using System;
|
|
|
|
namespace S7.Net.Types
|
|
{
|
|
/// <summary>
|
|
/// Contains the conversion methods to convert Real from S7 plc to C# float.
|
|
/// </summary>
|
|
[Obsolete("Class Single is obsolete. Use Real instead.")]
|
|
public static class Single
|
|
{
|
|
/// <summary>
|
|
/// Converts a S7 Real (4 bytes) to float
|
|
/// </summary>
|
|
public static float FromByteArray(byte[] bytes) => Real.FromByteArray(bytes);
|
|
|
|
/// <summary>
|
|
/// Converts a S7 DInt to float
|
|
/// </summary>
|
|
public static float FromDWord(Int32 value)
|
|
{
|
|
byte[] b = DInt.ToByteArray(value);
|
|
float d = FromByteArray(b);
|
|
return d;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a S7 DWord to float
|
|
/// </summary>
|
|
public static float FromDWord(UInt32 value)
|
|
{
|
|
byte[] b = DWord.ToByteArray(value);
|
|
float d = FromByteArray(b);
|
|
return d;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Converts a double to S7 Real (4 bytes)
|
|
/// </summary>
|
|
public static byte[] ToByteArray(float value) => Real.ToByteArray(value);
|
|
|
|
/// <summary>
|
|
/// Converts an array of float to an array of bytes
|
|
/// </summary>
|
|
public static byte[] ToByteArray(float[] value)
|
|
{
|
|
ByteArray arr = new ByteArray();
|
|
foreach (float val in value)
|
|
arr.Add(ToByteArray(val));
|
|
return arr.Array;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts an array of S7 Real to an array of float
|
|
/// </summary>
|
|
public static float[] ToArray(byte[] bytes)
|
|
{
|
|
float[] values = new float[bytes.Length / 4];
|
|
|
|
int counter = 0;
|
|
for (int cnt = 0; cnt < bytes.Length / 4; cnt++)
|
|
values[cnt] = FromByteArray(new byte[] { bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++] });
|
|
|
|
return values;
|
|
}
|
|
|
|
}
|
|
}
|