台架程序tp化

This commit is contained in:
hsc
2026-07-20 17:02:20 +08:00
parent 7f2721ab11
commit b79eea918e
29 changed files with 153 additions and 511 deletions

View File

@@ -18,7 +18,7 @@ using static System.Runtime.InteropServices.JavaScript.JSType;
using UIShare.GlobalVariable;
using UIShare;
using System;
using DeviceCommand.Device;
using DeviceCommand.Devices;
using DeviceCommand.Base;
using AutoMapper;
using Microsoft.Extensions.Logging.Abstractions;

View File

@@ -1,5 +1,5 @@
using DeviceCommand.Device;
using DeviceCommand.Devices;
using Logger;
using MaterialDesignThemes.Wpf;
using Microsoft.Win32;

View File

@@ -1,7 +1,5 @@
using System.Reflection;
using BenchMovementModule.Views;
using BenchMovementModule.Views.Dialogs;
using BenchMovementModule.ViewModels.Dialogs;
namespace BenchMovementModule
{
@@ -16,9 +14,6 @@ namespace BenchMovementModule
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<BenchMovementView>("BenchMovementView");
// 台架 TCP 连接配置弹窗
containerRegistry.RegisterDialog<GantryConfigDialogView, GantryConfigDialogViewModel>("GantryConfigDialog");
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -1,10 +1,13 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;
using BenchMovementModule.HardwareDrive;
using DeviceCommand.Devices;
using Logger;
using Prism.Ioc;
using UIShare.GlobalVariable;
using UIShare.ViewModelBase;
namespace BenchMovementModule.ViewModels
@@ -15,8 +18,10 @@ namespace BenchMovementModule.ViewModels
public bool KeepAlive => true;
// ===== GantryControlTcp 实例 =====
// ===== GantryControlTcp 实例(从 DeviceManager 获取)=====
private GantryControlTcp? _gantry;
private DeviceManager? _deviceManager;
private readonly GlobalInfo _globalInfo;
// ===== 连接状态 =====
private bool _isConnected;
@@ -116,7 +121,6 @@ namespace BenchMovementModule.ViewModels
#endregion
#region
public ICommand OpenConfigCommand { get; }
public ICommand ConnectCommand { get; }
public ICommand DisconnectCommand { get; }
public ICommand MoveXCommand { get; }
@@ -140,10 +144,7 @@ namespace BenchMovementModule.ViewModels
public BenchMovementViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
// 创建 GantryControlTcp 实例(默认配置,用户可通过弹窗修改)
_gantry = new GantryControlTcp("192.168.0.30", 502);
OpenConfigCommand = new DelegateCommand(OnOpenConfig);
_globalInfo = containerProvider.Resolve<GlobalInfo>();
ConnectCommand = new DelegateCommand(OnConnect);
DisconnectCommand = new DelegateCommand(OnDisconnect);
MoveXCommand = new DelegateCommand(OnMoveX);
@@ -169,16 +170,6 @@ namespace BenchMovementModule.ViewModels
#region
private void OnOpenConfig()
{
var param = new DialogParameters();
param.Add("Gantry", _gantry);
_dialogService.ShowDialog("GantryConfigDialog", param, result =>
{
UpdateConnectionStatus();
});
}
private async void OnConnect()
{
if (_gantry == null) return;
@@ -470,14 +461,60 @@ namespace BenchMovementModule.ViewModels
if (!_isInitiated)
{
_isInitiated = true;
InitGantryFromDeviceManager();
}
}
/// <summary>
/// 从 DeviceManager.DeviceMap 中查找 GantryControlTcp 实例,
/// 优先使用当前打开的作用域,找不到则取任意已注册作用域。
/// </summary>
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<DeviceManager>();
_gantry = _deviceManager.DeviceMap.Values.OfType<GantryControlTcp>().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();
_gantry?.Close();
_gantry?.TcpDevice?.Dispose();
// GantryControlTcp 由 DeviceManager 统一管理生命周期,此处仅停止轮询
}
#endregion

View File

@@ -1,202 +0,0 @@
using System.Collections.ObjectModel;
using System.Windows.Input;
using BenchMovementModule.HardwareDrive;
using Logger;
using UIShare.ViewModelBase;
namespace BenchMovementModule.ViewModels.Dialogs
{
public class GantryConfigDialogViewModel : DialogViewModelBase
{
#region
private string _title = "台架 TCP 连接配置";
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
}
private string _ipAddress = "192.168.0.30";
public string IpAddress
{
get => _ipAddress;
set => SetProperty(ref _ipAddress, value);
}
private int _port = 502;
public int Port
{
get => _port;
set => SetProperty(ref _port, value);
}
private int _sendTimeout = 3000;
public int SendTimeout
{
get => _sendTimeout;
set => SetProperty(ref _sendTimeout, value);
}
private int _receiveTimeout = 3000;
public int ReceiveTimeout
{
get => _receiveTimeout;
set => SetProperty(ref _receiveTimeout, value);
}
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);
}
private string _errorMessage = string.Empty;
public string ErrorMessage
{
get => _errorMessage;
set => SetProperty(ref _errorMessage, value);
}
public ObservableCollection<int> CommonPorts { get; } = new()
{
502, 102, 80, 8080, 5020, 4840
};
public ObservableCollection<int> CommonTimeouts { get; } = new()
{
500, 1000, 2000, 3000, 5000, 10000
};
#endregion
#region
public ICommand ConnectCommand { get; }
public ICommand DisconnectCommand { get; }
public ICommand CloseCommand { get; }
#endregion
private GantryControlTcp? _gantry;
public GantryConfigDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
ConnectCommand = new DelegateCommand(OnConnect);
DisconnectCommand = new DelegateCommand(OnDisconnect);
CloseCommand = new DelegateCommand(OnClose);
}
private void ApplyConfigToDevice()
{
if (_gantry?.TcpDevice == null) return;
_gantry.TcpDevice.ConfigureDevice(IpAddress, Port, SendTimeout, ReceiveTimeout);
}
private async void OnConnect()
{
ErrorMessage = string.Empty;
if (string.IsNullOrWhiteSpace(IpAddress))
{
ErrorMessage = "IP 地址不能为空";
return;
}
if (!System.Net.IPAddress.TryParse(IpAddress, out _))
{
ErrorMessage = "IP 地址格式不正确";
return;
}
if (Port <= 0 || Port > 65535)
{
ErrorMessage = "端口范围应在 1 - 65535";
return;
}
try
{
ConnectionStatus = "连接中...";
ApplyConfigToDevice();
bool ok = await _gantry!.ConnectAsync();
if (ok)
{
IsConnected = true;
ConnectionStatus = "已连接";
LoggerHelper.Info($"[BenchMovement] 台架连接成功: {IpAddress}:{Port}");
}
else
{
ConnectionStatus = "连接失败";
ErrorMessage = "连接失败,请检查网络和设备状态";
}
}
catch (Exception ex)
{
ConnectionStatus = "连接失败";
ErrorMessage = $"连接异常: {ex.Message}";
LoggerHelper.Error($"[BenchMovement] 连接异常: {ex.Message}");
}
}
private void OnDisconnect()
{
if (_gantry == null) return;
try
{
_gantry.Close();
IsConnected = false;
ConnectionStatus = "未连接";
ErrorMessage = string.Empty;
LoggerHelper.Info("[BenchMovement] 台架已断开");
}
catch (Exception ex)
{
ErrorMessage = $"断开失败: {ex.Message}";
LoggerHelper.Error($"[BenchMovement] 断开失败: {ex.Message}");
}
}
private void OnClose()
{
RequestClose.Invoke(ButtonResult.OK);
}
#region Prism Dialog
public override void OnDialogOpened(IDialogParameters parameters)
{
_eventAggregator.GetEvent<UIShare.PubEvent.OverlayEvent>().Publish(true);
if (parameters.ContainsKey("Gantry"))
{
_gantry = parameters.GetValue<GantryControlTcp>("Gantry");
// 从现有设备读取当前配置
if (_gantry?.TcpDevice != null)
{
IpAddress = _gantry.TcpDevice.IPAddress;
Port = _gantry.TcpDevice.Port;
SendTimeout = _gantry.TcpDevice.SendTimeout;
ReceiveTimeout = _gantry.TcpDevice.ReceiveTimeout;
}
// 同步当前连接状态
IsConnected = _gantry?.IsConnected ?? false;
ConnectionStatus = IsConnected ? "已连接" : "未连接";
}
}
public override void OnDialogClosed()
{
_eventAggregator.GetEvent<UIShare.PubEvent.OverlayEvent>().Publish(false);
}
#endregion
}
}

View File

@@ -195,8 +195,7 @@
</Ellipse.Style>
</Ellipse>
<TextBlock Text="{Binding ConnectionStatus}" FontSize="16" FontWeight="Bold" VerticalAlignment="Center" Margin="0,0,20,0"/>
<Button Content="配置连接" Width="100" Margin="0" Command="{Binding OpenConfigCommand}"/>
<Button Content="连接" Width="80" Margin="8,0,0,0" Style="{StaticResource ConnectButtonStyle}" Command="{Binding ConnectCommand}"/>
<Button Content="连接" Width="80" Margin="0" Style="{StaticResource ConnectButtonStyle}" Command="{Binding ConnectCommand}"/>
<Button Content="断开" Width="80" Margin="8,0,0,0" Style="{StaticResource StopButtonStyle}" Command="{Binding DisconnectCommand}"/>
<Button Content="刷新状态" Width="90" Margin="8,0,0,0" Command="{Binding ReadStatusCommand}"/>
</StackPanel>

View File

@@ -1,140 +0,0 @@
<UserControl x:Class="BenchMovementModule.Views.Dialogs.GantryConfigDialogView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:helpers="clr-namespace:UIShare.Helpers;assembly=UIShare"
xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare"
mc:Ignorable="d"
xmlns:prism="http://prismlibrary.com/"
Background="White"
prism:ViewModelLocator.AutoWireViewModel="True"
Width="440"
Height="380">
<prism:Dialog.WindowStyle>
<Style BasedOn="{StaticResource DialogUserManageStyle}"
TargetType="Window" />
</prism:Dialog.WindowStyle>
<UserControl.Resources>
<converters:StringToVisibilityConverter x:Key="StringToVisibility"/>
</UserControl.Resources>
<GroupBox Padding="12,8,12,8"
helpers:WindowDragHelper.EnableWindowDrag="True">
<GroupBox.Header>
<Grid Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="*" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}"
Foreground="White"
VerticalAlignment="Center"
Margin="5,0,10,0" />
</Grid>
</GroupBox.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- 连接配置表单 -->
<Grid Grid.Row="0" Margin="0,4,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0"
Text="IP 地址:"
VerticalAlignment="Center" Margin="0,6"/>
<TextBox Grid.Row="0" Grid.Column="1" Margin="0,6"
materialDesign:HintAssist.Hint=""
Text="{Binding IpAddress, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Grid.Row="1" Grid.Column="0"
Text="端口:"
VerticalAlignment="Center" Margin="0,6"/>
<ComboBox Grid.Row="1" Grid.Column="1" Margin="0,6"
IsEditable="True"
materialDesign:HintAssist.Hint=""
ItemsSource="{Binding CommonPorts}"
Text="{Binding Port, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Grid.Row="2" Grid.Column="0"
Text="发送超时(ms)"
VerticalAlignment="Center" Margin="0,6"/>
<ComboBox Grid.Row="2" Grid.Column="1" Margin="0,6"
IsEditable="True"
materialDesign:HintAssist.Hint=""
ItemsSource="{Binding CommonTimeouts}"
Text="{Binding SendTimeout, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Grid.Row="3" Grid.Column="0"
Text="接收超时(ms)"
VerticalAlignment="Center" Margin="0,6"/>
<ComboBox Grid.Row="3" Grid.Column="1" Margin="0,6"
IsEditable="True"
materialDesign:HintAssist.Hint=""
ItemsSource="{Binding CommonTimeouts}"
Text="{Binding ReceiveTimeout, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
<!-- 连接状态 -->
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0,8,0,0">
<Ellipse Width="12" Height="12" Margin="0,0,6,0">
<Ellipse.Style>
<Style TargetType="Ellipse">
<Setter Property="Fill" Value="#999999"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsConnected}" Value="True">
<Setter Property="Fill" Value="#5CB85C"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
<TextBlock Text="{Binding ConnectionStatus}" FontSize="14" FontWeight="Bold" VerticalAlignment="Center"/>
</StackPanel>
<!-- 错误提示 -->
<TextBlock Grid.Row="2"
Margin="0,8,0,0"
Foreground="#D32F2F"
TextWrapping="Wrap"
Text="{Binding ErrorMessage}"
Visibility="{Binding ErrorMessage, Converter={StaticResource StringToVisibility}}"/>
<!-- 按钮区 -->
<StackPanel Grid.Row="3"
Orientation="Horizontal"
HorizontalAlignment="Right"
Margin="0,12,0,0">
<Button Content="连接台架"
Width="100" Padding="0,4"
Margin="0,0,10,0"
Command="{Binding ConnectCommand}"/>
<Button Content="断开台架"
Width="100" Padding="0,4"
Margin="0,0,10,0"
Command="{Binding DisconnectCommand}"/>
<Button Content="关闭"
Width="80" Padding="0,4"
IsDefault="True"
Command="{Binding CloseCommand}"/>
</StackPanel>
</Grid>
</GroupBox>
</UserControl>

View File

@@ -1,12 +0,0 @@
using System.Windows.Controls;
namespace BenchMovementModule.Views.Dialogs
{
public partial class GantryConfigDialogView : UserControl
{
public GantryConfigDialogView()
{
InitializeComponent();
}
}
}

View File

@@ -1,4 +1,6 @@
using DeviceCommand.Base;
using Common.Attributes;
using DeviceCommand.Base;
using Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -6,84 +8,74 @@ using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace BenchMovementModule.HardwareDrive
namespace DeviceCommand.Devices
{
public abstract class GantryControlBase
[ADPCommand]
public class GantryControlTcp : ModbusTcp
{
protected readonly IModbusDevice _device;
protected readonly byte _slaveAddress;
private readonly byte _slaveAddress=1;
// 向后兼容TcpDevice 即自身(继承自 ModbusTcp
public ModbusTcp TcpDevice => this;
public GantryControlTcp(TcpConfig config) : base(config)
{
}
// =================================================================
// ------------------ Coil / Bit 状态与控制 (位元件) ------------------
// =================================================================
protected const ushort ADDR_M_ESTOP = 20496; // 0x5010: 紧停
protected const ushort ADDR_M_X_STOP = 41; // 0x29: X轴停止
protected const ushort ADDR_M_Y_STOP = 42; // 0x2A: Y轴停止
protected const ushort ADDR_M_Z_STOP = 43; // 0x2B: Z轴停止
private const ushort ADDR_M_ESTOP = 20496; // 0x5010: 紧停
private const ushort ADDR_M_X_STOP = 41; // 0x29: X轴停止
private const ushort ADDR_M_Y_STOP = 42; // 0x2A: Y轴停止
private const ushort ADDR_M_Z_STOP = 43; // 0x2B: Z轴停止
protected const ushort ADDR_M_X_START = 30; // 0x1E: X轴指定位置启动
protected const ushort ADDR_M_Y_START = 31; // 0x1F: Y轴指定位置启动
protected const ushort ADDR_M_Z_START = 35; // 0x23: Z轴指定位置启动
private const ushort ADDR_M_X_START = 30; // 0x1E: X轴指定位置启动
private const ushort ADDR_M_Y_START = 31; // 0x1F: Y轴指定位置启动
private const ushort ADDR_M_Z_START = 35; // 0x23: Z轴指定位置启动
protected const ushort ADDR_M_HOME_TRIGGER = 100; // 0x64: 回原点触发
private const ushort ADDR_M_HOME_TRIGGER = 100; // 0x64: 回原点触发
// =================================================================
// ------------------ 轴使能控制 (HM 寄存器) ------------------
// =================================================================
protected const ushort ADDR_HM_AXIS1_ENABLE = 49408; // 0xC100: 轴1使能 (X轴)
protected const ushort ADDR_HM_AXIS2_ENABLE = 49409; // 0xC101: 轴2使能 (Y轴)
protected const ushort ADDR_HM_AXIS3_ENABLE = 49410; // 0xC102: 轴3使能 (Z轴)
protected const ushort ADDR_HM_AXIS4_ENABLE = 49411; // 0xC103: 轴4使能
protected const ushort ADDR_HM_AXIS5_ENABLE = 49412; // 0xC104: 轴5使能
protected const ushort ADDR_HM_AXIS6_ENABLE = 49413; // 0xC105: 轴6使能
private const ushort ADDR_HM_AXIS1_ENABLE = 49408; // 0xC100: 轴1使能 (X轴)
private const ushort ADDR_HM_AXIS2_ENABLE = 49409; // 0xC101: 轴2使能 (Y轴)
private const ushort ADDR_HM_AXIS3_ENABLE = 49410; // 0xC102: 轴3使能 (Z轴)
private const ushort ADDR_HM_AXIS4_ENABLE = 49411; // 0xC103: 轴4使能
private const ushort ADDR_HM_AXIS5_ENABLE = 49412; // 0xC104: 轴5使能
private const ushort ADDR_HM_AXIS6_ENABLE = 49413; // 0xC105: 轴6使能
// =================================================================
// ------------------ 速度与位置参数 (HD 32位数据寄存器) --------------
// =================================================================
protected const ushort ADDR_HD_HOME_POS_SPEED = 41288; // 0xA148: 原点定位速度 (32位)
private const ushort ADDR_HD_HOME_POS_SPEED = 41288; // 0xA148: 原点定位速度 (32位)
// 轴原点位置
protected const ushort ADDR_HD_X_HOME_POS = 41198; // 0xA0EE: X轴原点位 (32位)
protected const ushort ADDR_HD_Z_HOME_POS = 41208; // 0xA0F8: Z轴原点位 (32位)
protected const ushort ADDR_HD_Y_HOME_POS = 41218; // 0xA102: Y轴原点位 (32位)
private const ushort ADDR_HD_X_HOME_POS = 41198; // 0xA0EE: X轴原点位 (32位)
private const ushort ADDR_HD_Z_HOME_POS = 41208; // 0xA0F8: Z轴原点位 (32位)
private const ushort ADDR_HD_Y_HOME_POS = 41218; // 0xA102: Y轴原点位 (32位)
// 轴运行目标位置
protected const ushort ADDR_HD_Y_TARGET = 41228; // 0xA10C: Y轴指定位置 (32位)
protected const ushort ADDR_HD_Z_TARGET = 41238; // 0xA116: Z轴指定位置 (32位)
protected const ushort ADDR_HD_X_TARGET = 41248; // 0xA120: X轴指定位置 (32位)
private const ushort ADDR_HD_Y_TARGET = 41228; // 0xA10C: Y轴指定位置 (32位)
private const ushort ADDR_HD_Z_TARGET = 41238; // 0xA116: Z轴指定位置 (32位)
private const ushort ADDR_HD_X_TARGET = 41248; // 0xA120: X轴指定位置 (32位)
// 轴运行速度
protected const ushort ADDR_HD_X_SPEED = 41318; // 0xA166: X轴速度 (32位)
protected const ushort ADDR_HD_Z_SPEED = 41328; // 0xA170: Z轴速度 (32位)
protected const ushort ADDR_HD_Y_SPEED = 41338; // 0xA17A: Y轴速度 (32位)
private const ushort ADDR_HD_X_SPEED = 41318; // 0xA166: X轴速度 (32位)
private const ushort ADDR_HD_Z_SPEED = 41328; // 0xA170: Z轴速度 (32位)
private const ushort ADDR_HD_Y_SPEED = 41338; // 0xA17A: Y轴速度 (32位)
// =================================================================
// ------------------ 轴绝对位置(只读,相对原点)------------------
// =================================================================
protected const ushort ADDR_ABS_AXIS1_POS = 47232; // 轴1绝对位置 (32位)
protected const ushort ADDR_ABS_AXIS2_POS = 47236; // 轴2绝对位置 (32位)
protected const ushort ADDR_ABS_AXIS3_POS = 47240; // 轴3绝对位置 (32位)
protected const ushort ADDR_ABS_AXIS4_POS = 47244; // 轴4绝对位置 (32位)
protected const ushort ADDR_ABS_AXIS5_POS = 47248; // 轴5绝对位置 (32位)
protected const ushort ADDR_ABS_AXIS6_POS = 47252; // 轴6绝对位置 (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<bool> ConnectAsync(CancellationToken ct = default)
{
return await _device.ConnectAsync(ct);
}
public void Close()
{
_device.Close();
}
private const ushort ADDR_ABS_AXIS1_POS = 47232; // 轴1绝对位置 (32位)
private const ushort ADDR_ABS_AXIS2_POS = 47236; // 轴2绝对位置 (32位)
private const ushort ADDR_ABS_AXIS3_POS = 47240; // 轴3绝对位置 (32位)
private const ushort ADDR_ABS_AXIS4_POS = 47244; // 轴4绝对位置 (32位)
private const ushort ADDR_ABS_AXIS5_POS = 47248; // 轴5绝对位置 (32位)
private const ushort ADDR_ABS_AXIS6_POS = 47252; // 轴6绝对位置 (32位)
// =================================================================
// ------------------ 核心控制逻辑与参数读写 ------------------------
@@ -106,7 +98,7 @@ namespace BenchMovementModule.HardwareDrive
_ => throw new ArgumentException("轴通道错误,仅支持 1 - 6", nameof(axis))
};
await _device.WriteSingleCoilAsync(_slaveAddress, address, enable, ct);
await WriteSingleCoilAsync(_slaveAddress, address, enable, ct);
}
/// <summary>
@@ -123,7 +115,7 @@ namespace BenchMovementModule.HardwareDrive
_ => throw new ArgumentException("轴通道错误,仅支持 1(X), 2(Y), 3(Z)", nameof(axis))
};
bool[] coils = await _device.ReadCoilsAsync(_slaveAddress, address, 1, ct);
bool[] coils = await ReadCoilsAsync(_slaveAddress, address, 1, ct);
return coils != null && coils.Length > 0 && coils[0];
}
@@ -144,9 +136,9 @@ namespace BenchMovementModule.HardwareDrive
if (zPos < 0 || zPos > 20000)
throw new ArgumentOutOfRangeException(nameof(zPos), $"Z轴原点设置值 [{zPos}] 超出合法范围 (0 - 20000)");
await _device.WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_X_HOME_POS, Int32ToUshorts(xPos), ct);
await _device.WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_Y_HOME_POS, Int32ToUshorts(yPos), ct);
await _device.WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_Z_HOME_POS, Int32ToUshorts(zPos), ct);
await WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_X_HOME_POS, Int32ToUshorts(xPos), ct);
await WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_Y_HOME_POS, Int32ToUshorts(yPos), ct);
await WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_Z_HOME_POS, Int32ToUshorts(zPos), ct);
}
/// <summary>
@@ -156,7 +148,7 @@ namespace BenchMovementModule.HardwareDrive
public async Task<(int X, int Y, int Z)> GetHomePositionAsync(CancellationToken ct = default)
{
// 41218(Y) - 41198(X) = 20加Y轴自身的 2 个寄存器,共批量读取 22 个连续寄存器
ushort[] registers = await _device.ReadHoldingRegistersAsync(_slaveAddress, ADDR_HD_X_HOME_POS, 22, ct);
ushort[] registers = await ReadHoldingRegistersAsync(_slaveAddress, ADDR_HD_X_HOME_POS, 22, ct);
if (registers == null || registers.Length < 22)
throw new InvalidOperationException("从 PLC 读取三轴原点位置失败,返回数据长度不足。");
@@ -176,7 +168,7 @@ namespace BenchMovementModule.HardwareDrive
if (speed < 0 || speed > 100000)
throw new ArgumentOutOfRangeException(nameof(speed), $"回原点定位速度值 [{speed}] 超出合法范围 (0 - 100000)");
await _device.WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_HOME_POS_SPEED, Int32ToUshorts(speed), ct);
await WriteMultipleRegistersAsync(_slaveAddress, ADDR_HD_HOME_POS_SPEED, Int32ToUshorts(speed), ct);
}
/// <summary>
@@ -184,7 +176,7 @@ namespace BenchMovementModule.HardwareDrive
/// </summary>
public async Task<int> GetHomePositionSpeedAsync(CancellationToken ct = default)
{
ushort[] registers = await _device.ReadHoldingRegistersAsync(_slaveAddress, ADDR_HD_HOME_POS_SPEED, 2, ct);
ushort[] registers = await ReadHoldingRegistersAsync(_slaveAddress, ADDR_HD_HOME_POS_SPEED, 2, ct);
if (registers == null || registers.Length < 2)
throw new InvalidOperationException("从 PLC 读取回原点定位速度失败,返回数据长度不足。");
@@ -209,7 +201,7 @@ namespace BenchMovementModule.HardwareDrive
_ => throw new ArgumentException("轴通道错误,仅支持 1(X), 2(Y), 3(Z)", nameof(axis))
};
await _device.WriteMultipleRegistersAsync(_slaveAddress, address, Int32ToUshorts(speed), ct);
await WriteMultipleRegistersAsync(_slaveAddress, address, Int32ToUshorts(speed), ct);
}
/// <summary>
@@ -225,7 +217,7 @@ namespace BenchMovementModule.HardwareDrive
_ => throw new ArgumentException("轴通道错误,仅支持 1(X), 2(Y), 3(Z)", nameof(axis))
};
ushort[] registers = await _device.ReadHoldingRegistersAsync(_slaveAddress, address, 2, ct);
ushort[] registers = await ReadHoldingRegistersAsync(_slaveAddress, address, 2, ct);
if (registers == null || registers.Length < 2)
throw new InvalidOperationException($"从 PLC 读取轴 {axis} 速度失败。");
@@ -274,7 +266,7 @@ namespace BenchMovementModule.HardwareDrive
);
}
await _device.WriteMultipleRegistersAsync(_slaveAddress, targetAddress, Int32ToUshorts(targetPos), ct);
await WriteMultipleRegistersAsync(_slaveAddress, targetAddress, Int32ToUshorts(targetPos), ct);
}
/// <summary>
@@ -290,7 +282,7 @@ namespace BenchMovementModule.HardwareDrive
_ => throw new ArgumentException("轴通道错误,仅支持 1(X), 2(Y), 3(Z)", nameof(axis))
};
ushort[] registers = await _device.ReadHoldingRegistersAsync(_slaveAddress, address, 2, ct);
ushort[] registers = await ReadHoldingRegistersAsync(_slaveAddress, address, 2, ct);
if (registers == null || registers.Length < 2)
throw new InvalidOperationException($"从 PLC 读取轴 {axis} 目标位置失败。");
@@ -303,7 +295,7 @@ namespace BenchMovementModule.HardwareDrive
/// </summary>
public async Task SetEStopStateAsync(bool state, CancellationToken ct = default)
{
await _device.WriteSingleCoilAsync(_slaveAddress, ADDR_M_ESTOP, state, ct);
await WriteSingleCoilAsync(_slaveAddress, ADDR_M_ESTOP, state, ct);
}
/// <summary>
@@ -319,7 +311,7 @@ namespace BenchMovementModule.HardwareDrive
_ => throw new ArgumentException("轴通道错误,仅支持 1(X), 2(Y), 3(Z)", nameof(axis))
};
await _device.WriteSingleCoilAsync(_slaveAddress, address, state, ct);
await WriteSingleCoilAsync(_slaveAddress, address, state, ct);
}
/// <summary>
@@ -407,7 +399,7 @@ namespace BenchMovementModule.HardwareDrive
throw new InvalidOperationException($"无法全开启动!存在运行速度为 0 的轴。当前速度 -> X:{xSpeed}, Y:{ySpeed}, Z:{zSpeed}");
// 2. 批量读取 1 ~ 6 号轴连续的使能状态线圈,极大地优化网络性能
bool[] enableStates = await _device.ReadCoilsAsync(_slaveAddress, ADDR_HM_AXIS1_ENABLE, 6, ct);
bool[] enableStates = await ReadCoilsAsync(_slaveAddress, ADDR_HM_AXIS1_ENABLE, 6, ct);
if (enableStates == null || enableStates.Length < 6)
throw new InvalidOperationException("从 PLC 读取 1~6 号轴使能状态失败。");
@@ -418,7 +410,7 @@ namespace BenchMovementModule.HardwareDrive
{
if (!enableStates[i])
{
await _device.WriteSingleCoilAsync(_slaveAddress, enableAddresses[i], true, ct);
await WriteSingleCoilAsync(_slaveAddress, enableAddresses[i], true, ct);
hasModifiedEnable = true;
}
}
@@ -450,15 +442,15 @@ namespace BenchMovementModule.HardwareDrive
throw new ArgumentOutOfRangeException(nameof(zTarget), $"Z轴目标位置 [{zTarget}] 越界,当前原点下最大范围 0-{zMaxAllowed}");
// 4. 全部联动校验通过,三轴点动同步启动
await _device.WriteSingleCoilAsync(_slaveAddress, ADDR_M_X_START, true, ct);
await _device.WriteSingleCoilAsync(_slaveAddress, ADDR_M_Y_START, true, ct);
await _device.WriteSingleCoilAsync(_slaveAddress, ADDR_M_Z_START, true, ct);
await WriteSingleCoilAsync(_slaveAddress, ADDR_M_X_START, true, ct);
await WriteSingleCoilAsync(_slaveAddress, ADDR_M_Y_START, true, ct);
await WriteSingleCoilAsync(_slaveAddress, ADDR_M_Z_START, true, ct);
await Task.Delay(100, ct); // 保持高电平状态以适配 PLC 扫描周期
await _device.WriteSingleCoilAsync(_slaveAddress, ADDR_M_X_START, false, ct);
await _device.WriteSingleCoilAsync(_slaveAddress, ADDR_M_Y_START, false, ct);
await _device.WriteSingleCoilAsync(_slaveAddress, ADDR_M_Z_START, false, ct);
await WriteSingleCoilAsync(_slaveAddress, ADDR_M_X_START, false, ct);
await WriteSingleCoilAsync(_slaveAddress, ADDR_M_Y_START, false, ct);
await WriteSingleCoilAsync(_slaveAddress, ADDR_M_Z_START, false, ct);
}
#region
@@ -466,17 +458,17 @@ namespace BenchMovementModule.HardwareDrive
/// <summary>
/// 触发点动脉冲信号(置 1 后,等待 100ms 自动置 0 释放)
/// </summary>
protected async Task TriggerCoilAsync(ushort address, CancellationToken ct = default)
private async Task TriggerCoilAsync(ushort address, CancellationToken ct = default)
{
await _device.WriteSingleCoilAsync(_slaveAddress, address, true, ct);
await WriteSingleCoilAsync(_slaveAddress, address, true, ct);
await Task.Delay(100, ct);
await _device.WriteSingleCoilAsync(_slaveAddress, address, false, ct);
await WriteSingleCoilAsync(_slaveAddress, address, false, ct);
}
/// <summary>
/// 将 32 位整型数据转换为 Modbus 的 2 个 16 位无符号整数(低字在前 CDAB 格式)
/// </summary>
protected ushort[] Int32ToUshorts(int value)
private ushort[] Int32ToUshorts(int value)
{
ushort lowWord = (ushort)(value & 0xFFFF);
ushort highWord = (ushort)((value >> 16) & 0xFFFF);
@@ -486,7 +478,7 @@ namespace BenchMovementModule.HardwareDrive
/// <summary>
/// 将 Modbus 的 2 个 16 位无符号整数转换为 32 位整型(低字在前 CDAB 格式)
/// </summary>
protected int UshortsToInt32(ushort[] registers)
private int UshortsToInt32(ushort[] registers)
{
uint lowWord = registers[0];
uint highWord = registers[1];
@@ -514,7 +506,7 @@ namespace BenchMovementModule.HardwareDrive
_ => throw new ArgumentException("轴编号错误,仅支持 1-6", nameof(axis))
};
ushort[] registers = await _device.ReadHoldingRegistersAsync(_slaveAddress, address, 2, ct);
ushort[] registers = await ReadHoldingRegistersAsync(_slaveAddress, address, 2, ct);
if (registers == null || registers.Length < 2)
throw new InvalidOperationException($"读取轴 {axis} 绝对位置失败。");
@@ -528,7 +520,7 @@ namespace BenchMovementModule.HardwareDrive
public async Task<(int Axis1, int Axis2, int Axis3, int Axis4, int Axis5, int Axis6)> GetAllAbsolutePositionsAsync(CancellationToken ct = default)
{
// 从 47232 开始连续读取 22 个寄存器(覆盖 47232-47253
ushort[] registers = await _device.ReadHoldingRegistersAsync(_slaveAddress, ADDR_ABS_AXIS1_POS, 22, ct);
ushort[] registers = await ReadHoldingRegistersAsync(_slaveAddress, ADDR_ABS_AXIS1_POS, 22, ct);
if (registers == null || registers.Length < 22)
throw new InvalidOperationException("批量读取 6 轴绝对位置失败,返回数据长度不足。");
@@ -545,4 +537,4 @@ namespace BenchMovementModule.HardwareDrive
#endregion
}
}
}

View File

@@ -9,7 +9,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeviceCommand.Device
namespace DeviceCommand.Devices
{
//[ADPCommand]
public class IOBoard : ModbusTcp

View File

@@ -5,7 +5,7 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using DeviceCommand.Base;
using DeviceCommand.Device;
using DeviceCommand.Devices;
using System.ComponentModel;
namespace DeviceCommand.Devices

View File

@@ -6,7 +6,7 @@ using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
namespace DeviceCommand.Device
namespace DeviceCommand.Devices
{
[ADPCommand]
public class IT7800E : Tcp

View File

@@ -6,7 +6,7 @@ using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
namespace DeviceCommand.Device
namespace DeviceCommand.Devices
{
//[ADPCommand]
public class N36200 : Tcp

View File

@@ -7,7 +7,7 @@ using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
namespace DeviceCommand.Device
namespace DeviceCommand.Devices
{
[ADPCommand]
public class N36600 : Tcp

View File

@@ -6,7 +6,7 @@ using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
namespace DeviceCommand.Device
namespace DeviceCommand.Devices
{
[ADPCommand]
public class N69200 : Tcp

View File

@@ -7,7 +7,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DeviceCommand.Device
namespace DeviceCommand.Devices
{
[ADPCommand]
public class SDS2000X_HD : Tcp

View File

@@ -6,7 +6,7 @@ using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
namespace DeviceCommand.Device
namespace DeviceCommand.Devices
{
[ADPCommand]
public class SPAW7000 : Tcp

View File

@@ -1,4 +1,4 @@
using DeviceCommand.Device;
using DeviceCommand.Devices;
using Prism.Commands;
using Prism.Ioc;
using System;

View File

@@ -1,4 +1,4 @@
using DeviceCommand.Device;
using DeviceCommand.Devices;
using Prism.Commands;
using Prism.Ioc;
using System;

View File

@@ -1,4 +1,4 @@
using DeviceCommand.Device;
using DeviceCommand.Devices;
using Prism.Commands;
using Prism.Ioc;
using System;

View File

@@ -1,4 +1,4 @@
using DeviceCommand.Device;
using DeviceCommand.Devices;
using Prism.Commands;
using Prism.Ioc;
using System;

View File

@@ -1,4 +1,4 @@
using DeviceCommand.Device;
using DeviceCommand.Devices;
using Prism.Commands;
using Prism.Ioc;
using System;
@@ -9,7 +9,7 @@ using System.Threading.Tasks;
using System.Windows.Input;
using UIShare.GlobalVariable;
using UIShare.ViewModelBase;
using static DeviceCommand.Device.SDS2000X_HD;
using static DeviceCommand.Devices.SDS2000X_HD;
namespace DeviceEditModule.ViewModels
{

View File

@@ -1,4 +1,4 @@
using DeviceCommand.Device;
using DeviceCommand.Devices;
using Prism.Commands;
using Prism.Ioc;
using System;

View File

@@ -1,5 +1,5 @@
using DeviceCommand.Base;
using DeviceCommand.Device;
using DeviceCommand.Devices;
using MainModule.Views;
using System.Reflection;
using UIShare.GlobalVariable;

View File

@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Windows.Input;
using DeviceCommand.Device;
using DeviceCommand.Devices;
using Logger;
using Prism.Ioc;

View File

@@ -333,7 +333,7 @@ namespace TestingModule.ViewModels
foreach (var type in validTypes)
{
//拦截没有在设备列表中的设备类型
//if (_systemConfig.DeviceList.Where(x=>x.Remark==type.Name).ToList().Count==0 && type.FullName.Contains("DeviceCommand.Device"))
//if (_systemConfig.DeviceList.Where(x=>x.Remark==type.Name).ToList().Count==0 && type.FullName.Contains("DeviceCommand.Devices"))
//{
// continue;
//}

View File

@@ -1,5 +1,4 @@
using DeviceCommand.Base;
using DeviceCommand.Device;
using DeviceCommand.Devices;
using Logger;
using Model.Models;

View File

@@ -5,6 +5,8 @@ using System.Collections.Generic;
namespace UIShare.UIViewModel
{
public class ParameterVM : BindableBase
{
#region