SimulatorModulePlc.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using Aitex.Core.RT.IOCore;
  5. using Aitex.Core.Util;
  6. using MECF.Framework.Common.IOCore;
  7. using MECF.Framework.RT.Core.IoProviders;
  8. using MECF.Framework.Simulator.Core.IoProviders;
  9. namespace EfemDualSimulator.Instances
  10. {
  11. public class SimulatorModulePlc
  12. {
  13. private Random _rd = new Random();
  14. public ObservableCollection<NotifiableIoItem> DiItemList { get; set; }
  15. public ObservableCollection<NotifiableIoItem> DoItemList { get; set; }
  16. public ObservableCollection<NotifiableIoItem> AiItemList { get; set; }
  17. public ObservableCollection<NotifiableIoItem> AoItemList { get; set; }
  18. private string _source;
  19. public const int BufferSize = 200;
  20. PeriodicJob _threadTimer;
  21. protected List<PlcBuffer> _buffers = new List<PlcBuffer>();
  22. public SimulatorModulePlc(int port, string source, string ioMapPathFile, string module)
  23. {
  24. _source = source;
  25. _buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.DI, Offset = 0, Size = BufferSize, BoolValue = new bool[BufferSize] });
  26. _buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.DO, Offset = 0, Size = BufferSize, BoolValue = new bool[BufferSize] });
  27. _buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.AI, Offset = 0, Size = BufferSize, FloatValue = new float[BufferSize] });
  28. _buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.AO, Offset = 0, Size = BufferSize, FloatValue = new float[BufferSize] });
  29. List<IoBlockItem> lstBuffers = new List<IoBlockItem>();
  30. lstBuffers.Add(new IoBlockItem() { Type = IoType.DI, Offset = 0, Size = BufferSize });
  31. lstBuffers.Add(new IoBlockItem() { Type = IoType.DO, Offset = 0, Size = BufferSize });
  32. lstBuffers.Add(new IoBlockItem() { Type = IoType.AI, Offset = 0, Size = BufferSize });
  33. lstBuffers.Add(new IoBlockItem() { Type = IoType.AO, Offset = 0, Size = BufferSize });
  34. IoManager.Instance.SetBufferBlock(source, lstBuffers);
  35. IoManager.Instance.SetIoMap(source, 0, ioMapPathFile, module);
  36. Init();
  37. _threadTimer = new PeriodicJob(200, OnTimer, "SimulatorModulePlc", true);
  38. }
  39. void Init()
  40. {
  41. if (DiItemList == null)
  42. {
  43. List<DIAccessor> diItems = IO.GetDiList(_source);
  44. if (diItems != null)
  45. {
  46. DiItemList = new ObservableCollection<NotifiableIoItem>();
  47. foreach (var diItem in diItems)
  48. {
  49. NotifiableIoItem item = new NotifiableIoItem()
  50. {
  51. Name = diItem.Name,
  52. Index = diItem.Index,
  53. Description = diItem.Description,
  54. BoolValue = diItem.Value,
  55. Address = diItem.Addr,
  56. BlockOffset = diItem.BlockOffset,
  57. BlockIndex = diItem.Index,
  58. };
  59. DiItemList.Add(item);
  60. }
  61. }
  62. }
  63. if (DoItemList == null)
  64. {
  65. List<DOAccessor> doItems = IO.GetDoList(_source);
  66. if (doItems != null)
  67. {
  68. DoItemList = new ObservableCollection<NotifiableIoItem>();
  69. foreach (var ioItem in doItems)
  70. {
  71. NotifiableIoItem item = new NotifiableIoItem()
  72. {
  73. Name = ioItem.Name,
  74. Index = ioItem.Index,
  75. Description = ioItem.Description,
  76. BoolValue = ioItem.Value,
  77. Address = ioItem.Addr,
  78. BlockOffset = ioItem.BlockOffset,
  79. BlockIndex = ioItem.Index,
  80. };
  81. DoItemList.Add(item);
  82. }
  83. }
  84. }
  85. if (AiItemList == null)
  86. {
  87. List<AIAccessor> aiItems = IO.GetAiList(_source);
  88. if (aiItems != null)
  89. {
  90. AiItemList = new ObservableCollection<NotifiableIoItem>();
  91. foreach (var ioItem in aiItems)
  92. {
  93. NotifiableIoItem item = new NotifiableIoItem()
  94. {
  95. Name = ioItem.Name,
  96. Index = ioItem.Index,
  97. Description = ioItem.Description,
  98. ShortValue = ioItem.Value,
  99. Address = ioItem.Addr,
  100. BlockOffset = ioItem.BlockOffset,
  101. BlockIndex = ioItem.Index,
  102. };
  103. AiItemList.Add(item);
  104. }
  105. }
  106. }
  107. if (AoItemList == null)
  108. {
  109. List<AOAccessor> aoItems = IO.GetAoList(_source);
  110. if (aoItems != null)
  111. {
  112. AoItemList = new ObservableCollection<NotifiableIoItem>();
  113. foreach (var ioItem in aoItems)
  114. {
  115. NotifiableIoItem item = new NotifiableIoItem()
  116. {
  117. Name = ioItem.Name,
  118. Index = ioItem.Index,
  119. Description = ioItem.Description,
  120. ShortValue = ioItem.Value,
  121. Address = ioItem.Addr,
  122. BlockOffset = ioItem.BlockOffset,
  123. BlockIndex = ioItem.Index,
  124. };
  125. AoItemList.Add(item);
  126. }
  127. }
  128. }
  129. }
  130. protected bool OnTimer()
  131. {
  132. bool[] diBuffer = new bool[BufferSize];
  133. if (DiItemList != null)
  134. {
  135. foreach (var notifiableIoItem in DiItemList)
  136. {
  137. if (notifiableIoItem.HoldValue)
  138. {
  139. IO.DI[notifiableIoItem.Name].Value = notifiableIoItem.BoolValue;
  140. diBuffer[notifiableIoItem.BlockIndex] = notifiableIoItem.BoolValue;
  141. }
  142. else
  143. {
  144. notifiableIoItem.BoolValue = IO.DI[notifiableIoItem.Name].Value;
  145. diBuffer[notifiableIoItem.BlockIndex] = IO.DI[notifiableIoItem.Name].Value;
  146. }
  147. notifiableIoItem.InvokePropertyChanged("BoolValue");
  148. }
  149. }
  150. if (DoItemList != null)
  151. {
  152. foreach (var notifiableIoItem in DoItemList)
  153. {
  154. notifiableIoItem.BoolValue = IO.DO[notifiableIoItem.Name].Value;
  155. notifiableIoItem.InvokePropertyChanged("BoolValue");
  156. }
  157. }
  158. if (AiItemList != null)
  159. {
  160. foreach (var notifiableIoItem in AiItemList)
  161. {
  162. if (notifiableIoItem.HoldValue)
  163. {
  164. IO.AI[notifiableIoItem.Name].FloatValue = notifiableIoItem.FloatValue;
  165. }
  166. notifiableIoItem.FloatValue = IO.AI[notifiableIoItem.Name].FloatValue;
  167. notifiableIoItem.InvokePropertyChanged("FloatValue");
  168. }
  169. }
  170. if (AoItemList != null)
  171. {
  172. foreach (var notifiableIoItem in AoItemList)
  173. {
  174. notifiableIoItem.FloatValue = IO.AO[notifiableIoItem.Name].FloatValue;
  175. notifiableIoItem.InvokePropertyChanged("FloatValue");
  176. }
  177. }
  178. foreach (var plcBuffer in _buffers)
  179. {
  180. //IO修改 ---> PLC
  181. if (plcBuffer.Type == IoType.DI)
  182. {
  183. var ioBuffers = IoManager.Instance.GetDiBuffer(_source);
  184. if (ioBuffers != null)
  185. {
  186. foreach (var ioBuffer in ioBuffers)
  187. {
  188. if (plcBuffer.Offset == ioBuffer.Key)
  189. {
  190. //plcBuffer.BoolValue = ioBuffer.Value;
  191. for (int i = 0; i < plcBuffer.BoolValue.Length; i++)
  192. {
  193. plcBuffer.BoolValue[i] = diBuffer[i];
  194. }
  195. }
  196. }
  197. }
  198. }
  199. // PLC --> IO
  200. if (plcBuffer.Type == IoType.DO)
  201. {
  202. var ioBuffers = IoManager.Instance.GetDoBuffer(_source);
  203. if (ioBuffers != null)
  204. {
  205. foreach (var buffer in ioBuffers)
  206. {
  207. if (plcBuffer.Offset == buffer.Key)
  208. {
  209. //IoManager.Instance.SetDoBuffer(_source, plcBuffer.Offset, plcBuffer.BoolValue);
  210. }
  211. }
  212. }
  213. }
  214. //IO修改 ---> PLC
  215. if (plcBuffer.Type == IoType.AI)
  216. {
  217. var ioBuffers = IoManager.Instance.GetAiBufferFloat(_source);
  218. if (ioBuffers != null)
  219. {
  220. foreach (var buffer in ioBuffers)
  221. {
  222. if (plcBuffer.Offset == buffer.Key)
  223. {
  224. plcBuffer.FloatValue = buffer.Value;
  225. }
  226. }
  227. }
  228. }
  229. // PLC --> IO
  230. if (plcBuffer.Type == IoType.AO)
  231. {
  232. var ioBuffers = IoManager.Instance.GetAoBufferFloat(_source);
  233. if (ioBuffers != null)
  234. {
  235. foreach (var buffer in ioBuffers)
  236. {
  237. if (plcBuffer.Offset == buffer.Key)
  238. {
  239. //IoManager.Instance.SetAoBufferFloat(_source, plcBuffer.Offset, plcBuffer.FloatValue);
  240. }
  241. }
  242. }
  243. }
  244. }
  245. return true;
  246. }
  247. }
  248. }