From d8a22a15b11b333a1a815ca0adc0c0e51aa15d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Chsc=E2=80=9D?= <“huangsucan@kaiyili-lab.com”> Date: Wed, 29 Jul 2026 13:28:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E6=A4=8D=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACP.sln | 7 - ACP/ACP.csproj | 1 - BenchMovementModule/BenchMovementModule.cs | 19 - .../BenchMovementModule.csproj | 14 - .../HardwareDrive/GantryControlBase.cs | 164 ----- .../HardwareDrive/GantryControlRtu.cs | 28 - .../HardwareDrive/GantryControlTcp.cs | 27 - .../ViewModels/BenchMovementViewModel.cs | 522 ---------------- .../Views/BenchMovementView.xaml | 394 ------------ .../Views/BenchMovementView.xaml.cs | 28 - DeviceCommand/Devices/GantryControlTcp.cs | 540 ---------------- DeviceCommand/Devices/IT7800E.cs | 263 -------- DeviceCommand/Devices/N36200.cs | 386 ------------ DeviceCommand/Devices/N36600.cs | 484 --------------- DeviceCommand/Devices/N69200.cs | 575 ------------------ DeviceCommand/Devices/SDS2000X_HD.cs | 326 ---------- DeviceCommand/Devices/SPAW7000.cs | 259 -------- DeviceEditModule/DeviceEditModule.cs | 14 +- .../ViewModels/IT7800EViewModel.cs | 310 ---------- .../ViewModels/N36200ViewModel.cs | 309 ---------- .../ViewModels/N36600ViewModel.cs | 280 --------- .../ViewModels/N69200ViewModel.cs | 314 ---------- .../ViewModels/SDS2000X_HDViewModel.cs | 387 ------------ .../ViewModels/SPAW7000ViewModel.cs | 345 ----------- DeviceEditModule/Views/IT7800EView.xaml | 274 --------- DeviceEditModule/Views/IT7800EView.xaml.cs | 28 - DeviceEditModule/Views/N36200View.xaml | 264 -------- DeviceEditModule/Views/N36200View.xaml.cs | 28 - DeviceEditModule/Views/N36600View.xaml | 249 -------- DeviceEditModule/Views/N36600View.xaml.cs | 28 - DeviceEditModule/Views/N69200View.xaml | 263 -------- DeviceEditModule/Views/N69200View.xaml.cs | 28 - DeviceEditModule/Views/SDS2000X_HDView.xaml | 247 -------- .../Views/SDS2000X_HDView.xaml.cs | 28 - DeviceEditModule/Views/SPAW7000View.xaml | 262 -------- DeviceEditModule/Views/SPAW7000View.xaml.cs | 28 - 36 files changed, 2 insertions(+), 7721 deletions(-) delete mode 100644 BenchMovementModule/BenchMovementModule.cs delete mode 100644 BenchMovementModule/BenchMovementModule.csproj delete mode 100644 BenchMovementModule/HardwareDrive/GantryControlBase.cs delete mode 100644 BenchMovementModule/HardwareDrive/GantryControlRtu.cs delete mode 100644 BenchMovementModule/HardwareDrive/GantryControlTcp.cs delete mode 100644 BenchMovementModule/ViewModels/BenchMovementViewModel.cs delete mode 100644 BenchMovementModule/Views/BenchMovementView.xaml delete mode 100644 BenchMovementModule/Views/BenchMovementView.xaml.cs delete mode 100644 DeviceCommand/Devices/GantryControlTcp.cs delete mode 100644 DeviceCommand/Devices/IT7800E.cs delete mode 100644 DeviceCommand/Devices/N36200.cs delete mode 100644 DeviceCommand/Devices/N36600.cs delete mode 100644 DeviceCommand/Devices/N69200.cs delete mode 100644 DeviceCommand/Devices/SDS2000X_HD.cs delete mode 100644 DeviceCommand/Devices/SPAW7000.cs delete mode 100644 DeviceEditModule/ViewModels/IT7800EViewModel.cs delete mode 100644 DeviceEditModule/ViewModels/N36200ViewModel.cs delete mode 100644 DeviceEditModule/ViewModels/N36600ViewModel.cs delete mode 100644 DeviceEditModule/ViewModels/N69200ViewModel.cs delete mode 100644 DeviceEditModule/ViewModels/SDS2000X_HDViewModel.cs delete mode 100644 DeviceEditModule/ViewModels/SPAW7000ViewModel.cs delete mode 100644 DeviceEditModule/Views/IT7800EView.xaml delete mode 100644 DeviceEditModule/Views/IT7800EView.xaml.cs delete mode 100644 DeviceEditModule/Views/N36200View.xaml delete mode 100644 DeviceEditModule/Views/N36200View.xaml.cs delete mode 100644 DeviceEditModule/Views/N36600View.xaml delete mode 100644 DeviceEditModule/Views/N36600View.xaml.cs delete mode 100644 DeviceEditModule/Views/N69200View.xaml delete mode 100644 DeviceEditModule/Views/N69200View.xaml.cs delete mode 100644 DeviceEditModule/Views/SDS2000X_HDView.xaml delete mode 100644 DeviceEditModule/Views/SDS2000X_HDView.xaml.cs delete mode 100644 DeviceEditModule/Views/SPAW7000View.xaml delete mode 100644 DeviceEditModule/Views/SPAW7000View.xaml.cs diff --git a/ACP.sln b/ACP.sln index eb1924a..160b3d3 100644 --- a/ACP.sln +++ b/ACP.sln @@ -37,8 +37,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeviceEditModule", "DeviceE EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZLGUSBCANFD", "ZLGUSBCANFD\ZLGUSBCANFD.csproj", "{542FE380-2344-4343-9AC6-F0C587C13B88}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchMovementModule", "BenchMovementModule\BenchMovementModule.csproj", "{31F84D0E-3634-4C70-A830-21AF412E3BBF}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CANModule", "CANModule\CANModule.csproj", "{AF2533DD-599D-49D7-942B-B0C26534D15F}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "加密狗", "加密狗\加密狗.csproj", "{A35CA316-7B5B-4183-8636-4ECA532B1489}" @@ -119,10 +117,6 @@ Global {542FE380-2344-4343-9AC6-F0C587C13B88}.Debug|Any CPU.Build.0 = Debug|Any CPU {542FE380-2344-4343-9AC6-F0C587C13B88}.Release|Any CPU.ActiveCfg = Release|Any CPU {542FE380-2344-4343-9AC6-F0C587C13B88}.Release|Any CPU.Build.0 = Release|Any CPU - {31F84D0E-3634-4C70-A830-21AF412E3BBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {31F84D0E-3634-4C70-A830-21AF412E3BBF}.Debug|Any CPU.Build.0 = Debug|Any CPU - {31F84D0E-3634-4C70-A830-21AF412E3BBF}.Release|Any CPU.ActiveCfg = Release|Any CPU - {31F84D0E-3634-4C70-A830-21AF412E3BBF}.Release|Any CPU.Build.0 = Release|Any CPU {AF2533DD-599D-49D7-942B-B0C26534D15F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {AF2533DD-599D-49D7-942B-B0C26534D15F}.Debug|Any CPU.Build.0 = Debug|Any CPU {AF2533DD-599D-49D7-942B-B0C26534D15F}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -155,7 +149,6 @@ Global {3456C3AC-E7FF-45F8-A68F-BEED9969812C} = {1E92D601-68FC-45F6-8251-BD0F39226E5B} {850A5BE8-0BC1-4D6A-8351-DD812C9C4427} = {1E92D601-68FC-45F6-8251-BD0F39226E5B} {170AD4C1-189D-4FBE-B10D-2A4304527834} = {1E92D601-68FC-45F6-8251-BD0F39226E5B} - {31F84D0E-3634-4C70-A830-21AF412E3BBF} = {1E92D601-68FC-45F6-8251-BD0F39226E5B} {AF2533DD-599D-49D7-942B-B0C26534D15F} = {1E92D601-68FC-45F6-8251-BD0F39226E5B} {84006890-9288-44C4-9D17-FA88F3FDECFA} = {1E92D601-68FC-45F6-8251-BD0F39226E5B} {8923122F-F7D3-4AE7-9E86-9E78F1296A11} = {1E92D601-68FC-45F6-8251-BD0F39226E5B} diff --git a/ACP/ACP.csproj b/ACP/ACP.csproj index d830b54..77a4fd2 100644 --- a/ACP/ACP.csproj +++ b/ACP/ACP.csproj @@ -19,7 +19,6 @@ - diff --git a/BenchMovementModule/BenchMovementModule.cs b/BenchMovementModule/BenchMovementModule.cs deleted file mode 100644 index 6041442..0000000 --- a/BenchMovementModule/BenchMovementModule.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Reflection; -using BenchMovementModule.Views; - -namespace BenchMovementModule -{ - public class BenchMovementModule : IModule - { - public void OnInitialized(IContainerProvider containerProvider) - { - IRegionManager regionManager = containerProvider.Resolve(); - regionManager.RegisterViewWithRegion("ShellViewManager", typeof(BenchMovementView)); - } - - public void RegisterTypes(IContainerRegistry containerRegistry) - { - containerRegistry.RegisterForNavigation("BenchMovementView"); - } - } -} diff --git a/BenchMovementModule/BenchMovementModule.csproj b/BenchMovementModule/BenchMovementModule.csproj deleted file mode 100644 index 3df7504..0000000 --- a/BenchMovementModule/BenchMovementModule.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - net8.0-windows - enable - true - enable - - - - - - - diff --git a/BenchMovementModule/HardwareDrive/GantryControlBase.cs b/BenchMovementModule/HardwareDrive/GantryControlBase.cs deleted file mode 100644 index 4240e31..0000000 --- a/BenchMovementModule/HardwareDrive/GantryControlBase.cs +++ /dev/null @@ -1,164 +0,0 @@ -using DeviceCommand.Base; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace BenchMovementModule.HardwareDrive -{ - public abstract class GantryControlBase - { - protected readonly IModbusDevice _device; - protected readonly byte _slaveAddress; - - // 默认 PLC Modbus 映射地址 - protected const ushort ADDR_HM_X_ENABLE = 0xC100; // HM0: X轴使能 - protected const ushort ADDR_HM_Y_ENABLE = 0xC101; // HM1: Y轴使能 - protected const ushort ADDR_HM_Z_ENABLE = 0xC102; // HM2: Z轴使能 - - protected const ushort ADDR_M_X_START = 30; // M30: X轴指定位置启动 - protected const ushort ADDR_M_Y_START = 31; // M31: Y轴指定位置启动 - protected const ushort ADDR_M_Z_START = 35; // M35: Z轴指定位置启动 - - protected const ushort ADDR_M_ALL_STOP = 40; // M40: 全停 - protected const ushort ADDR_M_X_STOP = 41; // M41: X轴停止 - protected const ushort ADDR_M_Y_STOP = 42; // M42: Y轴停止 - protected const ushort ADDR_M_Z_STOP = 43; // M43: Z轴停止 - - protected const ushort ADDR_M_HOME = 100; // M100: 回原点触发 - - protected const ushort ADDR_HD_X_SPEED = 0xA166; // HD230: X轴速度 (32位) - protected const ushort ADDR_HD_Y_SPEED = 0xA17A; // HD250: Y轴速度 (32位) - protected const ushort ADDR_HD_Z_SPEED = 0xA170; // HD240: Z轴速度 (32位) - - protected const ushort ADDR_HD_X_TARGET = 0xA10C; // HD140: X轴指定位置 (32位) - protected const ushort ADDR_HD_Y_TARGET = 0xA120; // HD160: Y轴指定位置 (32位) - protected const ushort ADDR_HD_Z_TARGET = 0xA116; // HD150: Z轴指定位置 (32位) - - public bool IsConnected => _device?.IsConnected ?? false; - - protected GantryControlBase(IModbusDevice device, byte slaveAddress = 1) - { - _device = device ?? throw new ArgumentNullException(nameof(device)); - _slaveAddress = slaveAddress; - } - - public async Task ConnectAsync(CancellationToken ct = default) - { - return await _device.ConnectAsync(ct); - } - - public void Close() - { - _device.Close(); - } - - /// - /// 轴使能控制 - /// - public async Task SetAxisEnableAsync(int axis, bool enable, CancellationToken ct = default) - { - ushort address = axis switch - { - 1 => ADDR_HM_X_ENABLE, // X - 2 => ADDR_HM_Y_ENABLE, // Y - 3 => ADDR_HM_Z_ENABLE, // Z - _ => throw new ArgumentException("轴通道错误,仅支持 1(X), 2(Y), 3(Z)") - }; - - await _device.WriteSingleCoilAsync(_slaveAddress, address, enable, ct); - } - - /// - /// 左右移动(X轴) - /// - /// 目标绝对位置(脉冲数/单位值) - /// 运行速度 - public async Task MoveLeftRightAsync(int position, int speed, CancellationToken ct = default) - { - // 1. 写入速度与目标位置(32位数据,写入连续的2个保持寄存器) - await _device.WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_X_SPEED, Int32ToUshorts(speed), ct); - await _device.WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_X_TARGET, Int32ToUshorts(position), ct); - - // 2. 边缘触发启动信号 M30 - await TriggerCoilAsync(ADDR_M_X_START, ct); - } - - /// - /// 上下移动(Z轴) - /// - /// 目标绝对位置 - /// 运行速度 - public async Task MoveUpDownAsync(int position, int speed, CancellationToken ct = default) - { - // 1. 写入速度与目标位置(32位数据) - await _device.WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_Z_SPEED, Int32ToUshorts(speed), ct); - await _device.WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_Z_TARGET, Int32ToUshorts(position), ct); - - // 2. 边缘触发启动信号 M35 - await TriggerCoilAsync(ADDR_M_Z_START, ct); - } - - /// - /// 前后移动(Y轴 - 预留拓展) - /// - public async Task MoveFrontBackAsync(int position, int speed, CancellationToken ct = default) - { - await _device.WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_Y_SPEED, Int32ToUshorts(speed), ct); - await _device.WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_Y_TARGET, Int32ToUshorts(position), ct); - await TriggerCoilAsync(ADDR_M_Y_START, ct); - } - - /// - /// 全局回零 - /// - public async Task HomeAsync(CancellationToken ct = default) - { - await TriggerCoilAsync(ADDR_M_HOME, ct); - } - - /// - /// 轴停止 - /// - /// 1:X轴, 2:Y轴, 3:Z轴, 0:全停 - public async Task StopAsync(int axis = 0, CancellationToken ct = default) - { - ushort address = axis switch - { - 0 => ADDR_M_ALL_STOP, - 1 => ADDR_M_X_STOP, - 2 => ADDR_M_Y_STOP, - 3 => ADDR_M_Z_STOP, - _ => ADDR_M_ALL_STOP - }; - - await TriggerCoilAsync(address, ct); - } - - #region 内部工具方法 - - /// - /// 触发一个点动信号(置 1 后,延时 100ms 自动置 0) - /// - protected async Task TriggerCoilAsync(ushort address, CancellationToken ct = default) - { - await _device.WriteSingleCoilAsync(_slaveAddress, address, true, ct); - await Task.Delay(100, ct); // 给予 PLC 扫描周期足够的响应时间 - await _device.WriteSingleCoilAsync(_slaveAddress, address, false, ct); - } - - /// - /// 将 32 位整型数据转换为 Modbus 的 2 个 16 位无符号整数(高低字转换) - /// - protected ushort[] Int32ToUshorts(int value) - { - // 本处采用低字在前(CDAB),如果PLC高字在前(ABCD),可将返回数组顺序颠倒 - ushort lowWord = (ushort)(value & 0xFFFF); - ushort highWord = (ushort)((value >> 16) & 0xFFFF); - return new ushort[] { lowWord, highWord }; - } - - #endregion - } -} diff --git a/BenchMovementModule/HardwareDrive/GantryControlRtu.cs b/BenchMovementModule/HardwareDrive/GantryControlRtu.cs deleted file mode 100644 index 8ce5fa3..0000000 --- a/BenchMovementModule/HardwareDrive/GantryControlRtu.cs +++ /dev/null @@ -1,28 +0,0 @@ -using DeviceCommand.Base; -using System; -using System.Collections.Generic; -using System.IO.Ports; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace BenchMovementModule.HardwareDrive -{ - public class GantryControlRtu : GantryControlBase - { - // 暴露出底层的 ModbusRtu 实例以便修改串口配置 - public ModbusRtu RtuDevice => (ModbusRtu)_device; - - public GantryControlRtu(string portName, int baudRate = 9600, byte slaveAddress = 1) - : base(new ModbusRtu(), slaveAddress) - { - RtuDevice.ConfigureDevice(portName, baudRate); - } - - public GantryControlRtu(string portName, int baudRate, int dataBits, StopBits stopBits, Parity parity, byte slaveAddress = 1) - : base(new ModbusRtu(), slaveAddress) - { - RtuDevice.ConfigureDevice(portName, baudRate, dataBits, stopBits, parity); - } - } -} diff --git a/BenchMovementModule/HardwareDrive/GantryControlTcp.cs b/BenchMovementModule/HardwareDrive/GantryControlTcp.cs deleted file mode 100644 index dae9bd9..0000000 --- a/BenchMovementModule/HardwareDrive/GantryControlTcp.cs +++ /dev/null @@ -1,27 +0,0 @@ -using DeviceCommand.Base; -using Model.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace BenchMovementModule.HardwareDrive -{ - public class GantryControlTcp : GantryControlBase - { - // 暴露出底层的 ModbusTcp 实例以便修改网络配置 - public ModbusTcp TcpDevice => (ModbusTcp)_device; - - public GantryControlTcp(TcpConfig config, byte slaveAddress = 1) - : base(new ModbusTcp(config), slaveAddress) - { - } - - public GantryControlTcp(string ipAddress, int port = 502, byte slaveAddress = 1) - : base(new ModbusTcp(), slaveAddress) - { - TcpDevice.ConfigureDevice(ipAddress, port); - } - } -} diff --git a/BenchMovementModule/ViewModels/BenchMovementViewModel.cs b/BenchMovementModule/ViewModels/BenchMovementViewModel.cs deleted file mode 100644 index d1690c3..0000000 --- a/BenchMovementModule/ViewModels/BenchMovementViewModel.cs +++ /dev/null @@ -1,522 +0,0 @@ -using System; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using System.Windows.Input; -using System.Windows.Threading; -using DeviceCommand.Devices; -using Logger; -using Prism.Ioc; -using UIShare.GlobalVariable; -using UIShare.ViewModelBase; - -namespace BenchMovementModule.ViewModels -{ - public class BenchMovementViewModel : NavigateViewModelBase, IRegionMemberLifetime, IDisposable - { - #region 属性 - - public bool KeepAlive => true; - - // ===== GantryControlTcp 实例(从 DeviceManager 获取)===== - private GantryControlTcp? _gantry; - private DeviceManager? _deviceManager; - private readonly GlobalInfo _globalInfo; - - // ===== 连接状态 ===== - private bool _isConnected; - public bool IsConnected - { - get => _isConnected; - set => SetProperty(ref _isConnected, value); - } - - private string _connectionStatus = "未连接"; - public string ConnectionStatus - { - get => _connectionStatus; - set => SetProperty(ref _connectionStatus, value); - } - - // ===== 6轴绝对位置显示(相对原点的实时位置,只读)===== - private int _absPos1; - public int AbsPos1 { get => _absPos1; set => SetProperty(ref _absPos1, value); } - - private int _absPos2; - public int AbsPos2 { get => _absPos2; set => SetProperty(ref _absPos2, value); } - - private int _absPos3; - public int AbsPos3 { get => _absPos3; set => SetProperty(ref _absPos3, value); } - - private int _absPos4; - public int AbsPos4 { get => _absPos4; set => SetProperty(ref _absPos4, value); } - - private int _absPos5; - public int AbsPos5 { get => _absPos5; set => SetProperty(ref _absPos5, value); } - - private int _absPos6; - public int AbsPos6 { get => _absPos6; set => SetProperty(ref _absPos6, value); } - - // ===== 寸动目标位置 ===== - private int _targetX; - public int TargetX { get => _targetX; set => SetProperty(ref _targetX, value); } - - private int _targetY; - public int TargetY { get => _targetY; set => SetProperty(ref _targetY, value); } - - private int _targetZ; - public int TargetZ { get => _targetZ; set => SetProperty(ref _targetZ, value); } - - // ===== 扫描参数 ===== - private int _scanStepX = 1000; - public int ScanStepX { get => _scanStepX; set => SetProperty(ref _scanStepX, value); } - - private int _scanStartX; - public int ScanStartX { get => _scanStartX; set => SetProperty(ref _scanStartX, value); } - - private int _scanEndX = 50000; - public int ScanEndX { get => _scanEndX; set => SetProperty(ref _scanEndX, value); } - - private int _scanStepY = 1000; - public int ScanStepY { get => _scanStepY; set => SetProperty(ref _scanStepY, value); } - - private int _scanStartY; - public int ScanStartY { get => _scanStartY; set => SetProperty(ref _scanStartY, value); } - - private int _scanEndY = 50000; - public int ScanEndY { get => _scanEndY; set => SetProperty(ref _scanEndY, value); } - - private int _scanStepZ = 100; - public int ScanStepZ { get => _scanStepZ; set => SetProperty(ref _scanStepZ, value); } - - private int _scanStartZ; - public int ScanStartZ { get => _scanStartZ; set => SetProperty(ref _scanStartZ, value); } - - private int _scanEndZ = 20000; - public int ScanEndZ { get => _scanEndZ; set => SetProperty(ref _scanEndZ, value); } - - // ===== 速度设定(脉冲)===== - private int _speedX = 5000; - public int SpeedX { get => _speedX; set => SetProperty(ref _speedX, value); } - - private int _speedY = 5000; - public int SpeedY { get => _speedY; set => SetProperty(ref _speedY, value); } - - private int _speedZ = 2000; - public int SpeedZ { get => _speedZ; set => SetProperty(ref _speedZ, value); } - - private int _homeSpeed = 3000; - public int HomeSpeed { get => _homeSpeed; set => SetProperty(ref _homeSpeed, value); } - - // ===== 零点设定 ===== - private int _homeX; - public int HomeX { get => _homeX; set => SetProperty(ref _homeX, value); } - - private int _homeY; - public int HomeY { get => _homeY; set => SetProperty(ref _homeY, value); } - - private int _homeZ; - public int HomeZ { get => _homeZ; set => SetProperty(ref _homeZ, value); } - - #endregion - - #region 命令 - public ICommand ConnectCommand { get; } - public ICommand DisconnectCommand { get; } - public ICommand MoveXCommand { get; } - public ICommand MoveYCommand { get; } - public ICommand MoveZCommand { get; } - public ICommand MoveAllCommand { get; } - public ICommand GoHomeCommand { get; } - public ICommand EmergencyStopCommand { get; } - public ICommand StopXCommand { get; } - public ICommand StopYCommand { get; } - public ICommand StopZCommand { get; } - public ICommand SetSpeedCommand { get; } - public ICommand SetHomeCommand { get; } - public ICommand ReadStatusCommand { get; } - #endregion - - #region 私有字段 - private readonly DispatcherTimer _pollTimer; - private bool _isInitiated = false; - #endregion - - public BenchMovementViewModel(IContainerProvider containerProvider) : base(containerProvider) - { - _globalInfo = containerProvider.Resolve(); - ConnectCommand = new DelegateCommand(OnConnect); - DisconnectCommand = new DelegateCommand(OnDisconnect); - MoveXCommand = new DelegateCommand(OnMoveX); - MoveYCommand = new DelegateCommand(OnMoveY); - MoveZCommand = new DelegateCommand(OnMoveZ); - MoveAllCommand = new DelegateCommand(OnMoveAll); - GoHomeCommand = new DelegateCommand(OnGoHome); - EmergencyStopCommand = new DelegateCommand(OnEmergencyStop); - StopXCommand = new DelegateCommand(OnStopX); - StopYCommand = new DelegateCommand(OnStopY); - StopZCommand = new DelegateCommand(OnStopZ); - SetSpeedCommand = new DelegateCommand(OnSetSpeed); - SetHomeCommand = new DelegateCommand(OnSetHome); - ReadStatusCommand = new DelegateCommand(OnReadStatus); - - // 位置轮询定时器(500ms) - _pollTimer = new DispatcherTimer(DispatcherPriority.Background) - { - Interval = TimeSpan.FromMilliseconds(500) - }; - _pollTimer.Tick += OnPollTick; - } - - #region 命令处理 - - private async void OnConnect() - { - if (_gantry == null) return; - try - { - ConnectionStatus = "连接中..."; - bool ok = await _gantry.ConnectAsync(); - if (ok) - { - IsConnected = true; - ConnectionStatus = "已连接"; - _pollTimer.Start(); - await ReadDeviceParametersAsync(); - LoggerHelper.Info("[BenchMovement] 台架连接成功"); - } - else - { - ConnectionStatus = "连接失败"; - } - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] 连接失败: {ex.Message}"); - ConnectionStatus = $"连接失败: {ex.Message}"; - } - UpdateConnectionStatus(); - } - - private void OnDisconnect() - { - if (_gantry == null) return; - try - { - _pollTimer.Stop(); - _gantry.Close(); - IsConnected = false; - ConnectionStatus = "未连接"; - LoggerHelper.Info("[BenchMovement] 台架已断开"); - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] 断开失败: {ex.Message}"); - } - UpdateConnectionStatus(); - } - - private async void OnMoveX() - { - if (!EnsureConnected()) return; - try - { - await _gantry!.SetAxisTargetPositionAsync(1, TargetX); - await _gantry.StartSingleAxisAsync(1); - LoggerHelper.Info($"[BenchMovement] X轴移动到 {TargetX}"); - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] X轴移动失败: {ex.Message}"); - ShowErrorMessageBox($"X轴移动失败: {ex.Message}", () => { }); - } - } - - private async void OnMoveY() - { - if (!EnsureConnected()) return; - try - { - await _gantry!.SetAxisTargetPositionAsync(2, TargetY); - await _gantry.StartSingleAxisAsync(2); - LoggerHelper.Info($"[BenchMovement] Y轴移动到 {TargetY}"); - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] Y轴移动失败: {ex.Message}"); - ShowErrorMessageBox($"Y轴移动失败: {ex.Message}", () => { }); - } - } - - private async void OnMoveZ() - { - if (!EnsureConnected()) return; - try - { - await _gantry!.SetAxisTargetPositionAsync(3, TargetZ); - await _gantry.StartSingleAxisAsync(3); - LoggerHelper.Info($"[BenchMovement] Z轴移动到 {TargetZ}"); - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] Z轴移动失败: {ex.Message}"); - ShowErrorMessageBox($"Z轴移动失败: {ex.Message}", () => { }); - } - } - - private async void OnMoveAll() - { - if (!EnsureConnected()) return; - try - { - await _gantry!.SetAxisTargetPositionAsync(1, TargetX); - await _gantry.SetAxisTargetPositionAsync(2, TargetY); - await _gantry.SetAxisTargetPositionAsync(3, TargetZ); - await _gantry.StartAllAxesAsync(); - LoggerHelper.Info($"[BenchMovement] 三轴同时移动: X={TargetX}, Y={TargetY}, Z={TargetZ}"); - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] 三轴移动失败: {ex.Message}"); - ShowErrorMessageBox($"三轴移动失败: {ex.Message}", () => { }); - } - } - - private async void OnGoHome() - { - if (!EnsureConnected()) return; - try - { - await _gantry!.HomeAsync(); - LoggerHelper.Info("[BenchMovement] 回原点已触发"); - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] 回原点失败: {ex.Message}"); - ShowErrorMessageBox($"回原点失败: {ex.Message}", () => { }); - } - } - - private async void OnEmergencyStop() - { - if (!EnsureConnected()) return; - try - { - await _gantry!.SetEStopStateAsync(true); - LoggerHelper.Info("[BenchMovement] 急停已触发"); - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] 急停失败: {ex.Message}"); - } - } - - private async void OnStopX() - { - if (!EnsureConnected()) return; - try - { - await _gantry!.SetStopStateAsync(1, true); - await Task.Delay(100); - await _gantry.SetStopStateAsync(1, false); - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] X轴停止失败: {ex.Message}"); - } - } - - private async void OnStopY() - { - if (!EnsureConnected()) return; - try - { - await _gantry!.SetStopStateAsync(2, true); - await Task.Delay(100); - await _gantry.SetStopStateAsync(2, false); - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] Y轴停止失败: {ex.Message}"); - } - } - - private async void OnStopZ() - { - if (!EnsureConnected()) return; - try - { - await _gantry!.SetStopStateAsync(3, true); - await Task.Delay(100); - await _gantry.SetStopStateAsync(3, false); - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] Z轴停止失败: {ex.Message}"); - } - } - - private async void OnSetSpeed() - { - if (!EnsureConnected()) return; - try - { - await _gantry!.SetAxisSpeedAsync(1, SpeedX); - await _gantry.SetAxisSpeedAsync(2, SpeedY); - await _gantry.SetAxisSpeedAsync(3, SpeedZ); - await _gantry.SetHomePositionSpeedAsync(HomeSpeed); - LoggerHelper.Info($"[BenchMovement] 速度设定完成: X={SpeedX}, Y={SpeedY}, Z={SpeedZ}, 回零={HomeSpeed}"); - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] 速度设定失败: {ex.Message}"); - ShowErrorMessageBox($"速度设定失败: {ex.Message}", () => { }); - } - } - - private async void OnSetHome() - { - if (!EnsureConnected()) return; - try - { - await _gantry!.SetHomePositionAsync(HomeX, HomeY, HomeZ); - LoggerHelper.Info($"[BenchMovement] 零点设定完成: X={HomeX}, Y={HomeY}, Z={HomeZ}"); - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] 零点设定失败: {ex.Message}"); - ShowErrorMessageBox($"零点设定失败: {ex.Message}", () => { }); - } - } - - private async void OnReadStatus() - { - await ReadDeviceParametersAsync(); - } - - #endregion - - #region 辅助方法 - - private bool EnsureConnected() - { - if (_gantry == null || !_gantry.IsConnected) - { - ShowErrorMessageBox("设备未连接,请先连接台架", () => { }); - return false; - } - return true; - } - - private void UpdateConnectionStatus() - { - IsConnected = _gantry?.IsConnected ?? false; - ConnectionStatus = IsConnected ? "已连接" : "未连接"; - } - - private async Task ReadDeviceParametersAsync() - { - if (_gantry == null || !_gantry.IsConnected) return; - try - { - var (x, y, z) = await _gantry.GetHomePositionAsync(); - HomeX = x; HomeY = y; HomeZ = z; - - SpeedX = await _gantry.GetAxisSpeedAsync(1); - SpeedY = await _gantry.GetAxisSpeedAsync(2); - SpeedZ = await _gantry.GetAxisSpeedAsync(3); - HomeSpeed = await _gantry.GetHomePositionSpeedAsync(); - - // 读取 6 轴绝对位置 - var positions = await _gantry.GetAllAbsolutePositionsAsync(); - AbsPos1 = positions.Axis1; AbsPos2 = positions.Axis2; AbsPos3 = positions.Axis3; - AbsPos4 = positions.Axis4; AbsPos5 = positions.Axis5; AbsPos6 = positions.Axis6; - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] 读取设备参数失败: {ex.Message}"); - } - } - - private async void OnPollTick(object? sender, EventArgs e) - { - if (_gantry == null || !_gantry.IsConnected) return; - try - { - // 批量读取 6 轴绝对位置(一次性通信,高效) - var pos = await _gantry.GetAllAbsolutePositionsAsync(); - AbsPos1 = pos.Axis1; AbsPos2 = pos.Axis2; AbsPos3 = pos.Axis3; - AbsPos4 = pos.Axis4; AbsPos5 = pos.Axis5; AbsPos6 = pos.Axis6; - } - catch { /* 忽略轮询错误 */ } - } - - #endregion - - #region 生命周期 - - public override void OnNavigatedTo(NavigationContext navigationContext) - { - base.OnNavigatedTo(navigationContext); - if (!_isInitiated) - { - _isInitiated = true; - InitGantryFromDeviceManager(); - } - } - - /// - /// 从 DeviceManager.DeviceMap 中查找 GantryControlTcp 实例, - /// 优先使用当前打开的作用域,找不到则取任意已注册作用域。 - /// - private void InitGantryFromDeviceManager() - { - try - { - // 优先使用当前打开的作用域 - IScopedProvider? scope = null; - if (!string.IsNullOrEmpty(_globalInfo.CurrentOpeningScope) - && _globalInfo.ScopeDic.TryGetValue(_globalInfo.CurrentOpeningScope, out scope)) - { - // 找到了 - } - else - { - // 退而求其次,取任意可用作用域 - scope = _globalInfo.ScopeDic.Values.FirstOrDefault(); - } - - if (scope == null) - { - LoggerHelper.Warn("[BenchMovement] 无可用的作用域,无法解析 DeviceManager。"); - return; - } - - _deviceManager = scope.Resolve(); - _gantry = _deviceManager.DeviceMap.Values.OfType().FirstOrDefault(); - - if (_gantry != null) - { - LoggerHelper.Info($"[BenchMovement] 已从 DeviceManager 获取 GantryControlTcp 实例。"); - UpdateConnectionStatus(); - } - else - { - LoggerHelper.Warn("[BenchMovement] DeviceManager 中未找到 GantryControlTcp 设备,请检查设备配置。"); - } - } - catch (Exception ex) - { - LoggerHelper.Error($"[BenchMovement] 初始化 GantryControlTcp 失败: {ex.Message}"); - } - } - - public void Dispose() - { - _pollTimer.Stop(); - // GantryControlTcp 由 DeviceManager 统一管理生命周期,此处仅停止轮询 - } - - #endregion - } -} diff --git a/BenchMovementModule/Views/BenchMovementView.xaml b/BenchMovementModule/Views/BenchMovementView.xaml deleted file mode 100644 index 0b57a9b..0000000 --- a/BenchMovementModule/Views/BenchMovementView.xaml +++ /dev/null @@ -1,394 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -