IOAccessor.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using MECF.Framework.RT.Core.IoProviders;
  7. namespace Aitex.Core.RT.IOCore
  8. {
  9. public delegate void SetValue<T>(int index, T Value);
  10. public delegate T GetValue<T>(int index);
  11. public enum IOType
  12. {
  13. DI,
  14. DO,
  15. AI,
  16. AO,
  17. }
  18. public class IOAccessor<T>
  19. {
  20. protected string name;
  21. protected string addr;
  22. protected int index;
  23. protected T[] values;
  24. protected SetValue<T> setter = null;
  25. protected GetValue<T> getter = null;
  26. public T Value
  27. {
  28. get
  29. {
  30. return getter(index);
  31. }
  32. set
  33. {
  34. setter(index, value);
  35. }
  36. }
  37. public T[] Buffer
  38. {
  39. get
  40. {
  41. return values;
  42. }
  43. }
  44. public string Name
  45. {
  46. get { return name; }
  47. }
  48. public int Index
  49. {
  50. get { return index; }
  51. }
  52. public int BlockOffset
  53. {
  54. get;
  55. set;
  56. }
  57. public string Addr
  58. {
  59. get { return addr; }
  60. set { addr = value; }
  61. }
  62. public string Provider
  63. {
  64. get;
  65. set;
  66. }
  67. public string Description
  68. {
  69. get;
  70. set;
  71. }
  72. public string StringIndex
  73. {
  74. get
  75. {
  76. return name.Substring(0, 2) + "-" + index.ToString();
  77. }
  78. }
  79. public int IoTableIndex { get; set; }
  80. public IOAccessor(string name, int index, T[] values)
  81. {
  82. this.name = name;
  83. this.index = index;
  84. this.values = values;
  85. getter = GetValue;
  86. setter = SetValue;
  87. }
  88. private T GetValue(int index)
  89. {
  90. Debug.Assert(values != null && index >= 0 && index < values.Length);
  91. return values[index];
  92. }
  93. private void SetValue(int index, T value)
  94. {
  95. Debug.Assert(values != null && index >= 0 && index < values.Length);
  96. values[index] = value;
  97. }
  98. }
  99. public class DIAccessor : IOAccessor<bool>
  100. {
  101. private IOAccessor<bool> rawAccessor;
  102. public bool RawData
  103. {
  104. get
  105. {
  106. return rawAccessor.Value;
  107. }
  108. set
  109. {
  110. rawAccessor.Value = value;
  111. }
  112. }
  113. public DIAccessor(string name, int index, bool[] values, bool[] raws)
  114. : base(name, index, values)
  115. {
  116. rawAccessor = new IOAccessor<bool>(name, index, raws);
  117. }
  118. }
  119. public class DOAccessor : IOAccessor<bool>
  120. {
  121. public DOAccessor(string name, int index, bool[] values)
  122. : base(name, index, values)
  123. {
  124. setter = SetValueSafe;
  125. }
  126. public bool SetValue(bool value, out string reason)
  127. {
  128. if (IO.CanSetDO(name, value, out reason))
  129. {
  130. //IIoProvider provider = IoProviderManager.Instance.GetProvider(this.Provider);
  131. //if (provider != null)
  132. //{
  133. // if (!provider.SetValue(this, value))
  134. // {
  135. // reason = $"Write DO[{Name}] failed";
  136. // return false;
  137. // }
  138. //}
  139. values[index] = value;
  140. return true;
  141. }
  142. return false;
  143. }
  144. public async Task<bool> SetPulseValue(bool value, int milliSecondsDelay, bool holdValue = false)
  145. {
  146. if (IO.CanSetDO(name, value, out string _))
  147. {
  148. values[index] = value;
  149. }
  150. else
  151. {
  152. return false;
  153. }
  154. await Task.Delay(milliSecondsDelay);
  155. if (IO.CanSetDO(name, holdValue, out string _))
  156. {
  157. values[index] = holdValue;
  158. return true;
  159. }
  160. else
  161. {
  162. return false;
  163. }
  164. }
  165. public bool Check(bool value, out string reason)
  166. {
  167. return IO.CanSetDO(name, value, out reason);
  168. }
  169. private void SetValueSafe(int index, bool value)
  170. {
  171. string reason;
  172. if (IO.CanSetDO(name, value, out reason))
  173. {
  174. values[index] = value;
  175. //IIoProvider provider = IoProviderManager.Instance.GetProvider(this.Provider);
  176. //if (provider != null)
  177. //{
  178. // provider.SetValue(this, value);
  179. //}
  180. }
  181. }
  182. }
  183. public class AIAccessor : IOAccessor<short>
  184. {
  185. protected float[] _floatValues;
  186. public float FloatValue
  187. {
  188. get
  189. {
  190. return _floatValues[index];
  191. }
  192. set
  193. {
  194. _floatValues[index] = value;
  195. }
  196. }
  197. public AIAccessor(string name, int index, short[] values, float[] floatValues)
  198. : base(name, index, values)
  199. {
  200. _floatValues = floatValues;
  201. }
  202. }
  203. public class AOAccessor : IOAccessor<short>
  204. {
  205. protected float[] _floatValues;
  206. public float FloatValue
  207. {
  208. get
  209. {
  210. return _floatValues[index];
  211. }
  212. set
  213. {
  214. //IIoProvider provider = IoProviderManager.Instance.GetProvider(this.Provider);
  215. //if (provider != null)
  216. //{
  217. // provider.SetValueFloat(this, value);
  218. //}
  219. byte[] val2 = BitConverter.GetBytes(value);
  220. this.Buffer[this.Index] = BitConverter.ToInt16(val2, 0);
  221. this.Buffer[this.Index + 1] = BitConverter.ToInt16(val2, 2);
  222. _floatValues[index] = value;
  223. }
  224. }
  225. public AOAccessor(string name, int index, short[] values, float[] floatValues)
  226. : base(name, index, values)
  227. {
  228. setter = SetValueSafe;
  229. _floatValues = floatValues;
  230. }
  231. private void SetValueSafe(int index, short value)
  232. {
  233. values[index] = value;
  234. //IIoProvider provider = IoProviderManager.Instance.GetProvider(this.Provider);
  235. //if (provider != null)
  236. //{
  237. // provider.SetValue(this, value);
  238. //}
  239. }
  240. }
  241. }