MainThreadClass.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. using CCDCount.DLL.Tools;
  2. using CCDCount.MODEL.CameraClass;
  3. using CCDCount.MODEL.ConfigModel;
  4. using CCDCount.MODEL.ShuLiClass;
  5. using LogClass;
  6. using MvCameraControl;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Runtime.InteropServices;
  15. using System.Runtime.Remoting.Messaging;
  16. using System.Threading;
  17. namespace CCDCount.DLL
  18. {
  19. public class MainThreadClass
  20. {
  21. #region 变量与实例
  22. Thread SwitchIdentifyImageThread = null;
  23. private bool IsSwitch = false;
  24. public ShuLiClass shuLiClass = null;
  25. CameraClass cameraClass = new CameraClass();
  26. public string ThisCameraDevice = string.Empty;
  27. public string ThisCameraSN = string.Empty;
  28. public string ThisCamerName = string.Empty;
  29. public int ThisCamerNo = -1;
  30. public bool CameraStatic { get { return _CameraStatic; } }
  31. private bool _CameraStatic = false;
  32. // 返回交换线程状态
  33. public bool CameraRunStatic { get { return IsSwitch; } }
  34. public bool IsOpenLoadThread { get { return _IsOpenLoadThread; }}
  35. private bool _IsOpenLoadThread = false;
  36. public int HistoryActiveNum { get { return shuLiClass.GetHistoryActiveNum(); } }
  37. public int OkHistoryNum { get { return shuLiClass.GetOkHistoryNum(); } }
  38. public int NgHistoryNum { get { return shuLiClass.GetNgHistoryNum(); } }
  39. // 数粒信息发送线程
  40. Thread SendBottLogicMessageThread = null;
  41. bool IsSend = false;
  42. // Modbus客户端实例
  43. ModbusTcpClient modbusTcpClient = null;
  44. // 颗粒结果待发送队列
  45. ConcurrentQueue<ushort> SendQueue = new ConcurrentQueue<ushort>();
  46. /// <summary>
  47. /// 数粒状态计时器
  48. /// </summary>
  49. Stopwatch stopwatch = Stopwatch.StartNew();
  50. /// <summary>
  51. /// 数粒状态
  52. /// </summary>
  53. private bool _ShuLiState = true;
  54. public bool ShuLiState { get { return _ShuLiState; } }
  55. private bool IsDebug = false;
  56. #endregion
  57. #region 公共方法
  58. /// <summary>
  59. /// 设置ModbusTcpClient
  60. /// </summary>
  61. /// <param name="modbusTcpClient"></param>
  62. public void SetModbusClient(ModbusTcpClient modbusTcpClient)
  63. {
  64. this.modbusTcpClient = modbusTcpClient;
  65. if (!this.modbusTcpClient.IsTcpClientConnected())
  66. {
  67. LOG.error("ModbusTcpClient Connect Failed!");
  68. }
  69. else
  70. {
  71. StartSendBottLogicMessageThread();
  72. LOG.log("ModbusTcpClient Connect Success!", 6);
  73. }
  74. }
  75. /// <summary>
  76. /// 初始化构造方法
  77. /// </summary>
  78. /// <param name="configClass"></param>
  79. /// <param name="CameraConfig"></param>
  80. public MainThreadClass(ShuLiConfigClass configClass,CameraConfig CameraConfig)
  81. {
  82. // 数粒配置文件地址
  83. if (configClass!=null)
  84. {
  85. // 创建数粒对象(配置文件)
  86. shuLiClass = new ShuLiClass(configClass);
  87. }
  88. else
  89. {
  90. // 创建数粒对象(默认)
  91. shuLiClass = new ShuLiClass();
  92. }
  93. _IsOpenLoadThread = CameraConfig.IsOpenLoad;
  94. ThisCameraSN = CameraConfig.CameraSNNum;
  95. ThisCamerName = CameraConfig.CameraName;
  96. ThisCameraDevice = CameraConfig.DeviceName;
  97. ThisCamerNo = CameraConfig.CamerNo;
  98. }
  99. /// <summary>
  100. /// 开始主线程
  101. /// </summary>
  102. public bool StartMianThread(CameraConfig DeviceConfig)
  103. {
  104. bool result = false;
  105. // 相机列表
  106. List<CameraInfoClass> list = new List<CameraInfoClass>();
  107. // 获取相机列表
  108. cameraClass.GetCameraList(out list);
  109. if (list.Count == 0)
  110. {
  111. LOG.error(string.Format("{0}:没有相机", "MainThreadClass-StartMianThread"));
  112. // 如果没有相机,则退出
  113. return result;
  114. }
  115. // 加载相机
  116. // cameraClass.LoadCamereDevice(list.First().DeviceSN);
  117. if(!cameraClass.LoadCamereDevice(DeviceConfig))
  118. {
  119. LOG.error(string.Format("{0}:相机加载失败", "MainThreadClass-StartMianThread"));
  120. return result;
  121. }
  122. // 取图线程开启
  123. cameraClass.StartCamera();
  124. _CameraStatic = true;
  125. // 数据交换线程开启
  126. StartSwitchThread();
  127. // 为数粒算法的识别成功一粒回调函数添加方法
  128. shuLiClass.WorkCompleted += Worker_OneGrainCompleted;
  129. // 开启识别线程
  130. shuLiClass.StartIdentifyFuntion(cameraClass.GetCamereImageSize().Width);
  131. result = true;
  132. return result;
  133. }
  134. /// <summary>
  135. /// 开始主线程
  136. /// </summary>
  137. public bool StartMianThread()
  138. {
  139. bool result = false;
  140. // 相机列表
  141. List<CameraInfoClass> list = new List<CameraInfoClass>();
  142. // 获取相机列表
  143. cameraClass.GetCameraList(out list);
  144. if (list.Count == 0)
  145. {
  146. // 如果没有相机,则退出
  147. return result;
  148. }
  149. // 为数粒算法的识别成功一粒回调函数添加方法
  150. shuLiClass.WorkCompleted += Worker_OneGrainCompleted;
  151. // 加载相机
  152. if (!cameraClass.LoadCamereDevice(new CameraConfig() { CameraSNNum = list.First().DeviceSN}))
  153. return result;
  154. _CameraStatic = true;
  155. // 取图线程开启
  156. cameraClass.StartCamera();
  157. // 数据交换线程开启
  158. StartSwitchThread();
  159. // 开启识别线程
  160. shuLiClass.StartIdentifyFuntion(cameraClass.GetCamereImageSize().Width);
  161. result = true;
  162. return result;
  163. }
  164. /// <summary>
  165. /// 停止主线程
  166. /// </summary>
  167. public void StopMianThread()
  168. {
  169. shuLiClass.WorkCompleted -= Worker_OneGrainCompleted;
  170. // 相机取图线程关闭
  171. cameraClass.StopCamera();
  172. // 数据交换线程关闭
  173. StopSwitchThread();
  174. // 数粒识别线程关闭
  175. shuLiClass.StopIdentifyFuntion();
  176. StopSendBottLogicMessageThread();
  177. }
  178. /// <summary>
  179. /// 重加载相机
  180. /// </summary>
  181. /// <param name="CameraSN"></param>
  182. /// <returns></returns>
  183. public bool ReLoadCamera(string CameraSN)
  184. {
  185. bool result = false;
  186. result = cameraClass.ReLoadCameraDevice(CameraSN);
  187. if (result)
  188. {
  189. CameraConfig ReloadCaonfig = cameraClass.GetConfigValue();
  190. ThisCameraSN = ReloadCaonfig.CameraSNNum;
  191. ThisCamerName = ReloadCaonfig.CameraName;
  192. ThisCameraDevice = ReloadCaonfig.DeviceName;
  193. }
  194. return result;
  195. }
  196. /// <summary>
  197. /// 释放相机资源
  198. /// </summary>
  199. public void DisposeCamera()
  200. {
  201. cameraClass.StopCamera();
  202. ThisCameraSN = string.Empty;
  203. ThisCamerName = string.Empty;
  204. ThisCameraDevice = string.Empty;
  205. }
  206. /// <summary>
  207. /// 获取显示用的图片数据
  208. /// </summary>
  209. /// <param name="ImageHeight"></param>
  210. /// <param name="Data"></param>
  211. public void GetShowImage(int ImageHeight,out Bitmap ImageData)
  212. {
  213. List<RowStartEndCol> RowsShowList = new List<RowStartEndCol>();
  214. ActiveObjectClass NewActive = shuLiClass.GetLastActive();
  215. if (NewActive == null)
  216. {
  217. LOG.log(string.Format("{0}:没有获取到数粒数据", "MainThreadClass-GetShowImage"));
  218. ImageData = null;
  219. return;
  220. }
  221. List<ActiveObjectClass> Data = shuLiClass.GetHistoryActive().Where(o => o.LastSeenLine > NewActive.LastSeenLine - ImageHeight).ToList();
  222. Data.ForEach(o => o.RowsData.ForEach(p => RowsShowList.Add(p)));
  223. Bitmap BitmapImage = new Bitmap(NewActive.ImageWidth, ImageHeight);
  224. using (Graphics g = Graphics.FromImage(BitmapImage))
  225. {
  226. g.Clear(Color.White);
  227. using (Pen BlackPan = new Pen(Color.Black, 4))
  228. {
  229. for (int i = 0; i < shuLiClass.ChannelsRoi.Count - 1; i++)
  230. {
  231. g.DrawLine(BlackPan, new Point(shuLiClass.ChannelsRoi[i], 0), new Point(shuLiClass.ChannelsRoi[i], BitmapImage.Height));
  232. }
  233. }
  234. using (Pen redPen = new Pen(Color.Red, 1))
  235. {
  236. List<RowStartEndCol> ShowList = RowsShowList.Where(o => o.RowsCol > NewActive.LastSeenLine - BitmapImage.Height).ToList();
  237. RowsShowList.Where(o => o.RowsCol < NewActive.LastSeenLine - BitmapImage.Height).ToList().ForEach(o => RowsShowList.Remove(o));
  238. RowsShowList.Clear();
  239. ShowList.ForEach(o => g.DrawLine(redPen, new Point(o.StartCol, (int)(NewActive.LastSeenLine - o.RowsCol)), new Point(o.EndCol, (int)(NewActive.LastSeenLine - o.RowsCol))));
  240. ShowList.Clear();
  241. }
  242. }
  243. ImageData = BitmapImage.Clone() as Bitmap;
  244. BitmapImage.Dispose();
  245. //GC.Collect();
  246. }
  247. /// <summary>
  248. /// 获取无数据的图片
  249. /// </summary>
  250. /// <param name="ImageWidth"></param>
  251. /// <param name="ImageHeight"></param>
  252. /// <param name="ImageData"></param>
  253. public void GetNullShowImage(int ImageWidth, int ImageHeight, out Bitmap ImageData)
  254. {
  255. Bitmap BitmapImage = new Bitmap(ImageWidth, ImageHeight);
  256. using (Graphics g = Graphics.FromImage(BitmapImage))
  257. {
  258. g.Clear(Color.White);
  259. using (Pen BlackPan = new Pen(Color.Black, 4))
  260. {
  261. for (int i = 0; i < shuLiClass.ChannelsRoi.Count - 1; i++)
  262. {
  263. g.DrawLine(BlackPan, new Point(shuLiClass.ChannelsRoi[i], 0), new Point(shuLiClass.ChannelsRoi[i], BitmapImage.Height));
  264. }
  265. }
  266. }
  267. ImageData = BitmapImage.Clone() as Bitmap;
  268. BitmapImage.Dispose();
  269. }
  270. /// <summary>
  271. /// 获取此刻的Config数据
  272. /// </summary>
  273. /// <param name="config"></param>
  274. public void GetConfigValue(out CameraConfig Camconfig,out ShuLiConfigClass shuLiConfig)
  275. {
  276. //判断是否加载了相机
  277. if(cameraClass.IsLoadCamera())
  278. //获取已经加载的相机的配置
  279. Camconfig = cameraClass.GetConfigValue();
  280. else
  281. //新建一个相机配置
  282. Camconfig = new CameraConfig();
  283. //读取视觉配置
  284. shuLiConfig = shuLiClass.GetConfigValue();
  285. }
  286. /// <summary>
  287. /// 获取相机此刻的Config数据
  288. /// </summary>
  289. /// <param name="CameraConfig"></param>
  290. public void GetCameraConfig(out CameraConfig CameraConfig)
  291. {
  292. //判断是否加载了相机
  293. if (cameraClass.IsLoadCamera())
  294. //获取已经加载的相机的配置
  295. CameraConfig = cameraClass.GetConfigValue();
  296. else
  297. //新建一个相机配置
  298. CameraConfig = new CameraConfig();
  299. }
  300. /// <summary>
  301. /// 保存所有Config
  302. /// </summary>
  303. public void SaveAllConfig()
  304. {
  305. shuLiClass.SaveConfig();
  306. }
  307. /// <summary>
  308. /// 获取相机连接状态
  309. /// </summary>
  310. /// <returns></returns>
  311. public bool GetCameraConnectStatic()
  312. {
  313. bool result = false;
  314. if(cameraClass != null)
  315. result = cameraClass.IsConnectCamera();
  316. return result;
  317. }
  318. /// <summary>
  319. /// 清除历史数据
  320. /// </summary>
  321. /// <returns></returns>
  322. public bool ClearHistoryActive()
  323. {
  324. bool result = false;
  325. if(shuLiClass != null)
  326. result = shuLiClass.ClearHistoryActive();
  327. return result;
  328. }
  329. /// <summary>
  330. /// 获取过去一秒内颗粒数量
  331. /// </summary>
  332. /// <returns></returns>
  333. public int GetOneSecondActiveNum()
  334. {
  335. int result = 0;
  336. if(shuLiClass != null)
  337. result = shuLiClass.GetHistoryActive().Where(o=>o.EndCheckTime>DateTime.Now-TimeSpan.FromSeconds(1)).Count();
  338. return result;
  339. }
  340. #endregion
  341. #region 私有方法
  342. //bool IsFill = false;
  343. //bool IsXuanZhuanCloseFaMen = false;
  344. /// <summary>
  345. /// 每数完一粒识别线程执行的事件
  346. /// </summary>
  347. /// <param name="sender"></param>
  348. /// <param name="e"></param>
  349. private void Worker_OneGrainCompleted(object sender, ActiveObjectEventArgsClass e)
  350. {
  351. LOG.log("有活跃物体转换为了历史物体,回调事件被触发!", 6);
  352. LOG.log(string.Format("图像处理实例中的待识别图像缓存队列长度{0}", shuLiClass.ImageNum), 6);
  353. if (e.Actives.Where(o => o.StateCode == 7).Count() > 0)
  354. {
  355. stopwatch.Restart();
  356. _ShuLiState = false;
  357. }
  358. else if (stopwatch.ElapsedMilliseconds > 1000)
  359. {
  360. stopwatch.Stop();
  361. _ShuLiState = true;
  362. }
  363. //往数组中计数
  364. ushort result = new ushort();
  365. // 事件处理逻辑
  366. foreach (ActiveObjectClass oneActive in e.Actives)
  367. {
  368. Console.WriteLine(string.Format("输出当前颗粒信息,开始行:{0},结束行:{1},开始时间:{2}结束时间:{3},颗粒状态:{4},通道数:{5}",
  369. oneActive.StartLine, oneActive.LastSeenLine, oneActive.StartCheckTime.ToString("O"), oneActive.EndCheckTime.ToString("O"), oneActive.StateCode, oneActive.ChannelNO));
  370. LOG.log(string.Format("输出当前颗粒信息,开始行:{0},结束行:{1},开始时间:{2}结束时间:{3},颗粒状态:{4},通道数:{5}",
  371. oneActive.StartLine, oneActive.LastSeenLine, oneActive.StartCheckTime.ToString("O"), oneActive.EndCheckTime.ToString("O"), oneActive.StateCode, oneActive.ChannelNO), 6);
  372. if (oneActive.StateCode == 0 && oneActive.ChannelNO != -1)
  373. {
  374. //单通道合格计数
  375. result |= (ushort)(1 << (oneActive.ChannelNO));
  376. }
  377. else
  378. {
  379. //单通道不合格计数
  380. result |= (ushort)(1 << 8);
  381. }
  382. }
  383. LOG.log("当前待发送队列数量:" + SendQueue.Count, 6);
  384. if(IsSend)
  385. {
  386. SendQueue.Enqueue(result);
  387. }
  388. }
  389. #endregion
  390. #region 线程方法
  391. /// <summary>
  392. /// 开启交换线程
  393. /// </summary>
  394. private void StartSwitchThread()
  395. {
  396. IsSwitch = true;
  397. SwitchIdentifyImageThread = new Thread(SwitchIdentifyImageProcess);
  398. SwitchIdentifyImageThread.Start();
  399. }
  400. /// <summary>
  401. /// 关闭交换线程
  402. /// </summary>
  403. private void StopSwitchThread()
  404. {
  405. try
  406. {
  407. // 标志位设为false
  408. IsSwitch = false;
  409. if (SwitchIdentifyImageThread != null && SwitchIdentifyImageThread.IsAlive)
  410. SwitchIdentifyImageThread.Join();
  411. }
  412. catch (Exception ex)
  413. {
  414. LOG.error("Start thread failed!, " + ex.Message);
  415. throw;
  416. }
  417. }
  418. /// <summary>
  419. /// 交换线程
  420. /// </summary>
  421. private void SwitchIdentifyImageProcess()
  422. {
  423. IFrameOut IFramedata = null;
  424. Stopwatch stopwatch = Stopwatch.StartNew();
  425. stopwatch.Start();
  426. while (IsSwitch)
  427. {
  428. if (stopwatch.ElapsedMilliseconds > 1000)
  429. {
  430. Console.WriteLine("交换线程-图像处理实例中的待识别图像缓存队列长度{0}", shuLiClass.ImageNum);
  431. GC.Collect();
  432. stopwatch.Restart();
  433. }
  434. //Debug模式,不进行图像处理
  435. if (IsDebug)
  436. {
  437. continue;
  438. }
  439. //Thread.Sleep(5);
  440. bool result = cameraClass.GetOnceImage(out IFramedata);
  441. if (result)
  442. {
  443. if (IFramedata == null)
  444. continue;
  445. shuLiClass.SetOnceIdentifyImageData(IFramedata.Image);
  446. }
  447. else
  448. continue;
  449. IFramedata.Dispose();
  450. }
  451. stopwatch.Stop();
  452. }
  453. /// <summary>
  454. /// 启动发送消息线程
  455. /// </summary>
  456. private void StartSendBottLogicMessageThread()
  457. {
  458. try
  459. {
  460. //zmcauxClass.OpenZmcauxCard();
  461. timeBeginPeriod(1); // 设置为1ms精度
  462. IsSend = true;
  463. SendBottLogicMessageThread = new Thread(SendBottLogicMessageProcess);
  464. SendBottLogicMessageThread.Start();
  465. }
  466. catch (Exception ex)
  467. {
  468. LOG.error("Start thread failed!, " + ex.Message);
  469. throw;
  470. }
  471. }
  472. /// <summary>
  473. /// 停止发送消息线程
  474. /// </summary>
  475. private void StopSendBottLogicMessageThread()
  476. {
  477. try
  478. {
  479. // 标志位设为false
  480. IsSend = false;
  481. if (SendBottLogicMessageThread != null && SendBottLogicMessageThread.IsAlive)
  482. SendBottLogicMessageThread.Join();
  483. if (modbusTcpClient != null) modbusTcpClient.Disconnect();
  484. }
  485. catch (Exception ex)
  486. {
  487. LOG.error("Start thread failed!, " + ex.Message);
  488. throw;
  489. }
  490. }
  491. /// <summary>
  492. /// 信息发送线程
  493. /// </summary>
  494. private void SendBottLogicMessageProcess()
  495. {
  496. //获取数据
  497. ushort sendMessage = 0;
  498. bool AllowTransfer = false;
  499. bool TransferDone = false;
  500. Stopwatch sw = Stopwatch.StartNew();
  501. while (IsSend)
  502. {
  503. //LOG.log("进入线程", 6);
  504. sw.Restart();
  505. sendMessage = new ushort();
  506. //读取装瓶状态
  507. AllowTransfer = false;
  508. TransferDone = false;
  509. bool[] ReadResult = modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 11, numRegisters: 2);
  510. if (ReadResult == null)
  511. {
  512. continue;
  513. }
  514. AllowTransfer = ReadResult[1];
  515. TransferDone = ReadResult[0];
  516. //LOG.log(string.Format("读取值:AllowTransfer[0]:{0},TransferDone[1]:{1}", AllowTransfer, TransferDone), 6);
  517. //当允许写入且处于未写入的状态时
  518. if (AllowTransfer && !TransferDone)
  519. {
  520. if (SendQueue.Count() > 0)
  521. {
  522. if (!SendQueue.TryDequeue(out sendMessage))
  523. {
  524. LOG.error("MainThreadClass-SendBottLogicMessageProcess-SendQueue.TryDequeue failed!");
  525. }
  526. }
  527. if (sendMessage != 0)
  528. {
  529. //写入数据
  530. modbusTcpClient.WriteSingleRegister(slaveId: 1, registerAddress: 100, value: sendMessage);
  531. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 11, values: true);
  532. }
  533. sw.Stop();
  534. LOG.log(string.Format("sendMessage[1]:{0},此次写值耗时:{1}", sendMessage, sw.Elapsed), 6);
  535. }
  536. Thread.Sleep(1);
  537. }
  538. }
  539. #endregion
  540. #region 外部函数
  541. [DllImport("winmm.dll")]
  542. static extern uint timeBeginPeriod(uint period);
  543. #endregion
  544. }
  545. }