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

165 lines
7.0 KiB

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