Double.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. namespace S7.Net.Types
  3. {
  4. /// <summary>
  5. /// Contains the conversion methods to convert Real from S7 plc to C# double.
  6. /// </summary>
  7. [Obsolete("Class Double is obsolete. Use Real instead for 32bit floating point, or LReal for 64bit floating point.")]
  8. public static class Double
  9. {
  10. /// <summary>
  11. /// Converts a S7 Real (4 bytes) to double
  12. /// </summary>
  13. public static double FromByteArray(byte[] bytes) => Real.FromByteArray(bytes);
  14. /// <summary>
  15. /// Converts a S7 DInt to double
  16. /// </summary>
  17. public static double FromDWord(Int32 value)
  18. {
  19. byte[] b = DInt.ToByteArray(value);
  20. double d = FromByteArray(b);
  21. return d;
  22. }
  23. /// <summary>
  24. /// Converts a S7 DWord to double
  25. /// </summary>
  26. public static double FromDWord(UInt32 value)
  27. {
  28. byte[] b = DWord.ToByteArray(value);
  29. double d = FromByteArray(b);
  30. return d;
  31. }
  32. /// <summary>
  33. /// Converts a double to S7 Real (4 bytes)
  34. /// </summary>
  35. public static byte[] ToByteArray(double value) => Real.ToByteArray((float)value);
  36. /// <summary>
  37. /// Converts an array of double to an array of bytes
  38. /// </summary>
  39. public static byte[] ToByteArray(double[] value)
  40. {
  41. ByteArray arr = new ByteArray();
  42. foreach (double val in value)
  43. arr.Add(ToByteArray(val));
  44. return arr.Array;
  45. }
  46. /// <summary>
  47. /// Converts an array of S7 Real to an array of double
  48. /// </summary>
  49. public static double[] ToArray(byte[] bytes)
  50. {
  51. double[] values = new double[bytes.Length / 4];
  52. int counter = 0;
  53. for (int cnt = 0; cnt < bytes.Length / 4; cnt++)
  54. values[cnt] = FromByteArray(new byte[] { bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++] });
  55. return values;
  56. }
  57. }
  58. }