SimulatorPlc.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using Aitex.Core.RT.IOCore;
  2. using MECF.Framework.Common.IOCore;
  3. using MECF.Framework.RT.Core.IoProviders;
  4. using MECF.Framework.Simulator.Core.IoProviders;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using ATIO = Aitex.Core.RT.IOCore.IO;
  12. namespace athosSimulator.IO
  13. {
  14. public class SimulatorPlc : MCProtocolPlcSimulator
  15. {
  16. private Random _rd = new Random();
  17. public ObservableCollection<NotifiableIoItem> DiItemList { get; set; }
  18. public ObservableCollection<NotifiableIoItem> DoItemList { get; set; }
  19. public ObservableCollection<NotifiableIoItem> AiItemList { get; set; }
  20. public ObservableCollection<NotifiableIoItem> AoItemList { get; set; }
  21. private string _source;
  22. public const int BufferSize = 640;
  23. public SimulatorPlc(int port, string source, string ioMapPathFile, string module)
  24. : base("127.0.0.1", port)
  25. {
  26. _source = source;
  27. _buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.DI, Offset = 0, Size = BufferSize, BoolValue = new bool[BufferSize] });
  28. _buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.DO, Offset = 0, Size = BufferSize, BoolValue = new bool[BufferSize] });
  29. _buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.AI, Offset = 0, Size = BufferSize, ShortValue = new ushort[BufferSize] });
  30. _buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.AO, Offset = 0, Size = BufferSize, ShortValue = new ushort[BufferSize] });
  31. List<IoBlockItem> lstBuffers = new List<IoBlockItem>();
  32. lstBuffers.Add(new IoBlockItem() { Type = IoType.DI, Offset = 0, Size = BufferSize });
  33. lstBuffers.Add(new IoBlockItem() { Type = IoType.DO, Offset = 0, Size = BufferSize });
  34. lstBuffers.Add(new IoBlockItem() { Type = IoType.AI, Offset = 0, Size = BufferSize });
  35. lstBuffers.Add(new IoBlockItem() { Type = IoType.AO, Offset = 0, Size = BufferSize });
  36. IoManager.Instance.SetBufferBlock(source, lstBuffers);
  37. IoManager.Instance.SetIoMap(source, 0, ioMapPathFile, module);
  38. Init();
  39. SetDefaultValue();
  40. //SetScCommand = new DelegateCommand<string>(SetSc);
  41. }
  42. //private void SetSc(string obj)
  43. //{
  44. // NotifiableSCConfigItem item = ScItemList.First(x => x.Name == obj);
  45. // if (item.Type != SCConfigType.StringType.ToString() && string.IsNullOrEmpty(item.SetPoint))
  46. // return;
  47. // SC.SetItemValue(obj, item.SetPoint == null ? "" : item.SetPoint);
  48. // item.StringValue = SC.GetItemValue(obj).ToString();
  49. // item.InvokePropertyChanged(nameof(item.StringValue));
  50. //}
  51. void Init()
  52. {
  53. if (DiItemList == null)
  54. {
  55. List<DIAccessor> diItems = ATIO.GetDiList(_source);
  56. if (diItems != null)
  57. {
  58. DiItemList = new ObservableCollection<NotifiableIoItem>();
  59. foreach (var diItem in diItems)
  60. {
  61. NotifiableIoItem item = new NotifiableIoItem()
  62. {
  63. Name = diItem.Name,
  64. Index = diItem.Index,
  65. Description = diItem.Description,
  66. BoolValue = diItem.Value,
  67. Address = diItem.Addr,
  68. BlockOffset = diItem.BlockOffset,
  69. BlockIndex = diItem.Index,
  70. };
  71. DiItemList.Add(item);
  72. }
  73. }
  74. }
  75. if (DoItemList == null)
  76. {
  77. List<DOAccessor> doItems = ATIO.GetDoList(_source);
  78. if (doItems != null)
  79. {
  80. DoItemList = new ObservableCollection<NotifiableIoItem>();
  81. foreach (var ioItem in doItems)
  82. {
  83. NotifiableIoItem item = new NotifiableIoItem()
  84. {
  85. Name = ioItem.Name,
  86. Index = ioItem.Index,
  87. Description = ioItem.Description,
  88. BoolValue = ioItem.Value,
  89. Address = ioItem.Addr,
  90. BlockOffset = ioItem.BlockOffset,
  91. BlockIndex = ioItem.Index,
  92. };
  93. DoItemList.Add(item);
  94. }
  95. }
  96. }
  97. if (AiItemList == null)
  98. {
  99. List<AIAccessor> aiItems = ATIO.GetAiList(_source);
  100. if (aiItems != null)
  101. {
  102. AiItemList = new ObservableCollection<NotifiableIoItem>();
  103. foreach (var ioItem in aiItems)
  104. {
  105. NotifiableIoItem item = new NotifiableIoItem()
  106. {
  107. Name = ioItem.Name,
  108. Index = ioItem.Index,
  109. Description = ioItem.Description,
  110. ShortValue = ioItem.Value,
  111. Address = ioItem.Addr,
  112. BlockOffset = ioItem.BlockOffset,
  113. BlockIndex = ioItem.Index,
  114. };
  115. AiItemList.Add(item);
  116. }
  117. }
  118. }
  119. if (AoItemList == null)
  120. {
  121. List<AOAccessor> aoItems = ATIO.GetAoList(_source);
  122. if (aoItems != null)
  123. {
  124. AoItemList = new ObservableCollection<NotifiableIoItem>();
  125. foreach (var ioItem in aoItems)
  126. {
  127. NotifiableIoItem item = new NotifiableIoItem()
  128. {
  129. Name = ioItem.Name,
  130. Index = ioItem.Index,
  131. Description = ioItem.Description,
  132. ShortValue = ioItem.Value,
  133. Address = ioItem.Addr,
  134. BlockOffset = ioItem.BlockOffset,
  135. BlockIndex = ioItem.Index,
  136. };
  137. AoItemList.Add(item);
  138. }
  139. }
  140. }
  141. }
  142. private void SetDefaultValue()
  143. {
  144. //IO.DI[IoNameSorter6LP.DI_Maintenance].Value = true;
  145. }
  146. protected override bool OnTimer()
  147. {
  148. if (DiItemList != null)
  149. {
  150. foreach (var notifiableIoItem in DiItemList)
  151. {
  152. if (notifiableIoItem.HoldValue)
  153. {
  154. ATIO.DI[notifiableIoItem.Name].Value = notifiableIoItem.BoolValue;
  155. }
  156. notifiableIoItem.BoolValue = ATIO.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 = ATIO.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. ATIO.AI[notifiableIoItem.Name].Value = notifiableIoItem.ShortValue;
  175. }
  176. notifiableIoItem.ShortValue = ATIO.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 = ATIO.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. }