SimulatorPlc.cs 11 KB

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