SimulatorCard.cs 11 KB

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