TimeSpan.cs 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. namespace S7.Net.Types
  4. {
  5. /// <summary>
  6. /// Contains the methods to convert between <see cref="T:System.TimeSpan"/> and S7 representation of TIME values.
  7. /// </summary>
  8. public static class TimeSpan
  9. {
  10. /// <summary>
  11. /// The minimum <see cref="T:System.TimeSpan"/> value supported by the specification.
  12. /// </summary>
  13. public static readonly System.TimeSpan SpecMinimumTimeSpan = System.TimeSpan.FromMilliseconds(int.MinValue);
  14. /// <summary>
  15. /// The maximum <see cref="T:System.TimeSpan"/> value supported by the specification.
  16. /// </summary>
  17. public static readonly System.TimeSpan SpecMaximumTimeSpan = System.TimeSpan.FromMilliseconds(int.MaxValue);
  18. /// <summary>
  19. /// Parses a <see cref="T:System.TimeSpan"/> value from bytes.
  20. /// </summary>
  21. /// <param name="bytes">Input bytes read from PLC.</param>
  22. /// <returns>A <see cref="T:System.TimeSpan"/> object representing the value read from PLC.</returns>
  23. /// <exception cref="ArgumentOutOfRangeException">Thrown when the length of
  24. /// <paramref name="bytes"/> is not 4 or any value in <paramref name="bytes"/>
  25. /// is outside the valid range of values.</exception>
  26. public static System.TimeSpan FromByteArray(byte[] bytes)
  27. {
  28. var milliseconds = DInt.FromByteArray(bytes);
  29. return System.TimeSpan.FromMilliseconds(milliseconds);
  30. }
  31. /// <summary>
  32. /// Parses an array of <see cref="T:System.TimeSpan"/> values from bytes.
  33. /// </summary>
  34. /// <param name="bytes">Input bytes read from PLC.</param>
  35. /// <returns>An array of <see cref="T:System.TimeSpan"/> objects representing the values read from PLC.</returns>
  36. /// <exception cref="ArgumentOutOfRangeException">Thrown when the length of
  37. /// <paramref name="bytes"/> is not a multiple of 4 or any value in
  38. /// <paramref name="bytes"/> is outside the valid range of values.</exception>
  39. public static System.TimeSpan[] ToArray(byte[] bytes)
  40. {
  41. const int singleTimeSpanLength = 4;
  42. if (bytes.Length % singleTimeSpanLength != 0)
  43. throw new ArgumentOutOfRangeException(nameof(bytes), bytes.Length,
  44. $"Parsing an array of {nameof(System.TimeSpan)} requires a multiple of {singleTimeSpanLength} bytes of input data, input data is '{bytes.Length}' long.");
  45. var result = new System.TimeSpan[bytes.Length / singleTimeSpanLength];
  46. var milliseconds = DInt.ToArray(bytes);
  47. for (var i = 0; i < milliseconds.Length; i++)
  48. result[i] = System.TimeSpan.FromMilliseconds(milliseconds[i]);
  49. return result;
  50. }
  51. /// <summary>
  52. /// Converts a <see cref="T:System.TimeSpan"/> value to a byte array.
  53. /// </summary>
  54. /// <param name="timeSpan">The TimeSpan value to convert.</param>
  55. /// <returns>A byte array containing the S7 date time representation of <paramref name="timeSpan"/>.</returns>
  56. /// <exception cref="ArgumentOutOfRangeException">Thrown when the value of
  57. /// <paramref name="timeSpan"/> is before <see cref="P:SpecMinimumTimeSpan"/>
  58. /// or after <see cref="P:SpecMaximumTimeSpan"/>.</exception>
  59. public static byte[] ToByteArray(System.TimeSpan timeSpan)
  60. {
  61. if (timeSpan < SpecMinimumTimeSpan)
  62. throw new ArgumentOutOfRangeException(nameof(timeSpan), timeSpan,
  63. $"Time span '{timeSpan}' is before the minimum '{SpecMinimumTimeSpan}' supported in S7 time representation.");
  64. if (timeSpan > SpecMaximumTimeSpan)
  65. throw new ArgumentOutOfRangeException(nameof(timeSpan), timeSpan,
  66. $"Time span '{timeSpan}' is after the maximum '{SpecMaximumTimeSpan}' supported in S7 time representation.");
  67. return DInt.ToByteArray(Convert.ToInt32(timeSpan.TotalMilliseconds));
  68. }
  69. /// <summary>
  70. /// Converts an array of <see cref="T:System.TimeSpan"/> values to a byte array.
  71. /// </summary>
  72. /// <param name="timeSpans">The TimeSpan values to convert.</param>
  73. /// <returns>A byte array containing the S7 date time representations of <paramref name="timeSpans"/>.</returns>
  74. /// <exception cref="ArgumentOutOfRangeException">Thrown when any value of
  75. /// <paramref name="timeSpans"/> is before <see cref="P:SpecMinimumTimeSpan"/>
  76. /// or after <see cref="P:SpecMaximumTimeSpan"/>.</exception>
  77. public static byte[] ToByteArray(System.TimeSpan[] timeSpans)
  78. {
  79. var bytes = new List<byte>(timeSpans.Length * 4);
  80. foreach (var timeSpan in timeSpans) bytes.AddRange(ToByteArray(timeSpan));
  81. return bytes.ToArray();
  82. }
  83. }
  84. }