版博士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.
 
 
 
 

191 lines
8.9 KiB

  1. using Newtonsoft.Json.Linq;
  2. using ProductionControl.Device;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using static ProductionControl.Device.AxisDev;
  10. namespace ProductionControl
  11. {
  12. public class DevContainer
  13. {
  14. public AxisDev devAxis;//=new AxisDev();
  15. public CodeScannerDev devCodeScanner;//= new CodeScannerDev();
  16. public HeightDev devHeight;//=new HeightDev();
  17. public IOCardDev devIOCard;//=new IOCardDev();
  18. public LightDev devLight;//=new LightDev();
  19. public ScannerDev devScannerGentl;// = new ScannerDev( ScannerDev.ScannerType.GENTL);
  20. public ScannerDev devScannerCC;// = new ScannerDev(ScannerDev.ScannerType.CC);
  21. public SmallAxisDev devSmallAxis;// = new SmallAxisDev();
  22. public TensionDev devTension;//=new TensionDev();
  23. public DefectLib libDefect;//=new DefectLib();
  24. public SizeLib libSize;
  25. public ForLib libFor;
  26. public IFLib libIF;
  27. public Action<WarningEnum, string> WarningEvent;
  28. public Action<bool, string> StateChange;
  29. public Action<string, string> OutDebugEvent;
  30. public Action<int, AxisPosType, double> axisPosEvent;
  31. public bool state = false;
  32. private Thread t;
  33. private IntPtr preView1, preView2;
  34. public void start(IntPtr view1, IntPtr view2)
  35. {
  36. this.preView1 = view1;
  37. this.preView2 = view2;
  38. devCodeScanner = new CodeScannerDev();
  39. devCodeScanner.WarningEvent = WarningEvent;
  40. if (!devCodeScanner.start()) throw new Exception("扫码枪初始化失败!");
  41. t = new System.Threading.Thread(run);
  42. t.IsBackground = true;
  43. t.Start();
  44. }
  45. public void stop()
  46. {
  47. try
  48. {
  49. state = false;
  50. try { devAxis.stop(); } catch { }
  51. try { devHeight.stop(); } catch { }
  52. try { devIOCard.stop(); } catch { }
  53. try { devLight.stop(); } catch { }
  54. try { devScannerGentl.stop(); devScannerGentl.close(); } catch { }
  55. try { devScannerCC.stop(); devScannerCC.close(); } catch { }
  56. try { devSmallAxis.stop(); } catch { }
  57. try { devTension.stop(); } catch { }
  58. try { libDefect.stop(); } catch { }
  59. try { libSize.stop(); } catch { }
  60. try { devCodeScanner.stop(); } catch { }
  61. }
  62. catch { }
  63. }
  64. private void run()
  65. {
  66. try
  67. {
  68. devAxis = new AxisDev();
  69. devAxis.WarningEvent = WarningEvent;
  70. devAxis.axisPosEvent = axisPosEvent;//位置POS
  71. devHeight = new HeightDev();
  72. devHeight.WarningEvent = WarningEvent;
  73. devIOCard = new IOCardDev();
  74. devIOCard.WarningEvent = WarningEvent;
  75. devIOCard.OUTEvent = (index, data) =>
  76. {
  77. if (Config.HeightDevIOState && devAxis.IsInit && (devAxis.isMoveing(0) || devAxis.isMoveing(2)))
  78. {
  79. WarningEvent?.Invoke(WarningEnum.High, "厚度IO已打开,急停,请检查设备,防止损坏!");
  80. //devAxis.stopNow();
  81. }
  82. };
  83. devLight = new LightDev(Config.Light_Name);
  84. devLight.WarningEvent = WarningEvent;
  85. devScannerGentl = new ScannerDev(ScannerDev.ScannerType.GENTL);
  86. devScannerGentl.WarningEvent = WarningEvent;
  87. devScannerCC = new ScannerDev(ScannerDev.ScannerType.CC);
  88. devScannerCC.WarningEvent = WarningEvent;
  89. devSmallAxis = new SmallAxisDev();
  90. devSmallAxis.WarningEvent = WarningEvent;
  91. devTension = new TensionDev();
  92. devTension.WarningEvent = WarningEvent;
  93. libDefect = new DefectLib();
  94. libDefect.WarningEvent = WarningEvent;
  95. libSize = new SizeLib();
  96. libSize.WarningEvent = WarningEvent;
  97. libFor = new ForLib();
  98. libIF = new IFLib();
  99. //启动
  100. if (!devAxis.start(Config.Axis_PulseOutMode)) throw new Exception("运动板卡初始化失败!");
  101. //if (!devCodeScanner.start()) throw new Exception("扫码枪初始化失败!");
  102. if (!Config.SkipHeight && !devHeight.start(Config.HeightDev_IP, Config.HeightDev_Port)) throw new Exception("厚度测量设备初始化失败!");
  103. if (!devIOCard.start(Config.IOCard_DeviceNum)) throw new Exception("I/O板卡初始化失败!");
  104. if (!Config.SkipLight && !devLight.start(Config.Light_PortNum)) throw new Exception("光源设备初始化失败!");
  105. if (!Config.SkipScannerGL && !devScannerGentl.open()) throw new Exception("相机初始化失败!");
  106. if (!Config.SkipScannerGL && !devScannerGentl.start(this.preView1, null)) throw new Exception("相机打开失败!");
  107. if (!Config.SkipScannerCC && !devScannerCC.open()) throw new Exception("相机初始化失败!");
  108. if (!Config.SkipScannerCC && !devScannerCC.start(this.preView2, Config.appBasePath+"\\temp")) throw new Exception("相机打开失败!");
  109. if (!Config.SkipSmallAxis && !devSmallAxis.start(Config.SmallAxis_ComName, 9600)) throw new Exception("镜头电机初始化失败!");
  110. if (!Config.SkipTension && !devTension.start(Config.Tension_PortNum)) throw new Exception("张力检测设备初始化失败!");
  111. if (!libDefect.start()) throw new Exception("缺陷库初始化失败!");
  112. if (!libSize.start(Config.SizeEnginePath)) throw new Exception("尺寸库初始化失败!");
  113. //
  114. state = true;
  115. StateChange?.Invoke(true, "成功");
  116. }
  117. catch (Exception ex)
  118. {
  119. stop();
  120. StateChange?.Invoke(false, ex.Message);
  121. }
  122. }
  123. /// <summary>
  124. /// I/O指令输出
  125. /// </summary>
  126. /// <param name="key">CMDName</param>
  127. /// <param name="isStrobe">频闪</param>
  128. /// <param name="recover">输出sleep下,后恢复原信号</param>
  129. /// <param name="recoverWaitTime">sleep多久后反转</param>
  130. /// <returns></returns>
  131. public bool io_output(CMDName key, bool isStrobe = false, bool recover = false, int recoverWaitTime = 100)
  132. {
  133. if (Config.CMDProcess.ContainsKey(key))
  134. return io_output(key.ToString(),Config.CMDProcess[key], isStrobe, recover, recoverWaitTime);
  135. return false;
  136. }
  137. /// <summary>
  138. /// I/O指令输出
  139. /// </summary>
  140. /// <param name="processParam"></param>
  141. /// <param name="isStrobe">频闪</param>
  142. /// <param name="recover">输出sleep(recoverWaitTime)下,后恢复原信号</param>
  143. /// <param name="recoverWaitTime">sleep 后反转</param>
  144. public bool io_output(string tagName,JObject processParam, bool isStrobe = false, bool recover = false, int recoverWaitTime = 100)
  145. {
  146. bool result = false;
  147. string[] OUT_OP_SHOW = processParam.Value<JArray>("OUT_OP_SHOW").ToObject<List<string>>().ToArray();
  148. OUT_OP_SHOW = Utils.Util.IODataFormatBinaryStr(OUT_OP_SHOW, true);
  149. for (int i = 0; i < OUT_OP_SHOW.Length; i++)
  150. {
  151. for (int j = 0; j < OUT_OP_SHOW[i].Length; j++)
  152. {
  153. int jj = OUT_OP_SHOW[i].Length - j - 1;
  154. if (OUT_OP_SHOW[i][jj] == 'L' || OUT_OP_SHOW[i][jj] == 'H')
  155. {
  156. if (recover)
  157. {
  158. if (recoverWaitTime > 0)
  159. {
  160. OutDebugEvent?.Invoke(tagName, $"向I/O输出引脚{i}-{j},信号{OUT_OP_SHOW[i][jj]},输出时长:{recoverWaitTime}ms");
  161. devIOCard.writeBitState(i, j, OUT_OP_SHOW[i][jj] == 'H', isStrobe);
  162. Thread.Sleep(recoverWaitTime);
  163. }
  164. OutDebugEvent?.Invoke(tagName, $"向I/O输出引脚{i}-{j},信号{OUT_OP_SHOW[i][jj]=='L'}");
  165. devIOCard.writeBitState(i, j, OUT_OP_SHOW[i][jj] == 'L');
  166. }
  167. else
  168. {
  169. OutDebugEvent?.Invoke(tagName, $"向I/O输出引脚{i}-{j},信号{OUT_OP_SHOW[i][jj]}");
  170. devIOCard.writeBitState(i, j, OUT_OP_SHOW[i][jj] == 'H', isStrobe);
  171. }
  172. result = true;
  173. }
  174. }
  175. }
  176. return result;
  177. }
  178. }
  179. }