diff --git a/HitBotCSharpDemo/ShowForm.Designer.cs b/HitBotCSharpDemo/ShowForm.Designer.cs index 9f229ac..d2c3d87 100644 --- a/HitBotCSharpDemo/ShowForm.Designer.cs +++ b/HitBotCSharpDemo/ShowForm.Designer.cs @@ -187,7 +187,6 @@ this.lbl_Refresh_ID = new System.Windows.Forms.Label(); this.tim_IO_Refresh = new System.Windows.Forms.Timer(this.components); this.rit_Coord = new System.Windows.Forms.RichTextBox(); - this.test = new System.Windows.Forms.TabPage(); this.tac_MainForm.SuspendLayout(); this.tap_Axis.SuspendLayout(); this.grb_R.SuspendLayout(); @@ -236,7 +235,6 @@ this.tac_MainForm.Controls.Add(this.tap_Cam); this.tac_MainForm.Controls.Add(this.tap_tempCtrl); this.tac_MainForm.Controls.Add(this.tabPage1); - this.tac_MainForm.Controls.Add(this.test); this.tac_MainForm.Dock = System.Windows.Forms.DockStyle.Fill; this.tac_MainForm.Location = new System.Drawing.Point(0, 0); this.tac_MainForm.Name = "tac_MainForm"; @@ -1774,10 +1772,10 @@ // // cam_zoom // - this.cam_zoom.Location = new System.Drawing.Point(527, 29); + this.cam_zoom.Location = new System.Drawing.Point(289, 39); this.cam_zoom.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.cam_zoom.Name = "cam_zoom"; - this.cam_zoom.Size = new System.Drawing.Size(245, 162); + this.cam_zoom.Size = new System.Drawing.Size(481, 390); this.cam_zoom.TabIndex = 6; this.cam_zoom.TabStop = false; this.cam_zoom.Click += new System.EventHandler(this.cam_zoom_Click); @@ -1948,16 +1946,6 @@ this.rit_Coord.Text = ""; this.rit_Coord.TextChanged += new System.EventHandler(this.rit_Coord_TextChanged); // - // test - // - this.test.Location = new System.Drawing.Point(4, 22); - this.test.Name = "test"; - this.test.Padding = new System.Windows.Forms.Padding(3); - this.test.Size = new System.Drawing.Size(807, 502); - this.test.TabIndex = 7; - this.test.Text = "tabPage2"; - this.test.UseVisualStyleBackColor = true; - // // ShowForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); @@ -2183,7 +2171,6 @@ private System.Windows.Forms.PictureBox cam_zoom; private System.Windows.Forms.Label label33; private System.Windows.Forms.Label label32; - private System.Windows.Forms.TabPage test; } } diff --git a/HitBotCSharpDemo/ShowForm.cs b/HitBotCSharpDemo/ShowForm.cs index f293bed..4960bb2 100644 --- a/HitBotCSharpDemo/ShowForm.cs +++ b/HitBotCSharpDemo/ShowForm.cs @@ -1476,7 +1476,7 @@ namespace HitBotCSharpDemo private void Form1_FormClosing(object sender, FormClosingEventArgs e) { tempControl.OnFormClosing(); - //zoomControl?.OnFormClosing(); + zoomControl?.OnFormClosing(); } private void groupBox1_Enter(object sender, EventArgs e) @@ -1657,26 +1657,42 @@ namespace HitBotCSharpDemo button10.Enabled = false; button12.Enabled = false; } - // 启用变焦按钮 - //public void EnableZoomButtons() - //{ - // if (isZoomPortOpen && !string.IsNullOrEmpty(label30.Text)) - // { - // button6.Enabled = true; - // button8.Enabled = true; - // button10.Enabled = true; - // button12.Enabled = true; - // } - //} + // 添加运动完成后启用按钮的方法 + public void EnableZoomButtonsAfterMove() + { + if (isZoomPortOpen && zoomControl.IsSerialPortOpen()) + { + button6.Enabled = true; + button8.Enabled = true; + button10.Enabled = true; + button12.Enabled = true; + } + } private void button12_Click(object sender, EventArgs e) { + // 检查串口是否打开 + if (!isZoomPortOpen || !zoomControl.IsSerialPortOpen()) + { + MessageBox.Show("串口未打开,无法移动镜头"); + return; + } + // 移动到脉冲为9的位置 + zoomControl.MoveTo(9); } private void button10_Click(object sender, EventArgs e) { + // 检查串口是否打开 + if (!isZoomPortOpen || !zoomControl.IsSerialPortOpen()) + { + MessageBox.Show("串口未打开,无法移动镜头"); + return; + } + // 移动到脉冲为16498的位置 + zoomControl.MoveTo(16498); } } diff --git a/HitBotCSharpDemo/ZoomControl.cs b/HitBotCSharpDemo/ZoomControl.cs index 462cdc9..7d92491 100644 --- a/HitBotCSharpDemo/ZoomControl.cs +++ b/HitBotCSharpDemo/ZoomControl.cs @@ -21,6 +21,7 @@ namespace HitBotCSharpDemo [System.Runtime.InteropServices.DllImport("PMSOpticalDlld.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] private static extern bool PMSOptical_Close(ref IntPtr pHandle); + [System.Runtime.InteropServices.DllImport("PMSOpticalDlld.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] private static extern bool PMSOptical_IsOpened(IntPtr pHandle, out bool bIsOpened); @@ -29,12 +30,21 @@ namespace HitBotCSharpDemo CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] private static extern bool PMSOptical_GetPos(IntPtr pHandle, out int nCurrentPulse); + //获取设备运动状态 + [System.Runtime.InteropServices.DllImport("PMSOpticalDlld.dll", + CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] + private static extern bool PMSOptical_GetStatus(IntPtr pHandle, out int status); + + //绝对运动至指定脉冲位置 + [System.Runtime.InteropServices.DllImport("PMSOpticalDlld.dll", + CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] + private static extern bool PMSOptical_MoveTo(IntPtr pHandle, int nPulse); private IntPtr deviceHandle = IntPtr.Zero; private SerialPort serialPort; private ShowForm parentForm; - private StringBuilder receiveBuffer = new StringBuilder(); + private ZoomMoveController moveController; public ZoomControl(ShowForm form) { parentForm = form; @@ -132,6 +142,8 @@ namespace HitBotCSharpDemo if (bIsOpened) { // 关闭设备 + moveController?.Dispose(); + moveController = null; PMSOptical_Close(ref deviceHandle); deviceHandle = IntPtr.Zero; UpdateUIStatus(false, button, comboBox); @@ -152,6 +164,7 @@ namespace HitBotCSharpDemo if (deviceHandle != IntPtr.Zero) { // 再次验证设备是否真正打开 + moveController = new ZoomMoveController(deviceHandle, parentForm); bool finalCheck = PMSOptical_IsOpened(deviceHandle, out bIsOpened); if (finalCheck && bIsOpened) { @@ -300,5 +313,210 @@ namespace HitBotCSharpDemo } return false; } + public void OnFormClosing() + { + try + { + moveController?.Dispose(); + if (deviceHandle != IntPtr.Zero) + { + PMSOptical_Close(ref deviceHandle); + deviceHandle = IntPtr.Zero; + } + if (serialPort != null && serialPort.IsOpen) + { + serialPort.Close(); + } + } + catch (Exception ex) + { + Console.WriteLine($"关闭变焦控制时发生错误: {ex.Message}"); + } + } + + public void MoveTo(int targetPulse) + { + if (moveController == null && deviceHandle != IntPtr.Zero) + { + moveController = new ZoomMoveController(deviceHandle, parentForm); + } + + moveController?.MoveTo(targetPulse); + } + public class ZoomMoveController + { + private IntPtr deviceHandle; + private ShowForm parentForm; + private System.Threading.Timer statusTimer; + private bool isMoving = false; + private object moveLock = new object(); + public ZoomMoveController(IntPtr handle, ShowForm form) + { + deviceHandle = handle; + parentForm = form; + } + public void MoveTo(int targetPulse) + { + lock (moveLock) + { + if (deviceHandle == IntPtr.Zero) + { + MessageBox.Show("设备未连接", "错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + // 获取当前脉冲位置 + int currentPulse; + bool getCurrentResult = PMSOptical_GetPos(deviceHandle, out currentPulse); + + if (!getCurrentResult) + { + MessageBox.Show("获取当前位置失败", "错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + // 如果当前位置已经是目标位置,不执行移动 + if (currentPulse == targetPulse) + { + return; + } + try + { + // 禁用按钮 + parentForm.DisableZoomButtons(); + + // 开始移动 + int pulseToMove = targetPulse; + bool moveResult = PMSOptical_MoveTo(deviceHandle,pulseToMove); + + if (moveResult) + { + isMoving = true; + // 启动状态监控定时器,每500ms检查一次 + StartStatusMonitoring(); + } + else + { + MessageBox.Show("移动命令发送失败", "错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + // 重新启用按钮 + parentForm.EnableZoomButtonsAfterMove(); + } + } + catch (Exception ex) + { + MessageBox.Show($"移动时发生错误: {ex.Message}", "错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + // 重新启用按钮 + parentForm.EnableZoomButtonsAfterMove(); + } + } + } + private void StartStatusMonitoring() + { + // 如果已有定时器在运行,先停止 + statusTimer?.Dispose(); + + // 创建新的定时器,每500ms检查一次运动状态 + statusTimer = new System.Threading.Timer(CheckMovementStatus, null, 500, 500); + } + private void CheckMovementStatus(object state) + { + try + { + if (deviceHandle == IntPtr.Zero || !isMoving) + { + StopStatusMonitoring(); + return; + } + int status; + bool result = PMSOptical_GetStatus(deviceHandle, out status); + + if (result) + { + // 根据API文档:返回值 0为正在变焦,1为空闲状态,2为错误状态 + if (status == 1) // 空闲状态,移动完成 + { + isMoving = false; + StopStatusMonitoring(); + + // 在UI线程中启用按钮 + parentForm.Invoke(new Action(() => + { + parentForm.EnableZoomButtonsAfterMove(); + // 通过父窗体的公共方法更新当前脉冲显示 + UpdateCurrentPulseAfterMove(); + })); + } + else if (status == 2) // 错误状态 + { + isMoving = false; + StopStatusMonitoring(); + + parentForm.Invoke(new Action(() => + { + MessageBox.Show("镜头移动过程中发生错误", "错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + parentForm.EnableZoomButtonsAfterMove(); + })); + } + // status == 0 表示正在移动,继续监控 + } + else + { + // 查询状态失败 + isMoving = false; + StopStatusMonitoring(); + + parentForm.Invoke(new Action(() => + { + MessageBox.Show("查询运动状态失败", "错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + parentForm.EnableZoomButtonsAfterMove(); + })); + } + } + catch (Exception ex) + { + isMoving = false; + StopStatusMonitoring(); + + parentForm.Invoke(new Action(() => + { + MessageBox.Show($"监控运动状态时发生错误: {ex.Message}", "错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + parentForm.EnableZoomButtonsAfterMove(); + })); + } + } + private void UpdateCurrentPulseAfterMove() + { + try + { + int currentPulse; + bool result = PMSOptical_GetPos(deviceHandle, out currentPulse); + if (result) + { + // 直接更新显示 + parentForm.UpdateCurrentPulseDisplay(currentPulse.ToString()); + } + } + catch (Exception ex) + { + Console.WriteLine($"更新当前脉冲显示时发生错误: {ex.Message}"); + } + } + + private void StopStatusMonitoring() + { + statusTimer?.Dispose(); + statusTimer = null; + } + public void Dispose() + { + StopStatusMonitoring(); + } + } + } } diff --git a/HitBotCSharpDemo/bin/x64/Debug/HitBotCSharpDemo.exe b/HitBotCSharpDemo/bin/x64/Debug/HitBotCSharpDemo.exe index b99506f..9008548 100644 Binary files a/HitBotCSharpDemo/bin/x64/Debug/HitBotCSharpDemo.exe and b/HitBotCSharpDemo/bin/x64/Debug/HitBotCSharpDemo.exe differ diff --git a/HitBotCSharpDemo/bin/x64/Debug/HitBotCSharpDemo.pdb b/HitBotCSharpDemo/bin/x64/Debug/HitBotCSharpDemo.pdb index 9a6f4bf..994117b 100644 Binary files a/HitBotCSharpDemo/bin/x64/Debug/HitBotCSharpDemo.pdb and b/HitBotCSharpDemo/bin/x64/Debug/HitBotCSharpDemo.pdb differ diff --git a/HitBotCSharpDemo/obj/x64/Debug/CSharpDemo.csproj.GenerateResource.cache b/HitBotCSharpDemo/obj/x64/Debug/CSharpDemo.csproj.GenerateResource.cache index 3bbdf93..5b73363 100644 Binary files a/HitBotCSharpDemo/obj/x64/Debug/CSharpDemo.csproj.GenerateResource.cache and b/HitBotCSharpDemo/obj/x64/Debug/CSharpDemo.csproj.GenerateResource.cache differ diff --git a/HitBotCSharpDemo/obj/x64/Debug/HitBotCSharpDemo.exe b/HitBotCSharpDemo/obj/x64/Debug/HitBotCSharpDemo.exe index b99506f..9008548 100644 Binary files a/HitBotCSharpDemo/obj/x64/Debug/HitBotCSharpDemo.exe and b/HitBotCSharpDemo/obj/x64/Debug/HitBotCSharpDemo.exe differ diff --git a/HitBotCSharpDemo/obj/x64/Debug/HitBotCSharpDemo.pdb b/HitBotCSharpDemo/obj/x64/Debug/HitBotCSharpDemo.pdb index 9a6f4bf..994117b 100644 Binary files a/HitBotCSharpDemo/obj/x64/Debug/HitBotCSharpDemo.pdb and b/HitBotCSharpDemo/obj/x64/Debug/HitBotCSharpDemo.pdb differ