革博士程序V1仓库
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Runtime.InteropServices;
  11. using System.Threading;
  12. using LeatherApp.Utils;
  13. using Models;
  14. using OpenCvSharp;
  15. using OpenCvSharp.Extensions;
  16. using S7.Net;
  17. namespace LeatherApp.Device
  18. {
  19. /// <summary>
  20. /// PLC操作类
  21. /// </summary>
  22. public class PLCDev : IDisposable
  23. {
  24. private Plc devPlc;
  25. public Action<DateTime,WarningEnum, string> WarningEvent;
  26. /// <summary>
  27. /// 运行状态
  28. /// </summary>
  29. public Action<bool> RuningStateChangeEvent;
  30. /// <summary>
  31. /// 是否打开设备成功
  32. /// </summary>
  33. public bool IsInit { get; private set; } = false;
  34. private Thread t_task1;
  35. public bool? devRunningState { get; private set; }
  36. public PLCDev()
  37. {
  38. }
  39. public bool start(CpuType cpu, string iPAdrees, short rack, short slot)
  40. {
  41. try
  42. {
  43. devPlc = new Plc(cpu, iPAdrees, rack, slot);
  44. devPlc.Open();
  45. if (!devPlc.IsConnected) throw new Exception("Plc连接失败!");
  46. IsInit = true;
  47. t_task1 = new System.Threading.Thread(run1);
  48. t_task1.IsBackground = true;
  49. t_task1.Start();
  50. return true;
  51. }
  52. catch (Exception ex)
  53. {
  54. WarningEvent?.BeginInvoke(DateTime.Now,WarningEnum.High, ex.Message,null,null);
  55. return false;
  56. }
  57. }
  58. public void stop()
  59. {
  60. if (!IsInit) return;
  61. try
  62. {
  63. IsInit = false;
  64. //timer.Elapsed -= Timer_Elapsed;
  65. if (t_task1 != null)
  66. {
  67. bool b = t_task1.Join(1000);
  68. if (!b) t_task1.Abort();
  69. t_task1 = null;
  70. }
  71. devPlc.Close();
  72. }
  73. catch { }
  74. }
  75. private void run1()
  76. {
  77. while (IsInit)
  78. {
  79. //1.3
  80. bool res = (Boolean)devPlc.Read("DB3.DBX1.3");
  81. if (devRunningState == null || devRunningState != res)
  82. {
  83. devRunningState = res;
  84. RuningStateChangeEvent?.Invoke(res);
  85. }
  86. Thread.Sleep(100);
  87. }
  88. }
  89. /// <summary>
  90. /// 启动DEV
  91. /// </summary>
  92. public void runDev()
  93. {
  94. if (IsInit)
  95. {
  96. devPlc.Write("DB3.DBX0.1", false);
  97. devPlc.Write("DB3.DBX0.0", true);//启动
  98. }
  99. }
  100. /// <summary>
  101. /// 停止DEV
  102. /// </summary>
  103. public void pauseDev()
  104. {
  105. if (IsInit)
  106. {
  107. devPlc.Write("DB3.DBX0.0", false);
  108. devPlc.Write("DB3.DBX0.1", true);//停止
  109. }
  110. }
  111. //private void callback(PhotoTask task)
  112. //{
  113. // //返回成功/失败,异步调用
  114. // if (task.finishEvent != null || (task.finishEvent = finishEvent) != null)
  115. // //task.finishEvent.BeginInvoke(result, errInfo, res => task.finishEvent.EndInvoke(res), null);
  116. // System.Threading.ThreadPool.QueueUserWorkItem(waitCallback, task);
  117. //}
  118. ////异步回调
  119. //WaitCallback waitCallback = new WaitCallback(o =>
  120. //{
  121. // var task = (PhotoTask)o;
  122. // task.finishEvent(task);
  123. //});
  124. public void Dispose()
  125. {
  126. stop();
  127. }
  128. }
  129. }