IOAccessor.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. namespace Aitex.Core.RT.IOCore
  5. {
  6. public delegate void SetValue<T>(int index, T Value);
  7. public delegate T GetValue<T>(int index);
  8. public enum IOType
  9. {
  10. DI,
  11. DO,
  12. AI,
  13. AO,
  14. }
  15. public class CustomIOName
  16. {
  17. public const string Shutter = "CustomIO_Shutter";
  18. public const string Spindle = "CustomIO_Spindle";
  19. public const string Bath1 = "CustomIO_Bath1";
  20. public const string Bath2 = "CustomIO_Bath2";
  21. public const string Bath3 = "CustomIO_Bath3";
  22. public const string Bath4 = "CustomIO_Bath4";
  23. public const string LineHeaterCh1 = "CustomIO_LineHeaterCh1";
  24. public const string LineHeaterCh2 = "CustomIO_LineHeaterCh2";
  25. public const string LineHeaterCh3 = "CustomIO_LineHeaterCh3";
  26. public const string LineHeaterCh4 = "CustomIO_LineHeaterCh4";
  27. public const string LineHeaterCh5 = "CustomIO_LineHeaterCh5";
  28. public const string LineHeaterCh6 = "CustomIO_LineHeaterCh6";
  29. public const string LineHeaterCh7 = "CustomIO_LineHeaterCh7";
  30. public const string LineHeaterCh8 = "CustomIO_LineHeaterCh8";
  31. public const string RobotX = "CustomIO_RobotX";
  32. public const string RobotZ = "CustomIO_RobotZ";
  33. public const string PLCStatus = "CustomIO_PLCStatus";
  34. public const string Tv = "CustomIO_TV";
  35. }
  36. public class IOAccessor<T>
  37. {
  38. protected string name;
  39. protected string addr;
  40. protected int index;
  41. protected T[] values;
  42. protected SetValue<T> setter = null;
  43. protected GetValue<T> getter = null;
  44. public T Value
  45. {
  46. get
  47. {
  48. return getter(index);
  49. }
  50. set
  51. {
  52. setter(index, value);
  53. }
  54. }
  55. public string Name
  56. {
  57. get { return name; }
  58. }
  59. public int Index
  60. {
  61. get { return index; }
  62. }
  63. public string Addr
  64. {
  65. get { return addr; }
  66. }
  67. public string StringIndex
  68. {
  69. get
  70. {
  71. return name.Substring(0, 2) + "-" + index.ToString();
  72. }
  73. }
  74. public int IoTableIndex { get; set; }
  75. public IOAccessor(string name, int index, T[] values)
  76. {
  77. this.name = name;
  78. this.index = index;
  79. this.values = values;
  80. getter = GetValue;
  81. setter = SetValue;
  82. }
  83. private T GetValue(int index)
  84. {
  85. Debug.Assert(values!= null && index >= 0 && index < values.Length);
  86. return values[index];
  87. }
  88. private void SetValue(int index, T value)
  89. {
  90. Debug.Assert(values != null && index >= 0 && index < values.Length);
  91. values[index] = value;
  92. }
  93. }
  94. public class DIAccessor : IOAccessor<bool>
  95. {
  96. private IOAccessor<bool> rawAccessor;
  97. public bool RawData
  98. {
  99. get
  100. {
  101. return rawAccessor.Value;
  102. }
  103. /// <summary>
  104. /// 修改数字量输入值
  105. ///
  106. /// 注意:
  107. /// 1. 此处修改的数字量输入值在调用IOManager.Update_Input方法后将被覆写
  108. /// 2. 只能修改索引号为500之后定义的软件DI信号点(规定0~499为硬件输入信号点区间)
  109. /// </summary>
  110. /// <param name="index">DI索引号</param>
  111. /// <param name="value"></param>
  112. set
  113. {
  114. rawAccessor.Value = value;
  115. }
  116. }
  117. public DIAccessor(string name, int index, bool[] values, bool[] raws)
  118. :base(name, index, values)
  119. {
  120. rawAccessor = new IOAccessor<bool>(name, index, raws);
  121. }
  122. }
  123. public class DOAccessor : IOAccessor<bool>
  124. {
  125. public DOAccessor(string name, int index, bool[] values)
  126. :base(name,index, values)
  127. {
  128. setter = SetValueSafe;
  129. }
  130. public bool SetValue(bool value, out string reason)
  131. {
  132. if (IO.CanSetDO(name, value, out reason))
  133. {
  134. values[index] = value;
  135. return true;
  136. }
  137. return false;
  138. }
  139. public bool Check(bool value, out string reason)
  140. {
  141. return IO.CanSetDO(name, value, out reason);
  142. }
  143. private void SetValueSafe(int index, bool value)
  144. {
  145. string reason;
  146. if (IO.CanSetDO(name, value, out reason))
  147. {
  148. values[index] = value;
  149. }
  150. }
  151. }
  152. public class AIAccessor : IOAccessor<float>
  153. {
  154. public AIAccessor(string name, int index, float[] values)
  155. :base(name,index, values)
  156. {
  157. }
  158. }
  159. public class AOAccessor : IOAccessor<float>
  160. {
  161. public AOAccessor(string name, int index, float[] values)
  162. :base(name,index, values)
  163. {
  164. }
  165. }
  166. }