Single.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# float.
  6. /// </summary>
  7. [Obsolete("Class Single is obsolete. Use Real instead.")]
  8. public static class Single
  9. {
  10. /// <summary>
  11. /// Converts a S7 Real (4 bytes) to float
  12. /// </summary>
  13. public static float FromByteArray(byte[] bytes) => Real.FromByteArray(bytes);
  14. /// <summary>
  15. /// Converts a S7 DInt to float
  16. /// </summary>
  17. public static float FromDWord(Int32 value)
  18. {
  19. byte[] b = DInt.ToByteArray(value);
  20. float d = FromByteArray(b);
  21. return d;
  22. }
  23. /// <summary>
  24. /// Converts a S7 DWord to float
  25. /// </summary>
  26. public static float FromDWord(UInt32 value)
  27. {
  28. byte[] b = DWord.ToByteArray(value);
  29. float 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(float value) => Real.ToByteArray(value);
  36. /// <summary>
  37. /// Converts an array of float to an array of bytes
  38. /// </summary>
  39. public static byte[] ToByteArray(float[] value)
  40. {
  41. ByteArray arr = new ByteArray();
  42. foreach (float 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 float
  48. /// </summary>
  49. public static float[] ToArray(byte[] bytes)
  50. {
  51. float[] values = new float[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. }