IoProvider.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using Aitex.Core.RT.Event;
  5. using Aitex.Core.RT.Log;
  6. using Aitex.Core.Util;
  7. using MECF.Framework.Common.Equipment;
  8. using Venus_Core;
  9. namespace MECF.Framework.RT.Core.IoProviders
  10. {
  11. public abstract class IoProvider : IIoProvider
  12. {
  13. public string Module { get; set; }
  14. public string Name { get; set; }
  15. public bool IsOpened
  16. {
  17. get { return State == IoProviderStateEnum.Opened; }
  18. }
  19. public IoProviderStateEnum State { get; set; }
  20. protected PeriodicJob _thread;
  21. protected IIoBuffer _buffer;
  22. protected R_TRIG _trigError = new R_TRIG();
  23. protected string _source;
  24. protected XmlElement _nodeParameter;
  25. protected List<IoBlockItem> _blockSections;
  26. protected abstract void SetParameter(XmlElement nodeParameter);
  27. public abstract void Open();
  28. protected abstract void Close();
  29. protected abstract bool[] ReadDi(int offset, int size);
  30. protected abstract short[] ReadAi(int offset, int size);
  31. protected abstract void WriteDo(int offset, bool[] buffer);
  32. protected abstract void WriteAo(int offset, short[] buffer);
  33. public virtual void Initialize(string module, string name,List<IoBlockItem> lstBuffers , IIoBuffer buffer, XmlElement nodeParameter, Dictionary<int, string> ioMappingPathFile)
  34. {
  35. Module = module;
  36. Name = name;
  37. _source = module + "." + name;
  38. _buffer = buffer;
  39. _nodeParameter = nodeParameter;
  40. _blockSections = lstBuffers;
  41. buffer.SetBufferBlock(_source, lstBuffers);
  42. buffer.SetIoMap(_source, ioMappingPathFile);
  43. SetParameter(nodeParameter);
  44. State = IoProviderStateEnum.Uninitialized;
  45. _thread = new PeriodicJob(50, OnTimer, name);
  46. }
  47. public virtual void Initialize(string module, string name, List<IoBlockItem> lstBuffers, IIoBuffer buffer, XmlElement nodeParameter, string ioMappingPathFile, string ioModule )
  48. {
  49. Module = module;
  50. Name = name;
  51. _source = module + "." + name;
  52. _buffer = buffer;
  53. _nodeParameter = nodeParameter;
  54. _blockSections = lstBuffers;
  55. buffer.SetBufferBlock(_source, lstBuffers);
  56. buffer.SetIoMapByModule(_source, 0, ioMappingPathFile, ioModule);
  57. SetParameter(nodeParameter);
  58. State = IoProviderStateEnum.Uninitialized;
  59. _thread = new PeriodicJob(50, OnTimer, name);
  60. }
  61. protected void SetState(IoProviderStateEnum newState)
  62. {
  63. State = newState;
  64. }
  65. void TraceArray(bool[] data)
  66. {
  67. string[] values = new string[data.Length];
  68. for (int i = 0; i < data.Length; i++)
  69. {
  70. values[i++] = data[i] ? "1" : "0";
  71. }
  72. System.Diagnostics.Trace.WriteLine(string.Join(",", values));
  73. }
  74. protected virtual bool OnTimer()
  75. {
  76. if (State != IoProviderStateEnum.Opened)
  77. {
  78. SetState(IoProviderStateEnum.Opening);
  79. Open();
  80. }
  81. if (State == IoProviderStateEnum.Opened)
  82. {
  83. try
  84. {
  85. foreach (var bufferSection in _blockSections)
  86. {
  87. if (bufferSection.Type == IoType.DI)
  88. {
  89. bool[] diBuffer = ReadDi(bufferSection.Offset, bufferSection.Size);
  90. if (diBuffer != null)
  91. {
  92. _buffer.SetDiBuffer(_source, bufferSection.Offset, diBuffer);
  93. //TraceArray(diBuffer);
  94. }
  95. }
  96. else if (bufferSection.Type == IoType.AI)
  97. {
  98. short[] aiBuffer = ReadAi(bufferSection.Offset, bufferSection.Size);
  99. if (aiBuffer != null)
  100. {
  101. _buffer.SetAiBuffer(_source, bufferSection.Offset, aiBuffer);
  102. }
  103. }
  104. }
  105. Dictionary<int, short[]> aos = _buffer.GetAoBuffer(_source);
  106. if (aos != null)
  107. {
  108. foreach (var ao in aos)
  109. {
  110. WriteAo(ao.Key, ao.Value);
  111. }
  112. }
  113. Dictionary<int, bool[]> dos = _buffer.GetDoBuffer(_source);
  114. if (dos != null)
  115. {
  116. foreach (var doo in dos)
  117. {
  118. WriteDo(doo.Key, doo.Value);
  119. }
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. LOG.WriteExeption(ex);
  125. SetState(IoProviderStateEnum.Error);
  126. Close();
  127. }
  128. }
  129. _trigError.CLK = State == IoProviderStateEnum.Error;
  130. if (_trigError.Q)
  131. {
  132. EV.PostMessage(Module, EventEnum.DefaultAlarm, string.Format("{0} error", _source));
  133. }
  134. return true;
  135. }
  136. public void Reset()
  137. {
  138. _trigError.RST = true;
  139. }
  140. public void Start()
  141. {
  142. if(_thread != null)
  143. _thread.Start();
  144. }
  145. public void Stop()
  146. {
  147. SetState(IoProviderStateEnum.Closing);
  148. Close();
  149. _thread.Stop();
  150. }
  151. public virtual void SetValue(string variableName, object value)
  152. {
  153. }
  154. //public virtual bool WriteRecipe<T>(ModuleName moduleName, RecipeType currentRecipeType, T data)
  155. //{
  156. // return false;
  157. //}
  158. }
  159. }