AdvantecIoCard.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.RT.Core.IoProviders;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Xml;
  9. using Automation.BDaq;
  10. namespace EfemRT.Devices
  11. {
  12. public class AdvantecIoCard : IoProvider
  13. {
  14. private DICard _diCard;
  15. private DOCard _doCard;
  16. private IOCard _ioCard;
  17. private string _type;
  18. protected override void SetParameter(XmlElement nodeParameter)
  19. {
  20. string name = nodeParameter.GetAttribute("name");
  21. int deviceId = int.Parse(nodeParameter.GetAttribute("device_id"));
  22. _type = nodeParameter.GetAttribute("type");
  23. string description = nodeParameter.GetAttribute("description");
  24. int port = int.Parse(nodeParameter.GetAttribute("port"));
  25. if (_type == "di")
  26. {
  27. _diCard = new DICard(name, deviceId);
  28. _diCard.Open();
  29. }
  30. else if (_type == "do")
  31. {
  32. _doCard = new DOCard(name, deviceId);
  33. _doCard.Open();
  34. }
  35. else if (_type == "dio")
  36. {
  37. //_ioCard = new IOCard(deviceId);
  38. _ioCard = new IOCard(description,port);
  39. }
  40. }
  41. protected override bool OnTimer()
  42. {
  43. try
  44. {
  45. foreach (var bufferSection in _blockSections)
  46. {
  47. if (bufferSection.Type == IoType.DI)
  48. {
  49. bool[] diBuffer = ReadDi(bufferSection.Offset, bufferSection.Size);
  50. if (diBuffer != null)
  51. {
  52. _buffer.SetDiBuffer(_source, bufferSection.Offset, diBuffer);
  53. //TraceArray(diBuffer);
  54. }
  55. }
  56. }
  57. Dictionary<int, bool[]> dos = _buffer.GetDoBuffer(_source);
  58. if (dos != null)
  59. {
  60. foreach (var doo in dos)
  61. {
  62. WriteDo(doo.Key, doo.Value);
  63. }
  64. }
  65. }
  66. catch (Exception ex)
  67. {
  68. LOG.Write(ex);
  69. Close();
  70. }
  71. return true;
  72. }
  73. protected override void Open()
  74. {
  75. }
  76. protected override void Close()
  77. {
  78. }
  79. protected override bool[] ReadDi(int offset, int size)
  80. {
  81. if (_type == "dio")
  82. {
  83. if (_ioCard == null)
  84. return null;
  85. if (_ioCard.Read(out byte[] buffer))
  86. {
  87. bool[] result1 = new bool[size];
  88. for (int i = 0; i < size && i < buffer.Length; i++)
  89. {
  90. result1[i] = buffer[i] == 1;
  91. }
  92. return result1;
  93. }
  94. return null;
  95. }
  96. if (_type != "di" || _diCard == null)
  97. return null;
  98. byte[] data = _diCard.DI;
  99. if (data == null || data.Length == 0)
  100. return null;
  101. bool[] result = new bool[size];
  102. for (int i = 0; i < size && i < data.Length; i++)
  103. {
  104. result[i] = data[i] == 1;
  105. }
  106. return result;
  107. }
  108. protected override short[] ReadAi(int offset, int size)
  109. {
  110. return null;
  111. }
  112. protected override void WriteDo(int offset, bool[] buffer)
  113. {
  114. if (_type == "dio")
  115. {
  116. if (_ioCard == null)
  117. return;
  118. if (buffer == null || buffer.Length == 0)
  119. return;
  120. _ioCard.Write(Array.ConvertAll(buffer, x => x ? (byte)1 : (byte)0));
  121. return;
  122. }
  123. if (_type != "do" || _doCard == null)
  124. return;
  125. if (buffer == null || buffer.Length == 0)
  126. return;
  127. _doCard.Write(Array.ConvertAll(buffer, x => x ? (byte)1 : (byte)0));
  128. }
  129. protected override void WriteAo(int offset, short[] buffer)
  130. {
  131. }
  132. }
  133. public class DICard
  134. {
  135. public string Name { get; set; }
  136. private int _portCount;
  137. public byte[] DI { get { return _buff; } }
  138. private byte[] _buff = null;
  139. private InstantDiCtrl _ctrl = null;
  140. private int _deviceNumber = -1;
  141. private R_TRIG _trigError = new R_TRIG();
  142. public DICard(string name, int deviceID)
  143. {
  144. Name = name;
  145. _deviceNumber = deviceID;
  146. _ctrl = new InstantDiCtrl();
  147. _ctrl.SelectedDevice = new DeviceInformation(deviceID);
  148. }
  149. public bool Open()
  150. {
  151. if (!_ctrl.Initialized)
  152. {
  153. LOG.Write($"{Name} : {_deviceNumber} initialize failed");
  154. return false;
  155. }
  156. _portCount = _ctrl.PortCount;
  157. _buff = new Byte[_portCount * 8];
  158. return true;
  159. }
  160. public bool Read()
  161. {
  162. if (!_ctrl.Initialized)
  163. {
  164. return false;
  165. }
  166. ErrorCode err = ErrorCode.Success;
  167. for (int i = 0; i < _portCount; i++)
  168. {
  169. for (int j = 0; j < 8; j++)
  170. {
  171. err = _ctrl.ReadBit(i, j, out byte data);
  172. _buff[i * 8 + j] = data;
  173. _trigError.CLK = err != ErrorCode.Success;
  174. if (_trigError.Q)
  175. {
  176. LOG.Write($"{Name} : {_deviceNumber} read error: {err}");
  177. break;
  178. }
  179. }
  180. }
  181. if (_trigError.M)
  182. {
  183. return false;
  184. }
  185. return true;
  186. }
  187. public bool Write(byte[] buffer)
  188. {
  189. return true;
  190. }
  191. public bool Reset()
  192. {
  193. return true;
  194. }
  195. public bool Close()
  196. {
  197. return true;
  198. }
  199. }
  200. public class DOCard
  201. {
  202. public string Name { get; set; }
  203. private int _portCount;
  204. private byte[] _buff = null;
  205. R_TRIG _trigError = new R_TRIG();
  206. private InstantDoCtrl _ctrl = null;
  207. public DOCard(string name, int deviceID)
  208. {
  209. Name = name;
  210. _ctrl = new InstantDoCtrl();
  211. _ctrl.SelectedDevice = new DeviceInformation(deviceID);
  212. }
  213. public bool Open()
  214. {
  215. if (!_ctrl.Initialized)
  216. {
  217. LOG.Write("Open DO card failed.");
  218. return false;
  219. }
  220. _portCount = _ctrl.PortCount;
  221. _buff = new Byte[_portCount * 8];
  222. return true;
  223. }
  224. public bool Read()
  225. {
  226. return true;
  227. }
  228. public bool Write(byte[] buffer)
  229. {
  230. if (!_ctrl.Initialized)
  231. {
  232. return false;
  233. }
  234. for (int i = 0; i < _portCount * 8 && i < buffer.Length; i++)
  235. {
  236. _buff[i] = buffer[i];
  237. }
  238. ErrorCode err = _ctrl.Write(0, _portCount, _buff);
  239. _trigError.CLK = err != ErrorCode.Success;
  240. if (_trigError.Q)
  241. {
  242. LOG.Write("Write DO failed.");
  243. }
  244. if (err != ErrorCode.Success)
  245. {
  246. return false;
  247. }
  248. return true;
  249. }
  250. public bool Reset()
  251. {
  252. return true;
  253. }
  254. public bool Close()
  255. {
  256. return true;
  257. }
  258. }
  259. public enum IOStatus : byte
  260. {
  261. 关闭 = 0,
  262. 打开 = 1
  263. };
  264. public class IOCard
  265. {
  266. /// <summary>
  267. /// 构造函数
  268. /// </summary>
  269. /// <param name="deviceNum"></param>
  270. // public IOCard(int deviceNum = 0)
  271. public IOCard(string deviceNum,int port)
  272. {
  273. try
  274. {
  275. _diCtrl.SelectedDevice = new DeviceInformation(deviceNum);
  276. _doCtrl.SelectedDevice = new DeviceInformation(deviceNum);
  277. init = true;
  278. LOG.Write("IO Card selected");
  279. }
  280. catch (Exception e)
  281. {
  282. //string str1=e.ToString();
  283. init = false;
  284. //MessageBox.Show("None Find IOCard" + deviceNum.ToString() + " !", " IOCard", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  285. }
  286. cardName = _diCtrl.SelectedDevice.Description;
  287. _portCount = port;
  288. _buff = new Byte[_portCount * 8];
  289. }
  290. public string Name { get; set; }
  291. private int _portCount;
  292. private byte[] _buff = null;
  293. R_TRIG _trigError = new R_TRIG();
  294. #region Global
  295. /// <summary>
  296. /// 输出
  297. /// </summary>
  298. private InstantDoCtrl _doCtrl = new InstantDoCtrl();
  299. /// <summary>
  300. /// 输入
  301. /// </summary>
  302. private InstantDiCtrl _diCtrl = new InstantDiCtrl();
  303. /// <summary>
  304. /// 是否初始化
  305. /// </summary>
  306. private bool init = false;
  307. /// <summary>
  308. /// 初始化
  309. /// </summary>
  310. public bool Init
  311. {
  312. get
  313. {
  314. return init;
  315. }
  316. }
  317. /// <summary>
  318. /// 设备名(Default:N/A)
  319. /// </summary>
  320. private string cardName = "N/A";
  321. /// <summary>
  322. /// 设备名(Default:N/A)
  323. /// </summary>
  324. public string CardName
  325. {
  326. get
  327. {
  328. return cardName;
  329. }
  330. }
  331. #endregion
  332. public bool Write(byte[] buffer)
  333. {
  334. if (!_doCtrl.Initialized)
  335. {
  336. return false;
  337. }
  338. ErrorCode err = ErrorCode.Success;
  339. for (int i = 0; i < _portCount; i++)
  340. {
  341. for (int j = 0; j < 8; j++)
  342. {
  343. if (i * 8 + j < buffer.Length)
  344. err = _doCtrl.WriteBit(i, j, buffer[i * 8 + j]);
  345. _trigError.CLK = err != ErrorCode.Success;
  346. if (_trigError.Q)
  347. {
  348. LOG.Write($"{Name} : write error: {err}");
  349. break;
  350. }
  351. }
  352. }
  353. _trigError.CLK = err != ErrorCode.Success;
  354. if (_trigError.Q)
  355. {
  356. LOG.Write("Write DO failed.");
  357. }
  358. if (err != ErrorCode.Success)
  359. {
  360. return false;
  361. }
  362. return true;
  363. }
  364. public bool Read(out byte[] buffer)
  365. {
  366. if (!_diCtrl.Initialized)
  367. {
  368. LOG.Write($"{Name} : not initialized");
  369. buffer = null;
  370. return false;
  371. }
  372. ErrorCode err = ErrorCode.Success;
  373. for (int i = 0; i < _portCount; i++)
  374. {
  375. for (int j = 0; j < 8; j++)
  376. {
  377. err = _diCtrl.ReadBit(i, j, out byte data);
  378. _buff[i * 8 + j] = data;
  379. _trigError.CLK = err != ErrorCode.Success;
  380. if (_trigError.Q)
  381. {
  382. LOG.Write($"{Name} : read error: {err}");
  383. break;
  384. }
  385. }
  386. }
  387. if (_trigError.M)
  388. {
  389. buffer = null;
  390. return false;
  391. }
  392. buffer = _buff;
  393. return true;
  394. }
  395. /// <summary>
  396. /// 销毁
  397. /// </summary>
  398. public void Disp()
  399. {
  400. _doCtrl.Cleanup();
  401. _diCtrl.Cleanup();
  402. _doCtrl.Dispose();
  403. _diCtrl.Dispose();
  404. }
  405. }
  406. }