SimulatorBuffer.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using Aitex.Core.RT.IOCore;
  7. using Aitex.Core.RT.PLC;
  8. using Aitex.Core.RT.Simulator;
  9. using Aitex.Core.Util;
  10. using Aitex.Triton160.RT.PLC;
  11. namespace Aitex.Triton160.RT.Simulator
  12. {
  13. public class SimulatorBuffer : IDataBuffer<PLC_INPUT_DATA, PLC_OUTPUT_DATA>
  14. {
  15. private PLC_INPUT_DATA _input;
  16. private PLC_OUTPUT_DATA _output;
  17. public PLC_INPUT_DATA Input
  18. {
  19. get
  20. {
  21. return _input;
  22. }
  23. set
  24. {
  25. _input = value;
  26. }
  27. }
  28. public PLC_OUTPUT_DATA Output
  29. {
  30. get
  31. {
  32. return _output;
  33. }
  34. set
  35. {
  36. _output = value;
  37. }
  38. }
  39. List<Tuple<int, int, string>> _diMap;
  40. List<Tuple<int, int, string>> _doMap;
  41. List<Tuple<int, int, string>> _aiMap;
  42. List<Tuple<int, int, string>> _aoMap;
  43. public SimulatorBuffer()
  44. {
  45. _input = new PLC_INPUT_DATA();
  46. _input.DI = new Byte[IOGroupManager.DioBlockLength];
  47. _input.AI = new float[IOGroupManager.AioBlockLength];
  48. _output = new PLC_OUTPUT_DATA();
  49. _output.DO = new Byte[IOGroupManager.DioBlockLength];
  50. _output.AO = new float[IOGroupManager.AioBlockLength];
  51. }
  52. public void Initialize(List<Tuple<int, int, string>> diMap, List<Tuple<int, int, string>> doMap, List<Tuple<int, int, string>> aiMap, List<Tuple<int, int, string>> aoMap)
  53. {
  54. _diMap = diMap;
  55. _doMap = doMap;
  56. _aiMap = aiMap;
  57. _aoMap = aoMap;
  58. }
  59. public Tuple<int, int, string> FindAI(string name)
  60. {
  61. return _aiMap.Find(item => item.Item3 == name);
  62. }
  63. public Tuple<int, int, string> FindAI(int iodefineIndex)
  64. {
  65. return _aiMap.Find(item => item.Item1 == iodefineIndex);
  66. }
  67. public Tuple<int, int, string> FindAO(string name)
  68. {
  69. return _aoMap.Find(item => item.Item3 == name);
  70. }
  71. public Tuple<int, int, string> FindAO(int iodefineIndex)
  72. {
  73. return _aoMap.Find(item => item.Item1 == iodefineIndex);
  74. }
  75. public Tuple<int, int, string> FindDI(string name)
  76. {
  77. return _diMap.Find(item => item.Item3 == name);
  78. }
  79. public Tuple<int, int, string> FindDI(int iodefineIndex)
  80. {
  81. return _diMap.Find(item => item.Item1 == iodefineIndex);
  82. }
  83. public Tuple<int, int, string> FindDO(int iodefineIndex)
  84. {
  85. return _doMap.Find(item => item.Item1 == iodefineIndex);
  86. }
  87. public Tuple<int, int, string> FindDO(string name)
  88. {
  89. return _doMap.Find(item => item.Item3 == name);
  90. }
  91. public void UpdateDI(bool[] values)
  92. {
  93. }
  94. public void SetDI(string name, bool value)
  95. {
  96. SetDI(name, value ? (byte)1 : (byte)0);
  97. }
  98. public void SetDI(string name, byte value)
  99. {
  100. Thread.Sleep(200);
  101. if (DiForce.Instance.ForceValues.ContainsKey(name))
  102. value = DiForce.Instance.ForceValues[name] ? (byte)1 : (byte)0;
  103. var item = FindDI(name);
  104. if (item != null)
  105. {
  106. _input.DI[item.Item2] = value;
  107. }
  108. }
  109. public void SetDI(string diName, string doName)
  110. {
  111. var item = FindDO(doName);
  112. if (item == null)
  113. return;
  114. SetDI(diName, _output.DO[item.Item2]);
  115. }
  116. public void SetAI(string aiName, float value)
  117. {
  118. if (AiForce.Instance.ForceValues.ContainsKey(aiName))
  119. value = AiForce.Instance.ForceValues[aiName];
  120. var item = FindAI(aiName);
  121. if (item != null)
  122. {
  123. _input.AI[item.Item2] = value;
  124. }
  125. }
  126. public float GetAI(string aiName)
  127. {
  128. var item = FindAI(aiName);
  129. if (item != null)
  130. {
  131. return _input.AI[item.Item2];
  132. }
  133. return 0;
  134. }
  135. public float GetAO(string aoName)
  136. {
  137. var item = FindAO(aoName);
  138. if (item != null)
  139. {
  140. return _output.AO[item.Item2];
  141. }
  142. return 0;
  143. }
  144. public byte GetDI(string diName)
  145. {
  146. var item = FindDI(diName);
  147. if (item != null)
  148. {
  149. return _input.DI[item.Item2];
  150. }
  151. return 0;
  152. }
  153. public byte GetDO(string doName)
  154. {
  155. var item = FindDO(doName);
  156. if (item != null)
  157. {
  158. return _output.DO[item.Item2];
  159. }
  160. return 0;
  161. }
  162. public void UpdateAI(float[] values)
  163. {
  164. }
  165. //仿真不输出output
  166. public void UpdateDO(bool[] values)
  167. {
  168. }
  169. //仿真不输出output
  170. public void UpdateAO(float[] values)
  171. {
  172. }
  173. //写入的IO,INDEX做了映射,需要映射回去
  174. public bool Write(PLC_OUTPUT_DATA data)
  175. {
  176. Array.Copy(data.DO, _output.DO, data.DO.Length);
  177. Array.Copy(data.AO, _output.AO, data.AO.Length);
  178. return true;
  179. }
  180. }
  181. }