革博士程序V1仓库
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

284 wiersze
10 KiB

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