IoProvider.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Xml;
  7. using Aitex.Core.RT.DataCenter;
  8. using Aitex.Core.RT.Event;
  9. using Aitex.Core.RT.IOCore;
  10. using Aitex.Core.RT.Log;
  11. using Aitex.Core.Util;
  12. namespace MECF.Framework.RT.Core.IoProviders
  13. {
  14. //
  15. public abstract class IoProvider : IIoProvider
  16. {
  17. public string Module { get; set; }
  18. public string Name { get; set; }
  19. public bool IsOpened
  20. {
  21. get { return State == IoProviderStateEnum.Opened; }
  22. }
  23. public IoProviderStateEnum State { get; set; }
  24. protected PeriodicJob _thread;
  25. protected IIoBuffer _buffer;
  26. protected R_TRIG _trigError = new R_TRIG();
  27. protected RD_TRIG _trigNotConnected = new RD_TRIG();
  28. protected string _source;
  29. protected XmlElement _nodeParameter;
  30. protected List<IoBlockItem> _blockSections;
  31. protected abstract void SetParameter(XmlElement nodeParameter);
  32. protected abstract void Open();
  33. protected abstract void Close();
  34. protected abstract bool[] ReadDi(int offset, int size);
  35. protected abstract short[] ReadAi(int offset, int size);
  36. protected abstract void WriteDo(int offset, bool[] buffer);
  37. protected abstract void WriteAo(int offset, short[] buffer);
  38. protected virtual float[] ReadAiFloat(int offset, int size)
  39. {
  40. return null;
  41. }
  42. protected virtual void WriteAoFloat(int offset, float[] buffer)
  43. {
  44. }
  45. protected virtual bool[] ReadDO(int offset, int size)
  46. {
  47. return null;
  48. }
  49. /// <summary>
  50. /// 单腔体
  51. /// </summary>
  52. /// <param name="module"></param>
  53. /// <param name="name"></param>
  54. /// <param name="lstBuffers"></param>
  55. /// <param name="buffer"></param>
  56. /// <param name="nodeParameter"></param>
  57. /// <param name="ioMappingPathFile"></param>
  58. public virtual void Initialize(string module, string name,List<IoBlockItem> lstBuffers , IIoBuffer buffer, XmlElement nodeParameter, Dictionary<int, string> ioMappingPathFile)
  59. {
  60. Module = module;
  61. Name = name;
  62. _source = module + "." + name;
  63. _buffer = buffer;
  64. _nodeParameter = nodeParameter;
  65. _blockSections = lstBuffers;
  66. buffer.SetBufferBlock(_source, lstBuffers);
  67. buffer.SetIoMap(_source, ioMappingPathFile);
  68. SetParameter(nodeParameter);
  69. State = IoProviderStateEnum.Uninitialized;
  70. _thread = new PeriodicJob(50, OnTimer, name);
  71. }
  72. /// <summary>
  73. /// 多腔体
  74. /// </summary>
  75. /// <param name="module"></param>
  76. /// <param name="name"></param>
  77. /// <param name="lstBuffers"></param>
  78. /// <param name="buffer"></param>
  79. /// <param name="nodeParameter"></param>
  80. /// <param name="ioMappingPathFile"></param>
  81. /// <param name="ioModule"></param>
  82. public virtual void Initialize(string module, string name, List<IoBlockItem> lstBuffers, IIoBuffer buffer, XmlElement nodeParameter, string ioMappingPathFile, string ioModule )
  83. {
  84. Module = module;
  85. Name = name;
  86. _source = module + "." + name;
  87. _buffer = buffer;
  88. _nodeParameter = nodeParameter;
  89. _blockSections = lstBuffers;
  90. buffer.SetBufferBlock(_source, lstBuffers);
  91. buffer.SetIoMapByModule(_source, 0, ioMappingPathFile, ioModule);
  92. SetParameter(nodeParameter);
  93. State = IoProviderStateEnum.Uninitialized;
  94. _thread = new PeriodicJob(50, OnTimer, name);
  95. }
  96. protected void SetState(IoProviderStateEnum newState)
  97. {
  98. State = newState;
  99. }
  100. public void TraceArray(bool[] data)
  101. {
  102. string[] values = new string[data.Length];
  103. for (int i = 0; i < data.Length; i++)
  104. {
  105. values[i++] = data[i] ? "1" : "0";
  106. }
  107. System.Diagnostics.Trace.WriteLine(string.Join(",", values));
  108. }
  109. protected virtual bool OnTimer()
  110. {
  111. if (State == IoProviderStateEnum.Uninitialized)
  112. {
  113. SetState(IoProviderStateEnum.Opening);
  114. Open();
  115. }
  116. if (State == IoProviderStateEnum.Opened)
  117. {
  118. try
  119. {
  120. bool isAIOFloatType = false;
  121. foreach (var bufferSection in _blockSections)
  122. {
  123. if (bufferSection.Type == IoType.DI)
  124. {
  125. bool[] diBuffer = ReadDi(bufferSection.Offset, bufferSection.Size);
  126. if (diBuffer != null)
  127. {
  128. _buffer.SetDiBuffer(_source, bufferSection.Offset, diBuffer);
  129. //TraceArray(diBuffer);
  130. }
  131. }
  132. else if (bufferSection.Type == IoType.AI)
  133. {
  134. short[] aiBuffer = ReadAi(bufferSection.Offset, bufferSection.Size);
  135. if (aiBuffer != null)
  136. {
  137. var aiBufferFloat = new float[aiBuffer.Length];
  138. for(var i = 0; i < aiBuffer.Length - 1; i++)
  139. {
  140. byte[] low = BitConverter.GetBytes(aiBuffer[i]);
  141. byte[] high = BitConverter.GetBytes(aiBuffer[i + 1]);
  142. aiBufferFloat[i] = BitConverter.ToSingle(new[] { low[0], low[1], high[0], high[1] }, 0);
  143. }
  144. _buffer.SetAiBuffer(_source, bufferSection.Offset, aiBuffer);
  145. if (bufferSection.AIOType == typeof(float))
  146. {
  147. isAIOFloatType = true;
  148. _buffer.SetAiBufferFloat(_source, bufferSection.Offset, Array.ConvertAll(aiBufferFloat, x => (float)x).ToArray());
  149. }
  150. }
  151. }
  152. else if (bufferSection.Type == IoType.DO)
  153. {
  154. bool[] doBuffer = ReadDO(bufferSection.Offset, bufferSection.Size);
  155. if (doBuffer != null && doBuffer.Length > 0)
  156. {
  157. _buffer.SetDoBuffer(_source, bufferSection.Offset, doBuffer);
  158. }
  159. }
  160. }
  161. Dictionary<int, short[]> aos = _buffer.GetAoBuffer(_source);
  162. if (aos != null)
  163. {
  164. foreach (var ao in aos)
  165. {
  166. WriteAo(ao.Key, ao.Value);
  167. if (isAIOFloatType)
  168. WriteAoFloat(ao.Key, Array.ConvertAll(ao.Value, x => (float)x).ToArray());
  169. }
  170. }
  171. Dictionary<int, bool[]> dos = _buffer.GetDoBuffer(_source);
  172. if (dos != null)
  173. {
  174. foreach (var doo in dos)
  175. {
  176. WriteDo(doo.Key, doo.Value);
  177. }
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. LOG.Write(ex);
  183. SetState(IoProviderStateEnum.Error);
  184. Close();
  185. }
  186. }
  187. _trigError.CLK = State == IoProviderStateEnum.Error;
  188. if (_trigError.Q)
  189. {
  190. EV.PostAlarmLog(Module, $"{_source} error");
  191. }
  192. _trigNotConnected.CLK = State != IoProviderStateEnum.Opened;
  193. if (_trigNotConnected.T)
  194. {
  195. EV.PostInfoLog(Module, $"{_source} connected");
  196. }
  197. if (_trigNotConnected.R)
  198. {
  199. EV.PostWarningLog(Module, $"{_source} not connected");
  200. }
  201. return true;
  202. }
  203. public virtual void Reset()
  204. {
  205. _trigError.RST = true;
  206. _trigNotConnected.RST = true;
  207. }
  208. public void Start()
  209. {
  210. if(_thread != null)
  211. _thread.Start();
  212. }
  213. public void Stop()
  214. {
  215. SetState(IoProviderStateEnum.Closing);
  216. Close();
  217. Thread.Sleep(300);
  218. _thread.Stop();
  219. }
  220. public virtual bool SetValue(AOAccessor aoItem, short value)
  221. {
  222. return true;
  223. }
  224. public virtual bool SetValueFloat(AOAccessor aoItem, float value)
  225. {
  226. return true;
  227. }
  228. public virtual bool SetValue(DOAccessor doItem, bool value)
  229. {
  230. return true;
  231. }
  232. }
  233. }