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

255 rivejä
8.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Timers;
  10. using System.Web.UI.WebControls.WebParts;
  11. using ProductionControl.Utils;
  12. using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
  13. namespace ProductionControl.Device
  14. {
  15. public class SmallAxisDev : IDisposable
  16. {
  17. #region 导入C函数
  18. #if (!DEBUG)
  19. const string dllName = "PMSOpticalDll.dll";
  20. #else
  21. const string dllName = "PMSOpticalDlld.dll";
  22. #endif
  23. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_OpenSocket", CallingConvention = CallingConvention.Cdecl)]
  24. public static extern IntPtr PMSOptical_OpenSocket(string ip, int nPort);
  25. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_OpenComm", CallingConvention = CallingConvention.Cdecl)]
  26. public static extern IntPtr PMSOptical_OpenComm(string comname, int nBaud);
  27. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_IsOpened", CallingConvention = CallingConvention.Cdecl)]
  28. public static extern bool PMSOptical_IsOpened(IntPtr ptr, ref bool bIsOpened);
  29. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_Close", CallingConvention = CallingConvention.Cdecl)]
  30. public static extern bool PMSOptical_Close(ref IntPtr ptr);
  31. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_GoHome", CallingConvention = CallingConvention.Cdecl)]
  32. public static extern bool PMSOptical_GoHome(IntPtr ptr);
  33. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_IsHomed", CallingConvention = CallingConvention.Cdecl)]
  34. public static extern bool PMSOptical_IsHomed(IntPtr ptr, ref bool bIsHomed);
  35. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_MoveTo", CallingConvention = CallingConvention.Cdecl)]
  36. public static extern bool PMSOptical_MoveTo(IntPtr ptr, int nPulse);
  37. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_Stop", CallingConvention = CallingConvention.Cdecl)]
  38. public static extern bool PMSOptical_Stop(IntPtr ptr);
  39. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_JogStart", CallingConvention = CallingConvention.Cdecl)]
  40. public static extern bool PMSOptical_JogStart(IntPtr ptr, int nJogSpeed);
  41. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_JogStop", CallingConvention = CallingConvention.Cdecl)]
  42. public static extern bool PMSOptical_JogStop(IntPtr ptr);
  43. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_GetPos", CallingConvention = CallingConvention.Cdecl)]
  44. public static extern bool PMSOptical_GetPos(IntPtr ptr, ref int nCurPluse);
  45. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_GetMaxPos", CallingConvention = CallingConvention.Cdecl)]
  46. public static extern bool PMSOptical_GetMaxPos(IntPtr ptr, ref int nMaxPluse);
  47. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_GetStatus", CallingConvention = CallingConvention.Cdecl)]
  48. public static extern bool PMSOptical_GetStatus(IntPtr ptr, ref int nStatus);
  49. [DllImport(dllName, CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_WaitForOpticalFinished", CallingConvention = CallingConvention.Cdecl)]
  50. public static extern void PMSOptical_WaitForOpticalFinished(IntPtr ptr, int nTimeOut);
  51. #endregion
  52. public Action<WarningEnum, string> WarningEvent;
  53. public Action<int, int> PPUChangeEvent;
  54. public Action<bool> StateChangeEvent;
  55. /// <summary>
  56. /// 最大倍数总的脉冲数
  57. /// </summary>
  58. public int MaxPPU { get; private set; }
  59. /// <summary>
  60. /// 是否打开设备成功
  61. /// </summary>
  62. public bool IsInit
  63. {
  64. get
  65. {
  66. if (pHandle == IntPtr.Zero)
  67. return false;
  68. bool bIsOpened = false;
  69. PMSOptical_IsOpened(pHandle, ref bIsOpened);
  70. return bIsOpened;
  71. }
  72. }
  73. private System.Timers.Timer timer = new System.Timers.Timer();
  74. private IntPtr pHandle;
  75. private int PrePPU;
  76. public bool PreRunState;
  77. private bool _isDebug = false;
  78. public SmallAxisDev(bool isDebug)
  79. {
  80. _isDebug = isDebug;
  81. }
  82. public SmallAxisDev()
  83. {
  84. }
  85. public bool start(string comName, int baud)
  86. {
  87. try
  88. {
  89. if(!IsInit)
  90. pHandle = PMSOptical_OpenComm(comName, baud);
  91. if (IsInit)
  92. {
  93. //获取回零状态并状态栏显示
  94. bool bIsHomed = false;
  95. PMSOptical_IsHomed(pHandle, ref bIsHomed);
  96. if (!bIsHomed)
  97. {
  98. Thread.Sleep(100);
  99. PMSOptical_WaitForOpticalFinished(pHandle, 10000);
  100. PMSOptical_IsHomed(pHandle, ref bIsHomed);
  101. if (!bIsHomed)
  102. throw new Exception("镜头电机复位失败!");
  103. }
  104. }
  105. PrePPU = 0;
  106. PreRunState = false;
  107. this.getMaxPPU();
  108. this.getCurrPPU();
  109. this.isMoving();
  110. //
  111. if (_isDebug)
  112. {
  113. timer.Elapsed += Timer_Elapsed;
  114. timer.Interval = 1000;
  115. timer.Start();
  116. }
  117. return true;
  118. }
  119. catch (Exception ex)
  120. {
  121. WarningEvent?.Invoke(WarningEnum.High, ex.Message);
  122. return false;
  123. }
  124. }
  125. public void stop()
  126. {
  127. try
  128. {
  129. if (_isDebug)
  130. {
  131. timer.Elapsed -= Timer_Elapsed;
  132. timer.Stop();
  133. }
  134. Thread.Sleep(200);
  135. PMSOptical_Close(ref pHandle);
  136. }
  137. catch { }
  138. }
  139. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  140. {
  141. if (!IsInit) return;
  142. getCurrPPU();
  143. isMoving();
  144. }
  145. public bool home(bool await=false)
  146. {
  147. if (!IsInit) return false;
  148. PMSOptical_GoHome(pHandle);
  149. if (await)
  150. {
  151. PMSOptical_WaitForOpticalFinished(pHandle, 10000);
  152. //判断是否回零并设置按钮状态
  153. bool bIsHomed = false;
  154. PMSOptical_IsHomed(pHandle, ref bIsHomed);
  155. if (!bIsHomed)
  156. return false;
  157. getCurrPPU();
  158. }
  159. return true;
  160. }
  161. public void gotoPos(int ppu,bool await=true)
  162. {
  163. if (!IsInit) return;
  164. if (ppu < 0 || ppu > MaxPPU) return;
  165. PMSOptical_MoveTo(pHandle, ppu);
  166. if (await)
  167. {
  168. PMSOptical_WaitForOpticalFinished(pHandle, 10000);
  169. getCurrPPU();
  170. }
  171. }
  172. public bool isMoving()
  173. {
  174. if (!IsInit)
  175. return false;
  176. int nStatus = 1; //注意,这里建议nStatus初始化为1 //返回0为运动状态,1为停止状态,2为初始化失败
  177. Thread.Sleep(20);
  178. //获取状态并刷新界面
  179. PMSOptical_GetStatus(pHandle, ref nStatus);
  180. bool state = 0 == nStatus;
  181. if (PreRunState != state)
  182. {
  183. StateChangeEvent?.Invoke(state);
  184. PreRunState = state;
  185. }
  186. return state;
  187. }
  188. //获取最大脉冲
  189. public int getMaxPPU()
  190. {
  191. if (!IsInit)
  192. return -1;
  193. int nMaxPos = 0;
  194. PMSOptical_GetMaxPos(pHandle, ref nMaxPos);
  195. MaxPPU = nMaxPos;
  196. return nMaxPos;
  197. }
  198. //获取当前脉冲
  199. public int getCurrPPU()
  200. {
  201. if (!IsInit)
  202. return -1;
  203. int nCurPos = 0;
  204. PMSOptical_GetPos(pHandle, ref nCurPos);
  205. if (PrePPU != nCurPos)
  206. {
  207. PPUChangeEvent?.Invoke(PrePPU, nCurPos);
  208. PrePPU = nCurPos;
  209. }
  210. return nCurPos;
  211. }
  212. private byte[] subBuff(byte[] buff, int start, int length)
  213. {
  214. byte[] res = new byte[length];
  215. for (int i = start; i < buff.Length && i < start + length; i++)
  216. res[i - start] = buff[i];
  217. return res;
  218. }
  219. public void Dispose()
  220. {
  221. stop();
  222. }
  223. }
  224. }