版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

273 lines
9.4 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Drawing.Drawing2D;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Security.Policy;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Timers;
  13. using Advantech.Motion;
  14. using Automation.BDaq;
  15. using Newtonsoft.Json.Linq;
  16. using OpenCvSharp;
  17. namespace AssistClient.Device
  18. {
  19. public class IOCardDev : IDisposable
  20. {
  21. private Automation.BDaq.InstantDiCtrl instantDiCtrl1;
  22. private Automation.BDaq.InstantDoCtrl instantDoCtrl;
  23. public string DeviceNum { get; private set; }
  24. /// <summary>
  25. /// 读取输入端口数量
  26. /// </summary>
  27. public int DIPortCount { get; private set; }
  28. /// <summary>
  29. /// 写入输出端口数量
  30. /// </summary>
  31. public int DOPortCount { get; private set; }
  32. /// <summary>
  33. /// 读取到的输入值
  34. /// </summary>
  35. public byte[] DIData { get; private set; }
  36. public byte[] DOData { get; private set; }
  37. /// <summary>
  38. /// Log输出
  39. /// </summary>
  40. public Action<WarningEnum, string> WarningEvent;
  41. public Action<int, byte> INEvent;
  42. public Action<int, byte> OUTEvent;
  43. /// <summary>
  44. /// 是否打开设备成功
  45. /// </summary>
  46. public bool IsInit { get; private set; } = false;
  47. private System.Timers.Timer timer = new System.Timers.Timer();
  48. //频闪使用
  49. private System.Timers.Timer timerStrobe = new System.Timers.Timer();
  50. private List<string> strobeList = new List<string>();
  51. public IOCardDev()
  52. {
  53. }
  54. public bool start(string deviceNum="")
  55. {
  56. try
  57. {
  58. instantDiCtrl1 = new Automation.BDaq.InstantDiCtrl();
  59. instantDoCtrl = new Automation.BDaq.InstantDoCtrl();
  60. strobeList=new List<string>();
  61. if (deviceNum != "")
  62. {
  63. instantDiCtrl1.SelectedDevice = new Automation.BDaq.DeviceInformation(deviceNum);
  64. instantDoCtrl.SelectedDevice = new DeviceInformation(deviceNum);
  65. }
  66. //根据加载的文件设置设备的所有配置
  67. string cfgPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\DevCfg\\IOCard_1703.xml";
  68. if (File.Exists(cfgPath))
  69. {
  70. var result = instantDiCtrl1.LoadProfile(cfgPath);//Loads a profile to initialize the device.
  71. var result2 = instantDoCtrl.LoadProfile(cfgPath);//Loads a profile to initialize the device.
  72. if (BioFailed(result) && BioFailed(result2))
  73. throw new Exception("CardIO Load Config Failed With Error Code: [" + result.ToString() + "]");
  74. }
  75. DeviceNum = instantDiCtrl1.SelectedDevice.Description;
  76. DIPortCount = instantDiCtrl1.Features.PortCount;
  77. DIData = new byte[DIPortCount];
  78. DOPortCount = instantDoCtrl.Features.PortCount;
  79. DOData = new byte[DOPortCount];
  80. loadDOData();
  81. IsInit = true;
  82. timer.Elapsed += Timer_Elapsed;
  83. timer.Interval = 100;
  84. timer.Start();
  85. timerStrobe.Elapsed += timerStrobe_Elapsed;
  86. timerStrobe.Interval = 500;
  87. timerStrobe.Start();
  88. return true;
  89. }
  90. catch (Exception ex)
  91. {
  92. WarningEvent?.Invoke(WarningEnum.High, ex.Message);
  93. return false;
  94. }
  95. }
  96. public void stop()
  97. {
  98. if (!IsInit) return;
  99. IsInit = false;
  100. strobeList.Clear();//闪烁状态清空
  101. timer.Stop();
  102. timerStrobe.Stop();
  103. //instantDiCtrl1.Cleanup();
  104. instantDiCtrl1.Dispose();
  105. //instantDoCtrl.Cleanup();
  106. instantDoCtrl.Dispose();
  107. }
  108. private void loadDOData(int portIndex=-1)
  109. {
  110. for (int i = 0; i < DOPortCount; ++i)
  111. {
  112. if (i == portIndex || portIndex < 0)
  113. {
  114. byte data;
  115. var reuslt = instantDoCtrl.Read(i, out data);
  116. if (reuslt != Automation.BDaq.ErrorCode.Success)
  117. throw new Exception("加载CardIO输出状态失败!");
  118. if (DOData[i] != data)
  119. {
  120. DOData[i] = data;
  121. //Config.HeightDevIOState = compareIOHeightDev();
  122. //WarningEvent(WarningEnum.Normal, "厚度气缸状态:" + Config.HeightDevIOState.ToString());
  123. OUTEvent?.Invoke(i, DOData[i]);
  124. }
  125. }
  126. }
  127. }
  128. /// <summary>
  129. /// 复位,全置0
  130. /// </summary>
  131. /// <returns></returns>
  132. public bool reset()
  133. {
  134. strobeList.Clear();//闪烁状态清空
  135. byte[] data=new byte[DOPortCount];
  136. for(int i = 0;i < data.Length; ++i)
  137. data[i]=0;
  138. var reuslt = instantDoCtrl.Write(0, data.Length, data);
  139. if (BioFailed(reuslt))
  140. return false;
  141. loadDOData();
  142. return true;
  143. }
  144. /// <summary>
  145. /// 闪烁状态会清空
  146. /// </summary>
  147. /// <param name="startPort"></param>
  148. /// <param name="data"></param>
  149. /// <returns></returns>
  150. public bool writeData(int startPort, byte[] data)
  151. {
  152. strobeList.Clear();//闪烁状态清空
  153. var reuslt = instantDoCtrl.Write(startPort, data.Length, data);
  154. if (BioFailed(reuslt))
  155. return false;
  156. loadDOData(data.Length == 1 ? startPort : -1);
  157. return true;
  158. }
  159. /// <summary>
  160. ///
  161. /// </summary>
  162. /// <param name="portIndex"></param>
  163. /// <param name="bitIndex"></param>
  164. /// <param name="state"></param>
  165. /// <param name="isStrobe">频闪</param>
  166. /// <returns></returns>
  167. public bool writeBitState(int portIndex, int bitIndex, bool state, bool isStrobe = false)
  168. {
  169. //byte bit = (byte)(DOData[portIndex] & (byte)(0x01<<bitIndex));
  170. //if (bit > 0 && state) return true;
  171. //if (bit == 0 && !state) return true;
  172. //byte data = DOData[portIndex];
  173. //data &= (byte)(~(0x1 << bitIndex));
  174. //data |= (byte)((state ? 1 : 0) << bitIndex);
  175. //var reuslt = instantDoCtrl.Write(startPort, len, data);
  176. var reuslt = instantDoCtrl.WriteBit(portIndex, bitIndex, (byte)(state ? 1 : 0));// (byte)((state ? 1 : 0) << bitIndex));
  177. if (BioFailed(reuslt))
  178. return false;
  179. loadDOData(portIndex);
  180. string key = portIndex + "|" + bitIndex;
  181. if (state && isStrobe)
  182. strobeList.Add(key);
  183. else if(strobeList.Contains(key))
  184. strobeList.Remove(key);
  185. return true;
  186. }
  187. public bool getDIBitState(int portIndex, int bitIndex)
  188. {
  189. byte data = 0x01;
  190. data=(byte)(data << bitIndex);
  191. return (DIData[portIndex] & data) > 0;
  192. }
  193. public bool getDOBitState(int portIndex, int bitIndex)
  194. {
  195. byte data = 0x01;
  196. data = (byte)(data << bitIndex);
  197. return (DOData[portIndex] & data) > 0;
  198. }
  199. private bool strobeState = true;
  200. private void timerStrobe_Elapsed(object sender, ElapsedEventArgs e)
  201. {
  202. if (!IsInit) return;
  203. string[] key;
  204. for(int i = 0;i< strobeList.Count; i++)
  205. {
  206. key=strobeList[i].Split('|');
  207. instantDoCtrl.WriteBit(Convert.ToInt16(key[0]), Convert.ToInt16(key[1]), (byte)(strobeState ? 1 : 0));
  208. }
  209. strobeState =!strobeState;
  210. }
  211. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  212. {
  213. if (!IsInit) return;
  214. timer.Stop();
  215. //byte data = 0;//data is used to the API ReadBit.
  216. //int bit = 0;//bit is used to the API ReadBit.
  217. Automation.BDaq.ErrorCode result;
  218. int startPort = 0;
  219. byte[] datas = new byte[DIPortCount];
  220. do
  221. {
  222. result = instantDiCtrl1.Read(startPort, DIPortCount, datas);
  223. //errorCode = instantDiCtrl.ReadBit(startPort, bit, out data);
  224. if (BioFailed(result))
  225. {
  226. //throw new Exception();
  227. continue;
  228. }
  229. for (int i = 0; i < datas.Length; i++)
  230. {
  231. if (DIData[i] != datas[i])
  232. {
  233. DIData[i]= datas[i];
  234. INEvent?.Invoke(i, DIData[i]);
  235. }
  236. }
  237. Thread.Sleep(10);
  238. } while (IsInit);
  239. }
  240. private static bool BioFailed(Automation.BDaq.ErrorCode err)
  241. {
  242. return err < Automation.BDaq.ErrorCode.Success && err >= Automation.BDaq.ErrorCode.ErrorHandleNotValid;
  243. }
  244. public void Dispose()
  245. {
  246. stop();
  247. }
  248. }
  249. }