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

219 lines
7.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. internal class SmallAxis
  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<int, string> log;
  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. private bool preState;
  77. public SmallAxis(string comName,int baud)
  78. {
  79. pHandle = PMSOptical_OpenComm(comName, baud);
  80. }
  81. public void start()
  82. {
  83. //获取回零状态并状态栏显示
  84. bool bIsHomed = false;
  85. PMSOptical_IsHomed(pHandle, ref bIsHomed);
  86. if (!bIsHomed)
  87. {
  88. Thread.Sleep(100);
  89. PMSOptical_WaitForOpticalFinished(pHandle, 10000);
  90. PMSOptical_IsHomed(pHandle, ref bIsHomed);
  91. if (!bIsHomed)
  92. throw new Exception("镜头电机复位失败!");
  93. }
  94. prePPU = 0;
  95. preState= false;
  96. this.getCurrPPU();
  97. this.isMoving();
  98. //
  99. timer.Elapsed += Timer_Elapsed;
  100. timer.Interval = 1000;
  101. timer.Enabled = true;
  102. }
  103. public void stop()
  104. {
  105. if (IsInit) return;
  106. timer.Elapsed -= Timer_Elapsed;
  107. Thread.Sleep(200);
  108. PMSOptical_Close(ref pHandle);
  109. }
  110. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  111. {
  112. if (!IsInit) return;
  113. getCurrPPU();
  114. isMoving();
  115. }
  116. public bool home(bool await=false)
  117. {
  118. if (!IsInit) return false;
  119. PMSOptical_GoHome(pHandle);
  120. if (await)
  121. {
  122. PMSOptical_WaitForOpticalFinished(pHandle, 10000);
  123. //判断是否回零并设置按钮状态
  124. bool bIsHomed = false;
  125. PMSOptical_IsHomed(pHandle, ref bIsHomed);
  126. if (!bIsHomed)
  127. return false;
  128. getCurrPPU();
  129. }
  130. return true;
  131. }
  132. public void gotoPos(int ppu,bool await=true)
  133. {
  134. if (!IsInit) return;
  135. PMSOptical_MoveTo(pHandle, ppu);
  136. if (await)
  137. {
  138. PMSOptical_WaitForOpticalFinished(pHandle, 10000);
  139. getCurrPPU();
  140. }
  141. }
  142. public bool isMoving()
  143. {
  144. if (!IsInit)
  145. return false;
  146. int nStatus = 1; //注意,这里建议nStatus初始化为1 //返回0为运动状态,1为停止状态,2为初始化失败
  147. Thread.Sleep(20);
  148. //获取状态并刷新界面
  149. PMSOptical_GetStatus(pHandle, ref nStatus);
  150. bool state = 0 == nStatus;
  151. if (preState != state)
  152. {
  153. StateChangeEvent?.Invoke(state);
  154. preState = state;
  155. }
  156. return state;
  157. }
  158. //获取最大脉冲
  159. public int getMaxPPU()
  160. {
  161. if (!IsInit)
  162. return -1;
  163. int nMaxPos = 0;
  164. PMSOptical_GetMaxPos(pHandle, ref nMaxPos);
  165. return nMaxPos;
  166. }
  167. //获取当前脉冲
  168. public int getCurrPPU()
  169. {
  170. if (!IsInit)
  171. return -1;
  172. int nCurPos = 0;
  173. PMSOptical_GetPos(pHandle, ref nCurPos);
  174. if (prePPU != nCurPos)
  175. {
  176. PPUChangeEvent?.Invoke(prePPU, nCurPos);
  177. prePPU = nCurPos;
  178. }
  179. return nCurPos;
  180. }
  181. private byte[] subBuff(byte[] buff, int start, int length)
  182. {
  183. byte[] res = new byte[length];
  184. for (int i = start; i < buff.Length && i < start + length; i++)
  185. res[i - start] = buff[i];
  186. return res;
  187. }
  188. }
  189. }