DataItem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using S7.Net.Protocol.S7;
  2. using System;
  3. namespace S7.Net.Types
  4. {
  5. /// <summary>
  6. /// Create an instance of a memory block that can be read by using ReadMultipleVars
  7. /// </summary>
  8. public class DataItem
  9. {
  10. /// <summary>
  11. /// Memory area to read
  12. /// </summary>
  13. public DataType DataType { get; set; }
  14. /// <summary>
  15. /// Type of data to be read (default is bytes)
  16. /// </summary>
  17. public VarType VarType { get; set; }
  18. /// <summary>
  19. /// Address of memory area to read (example: for DB1 this value is 1, for T45 this value is 45)
  20. /// </summary>
  21. public int DB { get; set; }
  22. /// <summary>
  23. /// Address of the first byte to read
  24. /// </summary>
  25. public int StartByteAdr { get; set; }
  26. /// <summary>
  27. /// Addess of bit to read from StartByteAdr
  28. /// </summary>
  29. public byte BitAdr { get; set; }
  30. /// <summary>
  31. /// Number of variables to read
  32. /// </summary>
  33. public int Count { get; set; }
  34. /// <summary>
  35. /// Contains the value of the memory area after the read has been executed
  36. /// </summary>
  37. public object? Value { get; set; }
  38. /// <summary>
  39. /// Create an instance of DataItem
  40. /// </summary>
  41. public DataItem()
  42. {
  43. VarType = VarType.Byte;
  44. Count = 1;
  45. }
  46. /// <summary>
  47. /// Create an instance of <see cref="DataItem"/> from the supplied address.
  48. /// </summary>
  49. /// <param name="address">The address to create the DataItem for.</param>
  50. /// <returns>A new <see cref="DataItem"/> instance with properties parsed from <paramref name="address"/>.</returns>
  51. /// <remarks>The <see cref="Count" /> property is not parsed from the address.</remarks>
  52. public static DataItem FromAddress(string address)
  53. {
  54. PLCAddress.Parse(address, out var dataType, out var dbNumber, out var varType, out var startByte,
  55. out var bitNumber);
  56. return new DataItem
  57. {
  58. DataType = dataType,
  59. DB = dbNumber,
  60. VarType = varType,
  61. StartByteAdr = startByte,
  62. BitAdr = (byte) (bitNumber == -1 ? 0 : bitNumber)
  63. };
  64. }
  65. /// <summary>
  66. /// Create an instance of <see cref="DataItem"/> from the supplied address and value.
  67. /// </summary>
  68. /// <param name="address">The address to create the DataItem for.</param>
  69. /// <param name="value">The value to be applied to the DataItem.</param>
  70. /// <returns>A new <see cref="DataItem"/> instance with properties parsed from <paramref name="address"/> and the supplied value set.</returns>
  71. public static DataItem FromAddressAndValue<T>(string address, T value)
  72. {
  73. var dataItem = FromAddress(address);
  74. dataItem.Value = value;
  75. if (typeof(T).IsArray)
  76. {
  77. var array = ((Array?)dataItem.Value);
  78. if ( array != null)
  79. {
  80. dataItem.Count = array.Length;
  81. }
  82. }
  83. return dataItem;
  84. }
  85. internal static DataItemAddress GetDataItemAddress(DataItem dataItem)
  86. {
  87. return new DataItemAddress(dataItem.DataType, dataItem.DB, dataItem.StartByteAdr, Plc.VarTypeToByteLength(dataItem.VarType, dataItem.Count));
  88. }
  89. }
  90. }