版博士V2.0程序
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

IOCard.cs 6.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing.Drawing2D;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Timers;
  11. using Advantech.Motion;
  12. using Automation.BDaq;
  13. using OpenCvSharp;
  14. namespace ProductionControl.Device
  15. {
  16. public class IOCard : IDisposable
  17. {
  18. private Automation.BDaq.InstantDiCtrl instantDiCtrl1;
  19. private Automation.BDaq.InstantDoCtrl instantDoCtrl;
  20. public string DeviceNum { get; private set; }
  21. /// <summary>
  22. /// 读取输入端口数量
  23. /// </summary>
  24. public int DIPortCount { get; private set; }
  25. /// <summary>
  26. /// 写入输出端口数量
  27. /// </summary>
  28. public int DOPortCount { get; private set; }
  29. /// <summary>
  30. /// 读取到的输入值
  31. /// </summary>
  32. public byte[] DIData { get; private set; }
  33. public byte[] DOData { get; private set; }
  34. /// <summary>
  35. /// Log输出,(0-info,1-warning 2-error)
  36. /// </summary>
  37. public Action<int, string> log;
  38. public Action<int, byte> INEvent;
  39. public Action<int, byte> OUTEvent;
  40. /// <summary>
  41. /// 是否打开设备成功
  42. /// </summary>
  43. public bool IsInit { get; private set; } = false;
  44. private System.Timers.Timer timer = new System.Timers.Timer();
  45. public IOCard()
  46. {
  47. }
  48. public void start(string deviceNum="")
  49. {
  50. instantDiCtrl1 = new Automation.BDaq.InstantDiCtrl();
  51. instantDoCtrl = new Automation.BDaq.InstantDoCtrl();
  52. if (deviceNum != "")
  53. {
  54. instantDiCtrl1.SelectedDevice = new Automation.BDaq.DeviceInformation(deviceNum);
  55. instantDoCtrl.SelectedDevice = new DeviceInformation(deviceNum);
  56. }
  57. //根据加载的文件设置设备的所有配置
  58. string cfgPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\DevCfg\\IOCard_1703.xml";
  59. if (File.Exists(cfgPath))
  60. {
  61. var result = instantDiCtrl1.LoadProfile(cfgPath);//Loads a profile to initialize the device.
  62. var result2 = instantDoCtrl.LoadProfile(cfgPath);//Loads a profile to initialize the device.
  63. if (BioFailed(result) && BioFailed(result2))
  64. throw new Exception("CardIO Load Config Failed With Error Code: "+ result.ToString() + "]");
  65. }
  66. DeviceNum = instantDiCtrl1.SelectedDevice.Description;
  67. DIPortCount = instantDiCtrl1.Features.PortCount;
  68. DIData = new byte[DIPortCount];
  69. DOPortCount = instantDoCtrl.Features.PortCount;
  70. DOData = new byte[DOPortCount];
  71. loadDOData();
  72. IsInit = true;
  73. timer.Elapsed += Timer_Elapsed;
  74. timer.Interval = 100;
  75. timer.Enabled = true;
  76. }
  77. public void stop()
  78. {
  79. if (!IsInit) return;
  80. IsInit = false;
  81. timer.Elapsed -= Timer_Elapsed;
  82. //instantDiCtrl1.Cleanup();
  83. instantDiCtrl1.Dispose();
  84. //instantDoCtrl.Cleanup();
  85. instantDoCtrl.Dispose();
  86. }
  87. private void loadDOData(int portIndex=-1)
  88. {
  89. for (int i = 0; i < DOPortCount; ++i)
  90. {
  91. if (i == portIndex || portIndex < 0)
  92. {
  93. byte data;
  94. var reuslt = instantDoCtrl.Read(i, out data);
  95. if (reuslt != Automation.BDaq.ErrorCode.Success)
  96. {
  97. throw new Exception("加载CardIO输出状态失败!");
  98. }
  99. if (DOData[i] != data)
  100. {
  101. DOData[i] = data;
  102. OUTEvent?.Invoke(i, DOData[i]);
  103. }
  104. }
  105. }
  106. }
  107. public bool writeData(int startPort,byte[] data)
  108. {
  109. var reuslt = instantDoCtrl.Write(startPort, data.Length, data);
  110. //errorCode = instantDoCtrl.WriteBit(startPort, bit, dataForWriteBit);
  111. if (BioFailed(reuslt))
  112. return false;
  113. loadDOData(data.Length == 1 ? startPort : -1);
  114. return true;
  115. }
  116. public bool writeBitState(int portIndex, int bitIndex, bool state)
  117. {
  118. //byte bit = (byte)(DOData[portIndex] & (byte)(0x01<<bitIndex));
  119. //if (bit > 0 && state) return true;
  120. //if (bit == 0 && !state) return true;
  121. //byte data = DOData[portIndex];
  122. //data &= (byte)(~(0x1 << bitIndex));
  123. //data |= (byte)((state ? 1 : 0) << bitIndex);
  124. //var reuslt = instantDoCtrl.Write(startPort, len, data);
  125. var reuslt = instantDoCtrl.WriteBit(portIndex, bitIndex, (byte)((state ? 1 : 0) << bitIndex));
  126. if (BioFailed(reuslt))
  127. return false;
  128. loadDOData(portIndex);
  129. return true;
  130. }
  131. public bool getDIBitState(int portIndex, int bitIndex)
  132. {
  133. byte data = 0x01;
  134. data=(byte)(data << bitIndex);
  135. return (DIData[portIndex] & data) > 0;
  136. }
  137. public bool getDOBitState(int portIndex, int bitIndex)
  138. {
  139. byte data = 0x01;
  140. data = (byte)(data << bitIndex);
  141. return (DOData[portIndex] & data) > 0;
  142. }
  143. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  144. {
  145. if (!IsInit) return;
  146. //byte data = 0;//data is used to the API ReadBit.
  147. //int bit = 0;//bit is used to the API ReadBit.
  148. Automation.BDaq.ErrorCode result;
  149. int startPort = 0;
  150. byte[] datas = new byte[DIPortCount];
  151. do
  152. {
  153. result = instantDiCtrl1.Read(startPort, DIPortCount, datas);
  154. //errorCode = instantDiCtrl.ReadBit(startPort, bit, out data);
  155. if (BioFailed(result))
  156. {
  157. //throw new Exception();
  158. continue;
  159. }
  160. for (int i = 0; i < datas.Length; i++)
  161. {
  162. if (DIData[i] != datas[i])
  163. {
  164. DIData[i]= datas[i];
  165. INEvent?.Invoke(i, DIData[i]);
  166. }
  167. }
  168. Thread.Sleep(100);
  169. } while (IsInit);
  170. }
  171. private static bool BioFailed(Automation.BDaq.ErrorCode err)
  172. {
  173. return err < Automation.BDaq.ErrorCode.Success && err >= Automation.BDaq.ErrorCode.ErrorHandleNotValid;
  174. }
  175. public void Dispose()
  176. {
  177. stop();
  178. }
  179. }
  180. }