版博士V2.0程序
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

290 rindas
10 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 ProductionControl.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. private bool compareIOHeightDev()
  129. {
  130. if (!Config.CMDProcess.ContainsKey(CMDName.厚度气缸与轴运动告警))
  131. return false;
  132. JObject joJson = Config.CMDProcess[CMDName.厚度气缸与轴运动告警];
  133. var outDatas = joJson.Value<JArray>("OUT_OP_SHOW");
  134. var data=Utils.Util.IOFormatBinary2bytes(outDatas.ToObject<List<string>>().ToArray());
  135. for (int i = 0; i < data.Length; i++)
  136. {
  137. if (data[i] > 0 && (data[i] & DOData[i]) == data[i])//这里只找了一个
  138. {
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. /// <summary>
  145. /// 复位,全置0
  146. /// </summary>
  147. /// <returns></returns>
  148. public bool reset()
  149. {
  150. strobeList.Clear();//闪烁状态清空
  151. byte[] data=new byte[DOPortCount];
  152. for(int i = 0;i < data.Length; ++i)
  153. data[i]=0;
  154. var reuslt = instantDoCtrl.Write(0, data.Length, data);
  155. if (BioFailed(reuslt))
  156. return false;
  157. loadDOData();
  158. return true;
  159. }
  160. /// <summary>
  161. /// 闪烁状态会清空
  162. /// </summary>
  163. /// <param name="startPort"></param>
  164. /// <param name="data"></param>
  165. /// <returns></returns>
  166. public bool writeData(int startPort, byte[] data)
  167. {
  168. strobeList.Clear();//闪烁状态清空
  169. var reuslt = instantDoCtrl.Write(startPort, data.Length, data);
  170. if (BioFailed(reuslt))
  171. return false;
  172. loadDOData(data.Length == 1 ? startPort : -1);
  173. return true;
  174. }
  175. /// <summary>
  176. ///
  177. /// </summary>
  178. /// <param name="portIndex"></param>
  179. /// <param name="bitIndex"></param>
  180. /// <param name="state"></param>
  181. /// <param name="isStrobe">频闪</param>
  182. /// <returns></returns>
  183. public bool writeBitState(int portIndex, int bitIndex, bool state, bool isStrobe = false)
  184. {
  185. //byte bit = (byte)(DOData[portIndex] & (byte)(0x01<<bitIndex));
  186. //if (bit > 0 && state) return true;
  187. //if (bit == 0 && !state) return true;
  188. //byte data = DOData[portIndex];
  189. //data &= (byte)(~(0x1 << bitIndex));
  190. //data |= (byte)((state ? 1 : 0) << bitIndex);
  191. //var reuslt = instantDoCtrl.Write(startPort, len, data);
  192. var reuslt = instantDoCtrl.WriteBit(portIndex, bitIndex, (byte)(state ? 1 : 0));// (byte)((state ? 1 : 0) << bitIndex));
  193. if (BioFailed(reuslt))
  194. return false;
  195. loadDOData(portIndex);
  196. string key = portIndex + "|" + bitIndex;
  197. if (state && isStrobe)
  198. strobeList.Add(key);
  199. else if(strobeList.Contains(key))
  200. strobeList.Remove(key);
  201. return true;
  202. }
  203. public bool getDIBitState(int portIndex, int bitIndex)
  204. {
  205. byte data = 0x01;
  206. data=(byte)(data << bitIndex);
  207. return (DIData[portIndex] & data) > 0;
  208. }
  209. public bool getDOBitState(int portIndex, int bitIndex)
  210. {
  211. byte data = 0x01;
  212. data = (byte)(data << bitIndex);
  213. return (DOData[portIndex] & data) > 0;
  214. }
  215. private bool strobeState = true;
  216. private void timerStrobe_Elapsed(object sender, ElapsedEventArgs e)
  217. {
  218. if (!IsInit) return;
  219. string[] key;
  220. for(int i = 0;i< strobeList.Count; i++)
  221. {
  222. key=strobeList[i].Split('|');
  223. instantDoCtrl.WriteBit(Convert.ToInt16(key[0]), Convert.ToInt16(key[1]), (byte)(strobeState ? 1 : 0));
  224. }
  225. strobeState =!strobeState;
  226. }
  227. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  228. {
  229. if (!IsInit) return;
  230. timer.Stop();
  231. //byte data = 0;//data is used to the API ReadBit.
  232. //int bit = 0;//bit is used to the API ReadBit.
  233. Automation.BDaq.ErrorCode result;
  234. int startPort = 0;
  235. byte[] datas = new byte[DIPortCount];
  236. do
  237. {
  238. result = instantDiCtrl1.Read(startPort, DIPortCount, datas);
  239. //errorCode = instantDiCtrl.ReadBit(startPort, bit, out data);
  240. if (BioFailed(result))
  241. {
  242. //throw new Exception();
  243. continue;
  244. }
  245. for (int i = 0; i < datas.Length; i++)
  246. {
  247. if (DIData[i] != datas[i])
  248. {
  249. DIData[i]= datas[i];
  250. INEvent?.Invoke(i, DIData[i]);
  251. }
  252. }
  253. Thread.Sleep(10);
  254. } while (IsInit);
  255. }
  256. private static bool BioFailed(Automation.BDaq.ErrorCode err)
  257. {
  258. return err < Automation.BDaq.ErrorCode.Success && err >= Automation.BDaq.ErrorCode.ErrorHandleNotValid;
  259. }
  260. public void Dispose()
  261. {
  262. stop();
  263. }
  264. }
  265. }