革博士程序V1仓库
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

198 строки
8.9 KiB

  1. using LeatherApp.Device;
  2. using LeatherApp.Interface;
  3. using Newtonsoft.Json.Linq;
  4. using S7.Net;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace LeatherApp
  15. {
  16. public class DevContainer
  17. {
  18. public PLCDev devPlc;
  19. public PhotoLib libPhoto;
  20. public ScannerCodeDev devCodeScanner;//= new CodeScannerDev();
  21. public IOCardDev devIOCard;//=new IOCardDev();
  22. public LightDev devLight;//=new LightDev();
  23. public ABSCamerCardDev devCamer1;
  24. public ABSCamerCardDev devCamer2;
  25. public DefectLib libDefect;//=new DefectLib();
  26. public Action<DateTime,WarningEnum, string> WarningEvent;
  27. public Action<bool, string> StateChange;
  28. public Action<string, string> OutDebugEvent;
  29. public bool state = false;
  30. private Thread t;
  31. private PictureBox preView1, preView2;
  32. public void start(PictureBox view1, PictureBox view2)
  33. {
  34. this.preView1 = view1;
  35. this.preView2 = view2;
  36. //devCodeScanner = new ScannerCodeDev();
  37. //devCodeScanner.WarningEvent = WarningEvent;
  38. //if (!devCodeScanner.start()) throw new Exception("扫码器初始化失败!");
  39. t = new System.Threading.Thread(run);
  40. t.IsBackground = true;
  41. t.Start();
  42. }
  43. public void stop()
  44. {
  45. try
  46. {
  47. state = false;
  48. try { devIOCard.stop(); } catch { }
  49. try { devPlc.stop(); } catch { }
  50. try { devLight.stop(); } catch { }
  51. try { devCamer1.stop(); devCamer1.close(); } catch { }
  52. try { devCamer2.stop(); devCamer2.close(); } catch { }
  53. try { devCodeScanner.stop(); } catch { }
  54. try { libDefect.stop(); } catch { }
  55. try { libPhoto.stop(); } catch { }
  56. }
  57. catch { }
  58. }
  59. private void run()
  60. {
  61. try
  62. {
  63. WarningEvent?.BeginInvoke(DateTime.Now,WarningEnum.Normal, "初始化设备...",null,null);
  64. devCodeScanner = new ScannerCodeDev();
  65. devCodeScanner.WarningEvent = WarningEvent;
  66. if (!Config.StopCodeScanner)
  67. devCodeScanner.start();
  68. devPlc = new PLCDev();
  69. devPlc.WarningEvent = WarningEvent;
  70. libPhoto = new PhotoLib();
  71. libPhoto.WarningEvent = WarningEvent;
  72. libDefect = new DefectLib();
  73. libDefect.WarningEvent = WarningEvent;
  74. devIOCard = new IOCardDev();
  75. devIOCard.WarningEvent = WarningEvent;
  76. devLight = new LightDev(Config.Light_Name);
  77. devLight.WarningEvent = WarningEvent;
  78. if (Config.Camer_Name == CamerDevNameEnum.海康)
  79. {
  80. devCamer2 = new CamerCardDev();
  81. devCamer1 = new CamerCardDev();
  82. }
  83. else
  84. {
  85. devCamer1 = new CamerCardDevIK();
  86. devCamer2 = new CamerCardDevIK();
  87. }
  88. devCamer1.WarningEvent = WarningEvent;
  89. devCamer2.WarningEvent = WarningEvent;
  90. //启动
  91. string appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  92. WarningEvent?.Invoke(DateTime.Now,WarningEnum.Normal, "1");
  93. //WarningEvent?.Invoke(DateTime.Now,WarningEnum.Normal, "打开设备...");
  94. if (!Config.StopIO && !devIOCard.start(Config.IOCard_DeviceNum)) throw new Exception("I/O板卡初始化失败!");
  95. if (!Config.StopPLC && !devPlc.start(CpuType.S71200, Config.PlcIPAdrees, Config.PlcRackN, Config.PlcSolt)) throw new Exception("Plc连接失败!");
  96. if (!Config.StopLight && !devLight.start(int.Parse(Config.Light_PortName.Substring(3)))) throw new Exception("光源设备初始化失败!");
  97. if (!libPhoto.start()) throw new Exception("图像库初始化失败!");
  98. WarningEvent?.Invoke(DateTime.Now,WarningEnum.Normal, "2");
  99. if (libDefect == null)
  100. {
  101. WarningEvent?.Invoke(DateTime.Now, WarningEnum.Normal, "算法库为空,重新初始化");
  102. libDefect = new DefectLib();
  103. libDefect.WarningEvent = WarningEvent;
  104. }
  105. if (!libDefect.start()) throw new Exception("缺陷库初始化失败!");
  106. WarningEvent?.Invoke(DateTime.Now,WarningEnum.Normal, "3");
  107. if (!File.Exists(appBasePath+ "\\DevCfg\\" + Config.Carmer1ConfigFilePath) || !File.Exists(appBasePath + "\\DevCfg\\" + Config.Carmer2ConfigFilePath))
  108. throw new Exception($"相机配置文件不存在({Config.Carmer1ConfigFilePath}或{Config.Carmer2ConfigFilePath})!");
  109. string scanner1Path = appBasePath + "\\temp\\";
  110. if (!Directory.Exists(scanner1Path + "scanner\\")) Directory.CreateDirectory(scanner1Path + "scanner\\");
  111. if (!devCamer2.open(1, 0)) throw new Exception("相机初始化失败!");
  112. if (!devCamer2.start(this.preView1, scanner1Path + "scanner\\")) throw new Exception("相机1打开失败!");
  113. if (!devCamer1.open(0, 0)) throw new Exception("相机初始化失败!");
  114. if (!devCamer1.start(this.preView2, scanner1Path + "scanner\\")) throw new Exception("相机0打开失败!");
  115. //
  116. state = true;
  117. StateChange?.Invoke(true, "成功");
  118. }
  119. catch (Exception ex)
  120. {
  121. stop();
  122. StateChange?.Invoke(false, ex.Message);
  123. }
  124. }
  125. /// <summary>
  126. /// I/O指令输出
  127. /// </summary>
  128. /// <param name="key">CMDName</param>
  129. /// <param name="isStrobe">频闪</param>
  130. /// <param name="recover">输出sleep下,后恢复原信号</param>
  131. /// <param name="recoverWaitTime">sleep多久后反转</param>
  132. /// <returns></returns>
  133. public bool io_output(CMDName key, bool isStrobe = false, bool recover = false, int recoverWaitTime = 100)
  134. {
  135. if (Config.CMDProcess.ContainsKey(key))
  136. return io_output(key.ToString(), Config.CMDProcess[key], isStrobe, recover, recoverWaitTime);
  137. return false;
  138. }
  139. /// <summary>
  140. /// I/O指令输出
  141. /// </summary>
  142. /// <param name="processParam"></param>
  143. /// <param name="isStrobe">频闪</param>
  144. /// <param name="recover">输出sleep(recoverWaitTime)下,后恢复原信号</param>
  145. /// <param name="recoverWaitTime">sleep 后反转</param>
  146. public bool io_output(string tagName, JObject processParam, bool isStrobe = false, bool recover = false, int recoverWaitTime = 100)
  147. {
  148. bool result = false;
  149. string[] OUT_OP_SHOW = processParam.Value<JArray>("OUT_OP_SHOW").ToObject<List<string>>().ToArray();
  150. OUT_OP_SHOW = Utils.Util.IODataFormatBinaryStr(OUT_OP_SHOW, true);
  151. for (int i = 0; i < OUT_OP_SHOW.Length; i++)
  152. {
  153. for (int j = 0; j < OUT_OP_SHOW[i].Length; j++)
  154. {
  155. int jj = OUT_OP_SHOW[i].Length - j - 1;
  156. if (OUT_OP_SHOW[i][jj] == 'L' || OUT_OP_SHOW[i][jj] == 'H')
  157. {
  158. if (recover)
  159. {
  160. if (recoverWaitTime > 0)
  161. {
  162. OutDebugEvent?.Invoke(tagName, $"向I/O输出引脚{i}-{j},信号{OUT_OP_SHOW[i][jj]},输出时长:{recoverWaitTime}ms");
  163. devIOCard.writeBitState(i, j, OUT_OP_SHOW[i][jj] == 'H', isStrobe);
  164. Thread.Sleep(recoverWaitTime);
  165. }
  166. OutDebugEvent?.Invoke(tagName, $"向I/O输出引脚{i}-{j},信号{OUT_OP_SHOW[i][jj] == 'L'}");
  167. devIOCard.writeBitState(i, j, OUT_OP_SHOW[i][jj] == 'L');
  168. }
  169. else
  170. {
  171. OutDebugEvent?.Invoke(tagName, $"向I/O输出引脚{i}-{j},信号{OUT_OP_SHOW[i][jj]}");
  172. devIOCard.writeBitState(i, j, OUT_OP_SHOW[i][jj] == 'H', isStrobe);
  173. }
  174. result = true;
  175. }
  176. }
  177. }
  178. return result;
  179. }
  180. }
  181. }