SimulatorPlc.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. namespace FurnaceSimulator.Instances
  12. {
  13. public class SimulatorPlc : MCProtocolPlcSimulator
  14. {
  15. private Random _rd = new Random();
  16. public ObservableCollection<NotifiableIoItem> DiItemList { get; set; }
  17. public ObservableCollection<NotifiableIoItem> DoItemList { get; set; }
  18. public ObservableCollection<NotifiableIoItem> AiItemList { get; set; }
  19. public ObservableCollection<NotifiableIoItem> AoItemList { get; set; }
  20. private string _source;
  21. public const int DIOBufferSize = 10000;
  22. public const int AIOBufferSize = 60000;
  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[DIOBufferSize], Type = IoType.DI, Offset = 0, Size = DIOBufferSize, BoolValue = new bool[DIOBufferSize] });
  28. _buffers.Add(new PlcBuffer() { Buffer = new byte[DIOBufferSize], Type = IoType.DO, Offset = 0, Size = DIOBufferSize, BoolValue = new bool[DIOBufferSize] });
  29. _buffers.Add(new PlcBuffer() { Buffer = new byte[AIOBufferSize], Type = IoType.AI, Offset = 0, Size = AIOBufferSize, ShortValue = new ushort[AIOBufferSize], FloatValue = new float[AIOBufferSize] });
  30. _buffers.Add(new PlcBuffer() { Buffer = new byte[AIOBufferSize], Type = IoType.AO, Offset = 0, Size = AIOBufferSize, ShortValue = new ushort[AIOBufferSize], FloatValue = new float[AIOBufferSize] });
  31. List<IoBlockItem> lstBuffers = new List<IoBlockItem>();
  32. lstBuffers.Add(new IoBlockItem() { Type = IoType.DI, Offset = 0, Size = DIOBufferSize });
  33. lstBuffers.Add(new IoBlockItem() { Type = IoType.DO, Offset = 0, Size = DIOBufferSize });
  34. lstBuffers.Add(new IoBlockItem() { Type = IoType.AI, Offset = 0, Size = AIOBufferSize });
  35. lstBuffers.Add(new IoBlockItem() { Type = IoType.AO, Offset = 0, Size = AIOBufferSize });
  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 = IO.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 = DiItemList.Count,
  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 = IO.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 = DoItemList.Count,
  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 = IO.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 = AiItemList.Count,
  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 = IO.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 = AoItemList.Count,
  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. bool[] diBuffer = new bool[DIOBufferSize];
  149. if (DiItemList != null)
  150. {
  151. foreach (var notifiableIoItem in DiItemList)
  152. {
  153. if (notifiableIoItem.HoldValue)
  154. {
  155. IO.DI[notifiableIoItem.Name].Value = notifiableIoItem.BoolValue;
  156. diBuffer[notifiableIoItem.BlockIndex] = notifiableIoItem.BoolValue;
  157. }
  158. else
  159. {
  160. notifiableIoItem.BoolValue = IO.DI[notifiableIoItem.Name].Value;
  161. diBuffer[notifiableIoItem.BlockIndex] = IO.DI[notifiableIoItem.Name].Value;
  162. }
  163. notifiableIoItem.InvokePropertyChanged("BoolValue");
  164. }
  165. }
  166. if (DoItemList != null)
  167. {
  168. foreach (var notifiableIoItem in DoItemList)
  169. {
  170. notifiableIoItem.BoolValue = IO.DO[notifiableIoItem.Name].Value;
  171. notifiableIoItem.InvokePropertyChanged("BoolValue");
  172. }
  173. }
  174. if (AiItemList != null)
  175. {
  176. foreach (var notifiableIoItem in AiItemList)
  177. {
  178. if (notifiableIoItem.HoldValue)
  179. {
  180. IO.AI[notifiableIoItem.Name].Value = notifiableIoItem.ShortValue;
  181. IO.AI[notifiableIoItem.Name].FloatValue = notifiableIoItem.FloatValue;
  182. }
  183. notifiableIoItem.ShortValue = IO.AI[notifiableIoItem.Name].Value;
  184. notifiableIoItem.InvokePropertyChanged("ShortValue");
  185. notifiableIoItem.FloatValue = IO.AI[notifiableIoItem.Name].FloatValue;
  186. notifiableIoItem.InvokePropertyChanged("FloatValue");
  187. }
  188. }
  189. if (AoItemList != null)
  190. {
  191. foreach (var notifiableIoItem in AoItemList)
  192. {
  193. notifiableIoItem.ShortValue = IO.AO[notifiableIoItem.Name].Value;
  194. notifiableIoItem.InvokePropertyChanged("ShortValue");
  195. notifiableIoItem.FloatValue = IO.AO[notifiableIoItem.Name].FloatValue;
  196. notifiableIoItem.InvokePropertyChanged("FloatValue");
  197. }
  198. }
  199. foreach (var plcBuffer in _buffers)
  200. {
  201. //IO修改 ---> PLC
  202. if (plcBuffer.Type == IoType.DI)
  203. {
  204. var ioBuffers = IoManager.Instance.GetDiBuffer(_source);
  205. if (ioBuffers != null)
  206. {
  207. foreach (var ioBuffer in ioBuffers)
  208. {
  209. if (plcBuffer.Offset == ioBuffer.Key)
  210. {
  211. //plcBuffer.BoolValue = ioBuffer.Value;
  212. for (int i = 0; i < plcBuffer.BoolValue.Length; i++)
  213. {
  214. plcBuffer.BoolValue[i] = diBuffer[i];
  215. }
  216. }
  217. }
  218. }
  219. }
  220. // PLC --> IO
  221. if (plcBuffer.Type == IoType.DO)
  222. {
  223. var ioBuffers = IoManager.Instance.GetDoBuffer(_source);
  224. if (ioBuffers != null)
  225. {
  226. foreach (var buffer in ioBuffers)
  227. {
  228. if (plcBuffer.Offset == buffer.Key)
  229. {
  230. //IoManager.Instance.SetDoBuffer(_source, plcBuffer.Offset, plcBuffer.BoolValue);
  231. }
  232. }
  233. }
  234. }
  235. //IO修改 ---> PLC
  236. if (plcBuffer.Type == IoType.AI)
  237. {
  238. var ioBuffers = IoManager.Instance.GetAiBuffer(_source);
  239. if (ioBuffers != null)
  240. {
  241. foreach (var buffer in ioBuffers)
  242. {
  243. if (plcBuffer.Offset == buffer.Key)
  244. {
  245. plcBuffer.ShortValue = Array.ConvertAll<short, ushort>(buffer.Value, x => (ushort)x);
  246. }
  247. }
  248. }
  249. var ioBuffersFloat = IoManager.Instance.GetAiBufferFloat(_source);
  250. if (ioBuffers != null)
  251. {
  252. foreach (var buffer in ioBuffersFloat)
  253. {
  254. if (plcBuffer.Offset == buffer.Key)
  255. {
  256. plcBuffer.FloatValue = buffer.Value;
  257. }
  258. }
  259. }
  260. }
  261. // PLC --> IO
  262. if (plcBuffer.Type == IoType.AO)
  263. {
  264. var ioBuffers = IoManager.Instance.GetAoBuffer(_source);
  265. if (ioBuffers != null)
  266. {
  267. foreach (var buffer in ioBuffers)
  268. {
  269. if (plcBuffer.Offset == buffer.Key)
  270. {
  271. //IoManager.Instance.SetAoBuffer(_source, plcBuffer.Offset, Array.ConvertAll<ushort, short>(plcBuffer.ShortValue, x => (short)x));
  272. }
  273. }
  274. }
  275. }
  276. }
  277. return true;
  278. }
  279. }
  280. }