SimulatorIO.cs 10 KB

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