SimulatorPlc.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 Venus_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 (_port == 6731)
  145. //{
  146. // Stop();
  147. //}
  148. if (DiItemList != null)
  149. {
  150. foreach (var notifiableIoItem in DiItemList)
  151. {
  152. if (notifiableIoItem.HoldValue)
  153. {
  154. IO.DI[notifiableIoItem.Name].Value = notifiableIoItem.BoolValue;
  155. }
  156. notifiableIoItem.BoolValue = IO.DI[notifiableIoItem.Name].Value;
  157. notifiableIoItem.InvokePropertyChanged("BoolValue");
  158. }
  159. }
  160. if (DoItemList != null)
  161. {
  162. foreach (var notifiableIoItem in DoItemList)
  163. {
  164. notifiableIoItem.BoolValue = IO.DO[notifiableIoItem.Name].Value;
  165. notifiableIoItem.InvokePropertyChanged("BoolValue");
  166. }
  167. }
  168. if (AiItemList != null)
  169. {
  170. foreach (var notifiableIoItem in AiItemList)
  171. {
  172. if (notifiableIoItem.HoldValue)
  173. {
  174. IO.AI[notifiableIoItem.Name].Value = notifiableIoItem.ShortValue;
  175. }
  176. notifiableIoItem.ShortValue = IO.AI[notifiableIoItem.Name].Value;
  177. notifiableIoItem.InvokePropertyChanged("ShortValue");
  178. }
  179. }
  180. if (AoItemList != null)
  181. {
  182. foreach (var notifiableIoItem in AoItemList)
  183. {
  184. notifiableIoItem.ShortValue = IO.AO[notifiableIoItem.Name].Value;
  185. notifiableIoItem.InvokePropertyChanged("ShortValue");
  186. }
  187. }
  188. foreach (var plcBuffer in _buffers)
  189. {
  190. //IO修改 ---> PLC
  191. if (plcBuffer.Type == IoType.DI)
  192. {
  193. var ioBuffers = IoManager.Instance.GetDiBuffer(_source);
  194. if (ioBuffers != null)
  195. {
  196. foreach (var ioBuffer in ioBuffers)
  197. {
  198. if (plcBuffer.Offset == ioBuffer.Key)
  199. {
  200. plcBuffer.BoolValue = ioBuffer.Value;
  201. //for (int i = 0; i < plcBuffer.BoolValue.Length; i++)
  202. //{
  203. // plcBuffer.BoolValue[i] = true;
  204. //}
  205. }
  206. }
  207. }
  208. }
  209. // PLC --> IO
  210. if (plcBuffer.Type == IoType.DO)
  211. {
  212. var ioBuffers = IoManager.Instance.GetDoBuffer(_source);
  213. if (ioBuffers != null)
  214. {
  215. foreach (var buffer in ioBuffers)
  216. {
  217. if (plcBuffer.Offset == buffer.Key)
  218. {
  219. IoManager.Instance.SetDoBuffer(_source, plcBuffer.Offset, plcBuffer.BoolValue);
  220. }
  221. }
  222. }
  223. }
  224. //IO修改 ---> PLC
  225. if (plcBuffer.Type == IoType.AI)
  226. {
  227. var ioBuffers = IoManager.Instance.GetAiBuffer(_source);
  228. if (ioBuffers != null)
  229. {
  230. foreach (var buffer in ioBuffers)
  231. {
  232. if (plcBuffer.Offset == buffer.Key)
  233. {
  234. plcBuffer.ShortValue = Array.ConvertAll<short, ushort>(buffer.Value, x => (ushort)x);
  235. }
  236. }
  237. }
  238. }
  239. // PLC --> IO
  240. if (plcBuffer.Type == IoType.AO)
  241. {
  242. var ioBuffers = IoManager.Instance.GetAoBuffer(_source);
  243. if (ioBuffers != null)
  244. {
  245. foreach (var buffer in ioBuffers)
  246. {
  247. if (plcBuffer.Offset == buffer.Key)
  248. {
  249. IoManager.Instance.SetAoBuffer(_source, plcBuffer.Offset, Array.ConvertAll<ushort, short>(plcBuffer.ShortValue, x => (short)x));
  250. }
  251. }
  252. }
  253. }
  254. }
  255. return true;
  256. }
  257. }
  258. }