You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.8 KiB

8 months ago
  1. using Mirle.Component.MPLC.DataBlocks;
  2. using Mirle.Component.MPLC.Interfaces;
  3. using System.Collections.Generic;
  4. namespace Mirle.Component.MPLC.SharedMemory
  5. {
  6. /// <summary>
  7. /// 共享記憶體讀取
  8. /// </summary>
  9. public class SMReadWriter : IMPLCProvider
  10. {
  11. /// <summary>
  12. /// 建構式
  13. /// </summary>
  14. public SMReadWriter() { }
  15. /// <summary>
  16. /// 資料區塊
  17. /// </summary>
  18. protected readonly List<SMDataBlock> _dataBlocks = new List<SMDataBlock>();
  19. /// <summary>
  20. /// 是否連線
  21. /// </summary>
  22. public bool IsConnected => true;
  23. /// <summary>
  24. /// 新增資料區塊
  25. /// </summary>
  26. /// <param name="newDataBlock">共享記憶體資料區塊</param>
  27. public virtual void AddDataBlock(SMDataBlock newDataBlock)
  28. {
  29. _dataBlocks.Add(newDataBlock);
  30. }
  31. /// <summary>
  32. /// 取得位元
  33. /// </summary>
  34. /// <param name="address">位置</param>
  35. /// <returns>True/False</returns>
  36. public virtual bool GetBit(string address)
  37. {
  38. foreach (SMDataBlock block in _dataBlocks)
  39. {
  40. if (block.TryGetBit(address, out bool value))
  41. return value;
  42. }
  43. return false;
  44. }
  45. /// <summary>
  46. /// 設置位元開啟
  47. /// </summary>
  48. /// <param name="address">位置</param>
  49. public virtual void SetBitOn(string address)
  50. {
  51. foreach (SMDataBlock block in _dataBlocks)
  52. {
  53. if (block.TrySetBitOn(address))
  54. return;
  55. }
  56. }
  57. /// <summary>
  58. /// 設置位元關閉
  59. /// </summary>
  60. /// <param name="address">位置</param>
  61. public virtual void SetBitOff(string address)
  62. {
  63. foreach (SMDataBlock block in _dataBlocks)
  64. {
  65. if (block.TrySetBitOff(address))
  66. return;
  67. }
  68. }
  69. /// <summary>
  70. /// 讀取字元
  71. /// </summary>
  72. /// <param name="address">位置</param>
  73. /// <returns>字元</returns>
  74. public virtual int ReadWord(string address)
  75. {
  76. foreach (SMDataBlock block in _dataBlocks)
  77. {
  78. if (block.TryGetWord(address, out int value))
  79. return value;
  80. }
  81. return 0;
  82. }
  83. /// <summary>
  84. /// 寫入字元
  85. /// </summary>
  86. /// <param name="address">位置</param>
  87. /// <param name="data">字元</param>
  88. public virtual void WriteWord(string address, int data)
  89. {
  90. foreach (SMDataBlock block in _dataBlocks)
  91. {
  92. if (block.TrySetWord(address, data))
  93. return;
  94. }
  95. }
  96. /// <summary>
  97. /// 讀取多個字元
  98. /// </summary>
  99. /// <param name="startAddress">起始位置</param>
  100. /// <param name="length">長度</param>
  101. /// <returns>字元陣列</returns>
  102. public virtual int[] ReadWords(string startAddress, int length)
  103. {
  104. foreach (SMDataBlock block in _dataBlocks)
  105. {
  106. if (block.TryGetWords(startAddress, out int[] data, length))
  107. return data;
  108. }
  109. return new int[length];
  110. }
  111. /// <summary>
  112. /// 寫入多個字元
  113. /// </summary>
  114. /// <param name="startAddress">起始位置</param>
  115. /// <param name="data">字元陣列</param>
  116. public virtual void WriteWords(string startAddress, int[] data)
  117. {
  118. foreach (SMDataBlock block in _dataBlocks)
  119. {
  120. if (block.TrySetWords(startAddress, data))
  121. return;
  122. }
  123. }
  124. }
  125. }