Jelajahi Sumber

新增PLC地址配置及系统参数写入功能

本次提交新增了PLC地址配置相关模型(PlcAddressConfig、PlcAddress),并在配置服务中实现了PLC地址的加载与获取接口。管理模块增加了系统参数批量写入PLC的异步方法,设置界面保存参数时同步下发至PLC。项目文件已包含新模型类。此改动提升了系统参数与PLC的联动和同步能力。
孝锋 徐 8 bulan lalu
induk
melakukan
7ba17a20cc

+ 1 - 2
TeamAAS-VM/App.xaml.cs

@@ -96,8 +96,7 @@ namespace TeamAAS_VP
             containerRegistry.RegisterForNavigation<CalibrationBasics>();
             containerRegistry.RegisterForNavigation<CalibrationCameraParams>();
             containerRegistry.RegisterForNavigation<CalibrationEditVision>();
-            containerRegistry.RegisterForNavigation<CalibrationHome>
-();
+            containerRegistry.RegisterForNavigation<CalibrationHome>();
             containerRegistry.RegisterForNavigation<CalibrationResult>();
             containerRegistry.RegisterForNavigation<CalibrationSelectTool>();
             containerRegistry.RegisterForNavigation<CalibrationTeachCenterPoint>();

+ 26 - 0
TeamAAS-VM/Core/Management.cs

@@ -290,6 +290,7 @@ namespace TeamAAS_VP.Core
                     SendTaskMessage($"{item.Name}{Lang.断开连接}!", MessageLevel.Alarm);
                 }
             }
+            //await WriteSystemParameterToPlcAsync(_configService.GetSystemConfiguration());
             if (sb.Length == 0)
             {
                 return (true, "");
@@ -830,6 +831,31 @@ namespace TeamAAS_VP.Core
         }
         #endregion
 
+
+        #region PLC
+        /// <summary>
+        /// 将系统参数写入PLC
+        /// </summary>
+        /// <param name="systemConfiguration"></param>
+        /// <returns></returns>
+        public async Task WriteSystemParameterToPlcAsync(SystemConfiguration systemConfiguration)
+        {
+            var addressConfig = _configService.GetPlcAddresses();
+            var plc=_plcService.GetPlcByNumber(addressConfig.PlcNo) as OpcUaClientPLC;
+            Dictionary<string,object> nodeValues = new Dictionary<string, object>();
+            nodeValues.Add(addressConfig.CameraShotCount.Address, systemConfiguration.CameraShotCount);
+            nodeValues.Add(addressConfig.UpperCameraPickShotCount.Address, systemConfiguration.UpperCameraPickShotCount);
+            nodeValues.Add(addressConfig.UpperCameraPlaceShotCount.Address, systemConfiguration.UpperCameraPlaceShotCount);
+            nodeValues.Add(addressConfig.UpperCameraLockAssembleCount.Address, systemConfiguration.UpperCameraLockAssembleCount);
+            nodeValues.Add(addressConfig.PickFixedPositionProductCameraCount.Address, systemConfiguration.PickFixedPositionProductCameraCount);
+            nodeValues.Add(addressConfig.LockFixedPositionProductCameraCount.Address, systemConfiguration.LockFixedPositionProductCameraCount);
+            nodeValues.Add(addressConfig.FixedCameraDetectLockAssembleCount.Address, systemConfiguration.FixedCameraDetectLockAssembleCount);
+
+            await plc.WriteNodesAsync(nodeValues);
+        }
+
+        #endregion
+
         #region 方法
 
         private void SendTaskMessage(string msg, MessageLevel level)

+ 8 - 0
TeamAAS-VM/Interfaces/IConfigService.cs

@@ -205,6 +205,14 @@ namespace TeamAAS_VP.Interfaces
         /// <returns>存在则返回 true,否则返回 false。</returns>
         bool ContainsPlc(Guid id);
 
+        // Plc Addresses
+        /// <summary>
+        /// Get all PlcAddress entries
+        /// </summary>
+        /// <returns></returns>
+        PlcAddressConfig GetPlcAddresses();
+
+        
         // Background TCP
 
         /// <summary>

+ 33 - 0
TeamAAS-VM/Models/PLC/PlcAddress.cs

@@ -0,0 +1,33 @@
+using Prism.Mvvm;
+using System;
+
+namespace TeamAAS_VP.Models
+{
+    /// <summary>
+    /// Represent a PLC address entry mapping a variable name to its node/address string and type.
+    /// </summary>
+    public class PlcAddress : BindableBase
+    {
+
+        private int _PlcNo;
+        /// <summary>
+        /// PLC number used in system (int)
+        /// </summary>
+        public int PlcNo { get => _PlcNo; set => SetProperty(ref _PlcNo, value); }
+
+        private string _Address;
+        /// <summary>
+        /// Address or node string used to access PLC (e.g. OP-UA node id or Modbus address)
+        /// </summary>
+        public string Address { get => _Address; set => SetProperty(ref _Address, value); }
+
+        private string _DataType;
+        /// <summary>
+        /// Data type string (e.g. "Bool","Int32","Float","String")
+        /// </summary>
+        public string DataType { get => _DataType; set => SetProperty(ref _DataType, value); }
+
+        private string _Description;
+        public string Description { get => _Description; set => SetProperty(ref _Description, value); }
+    }
+}

+ 89 - 0
TeamAAS-VM/Models/PlcAddressConfig.cs

@@ -0,0 +1,89 @@
+using Prism.Mvvm;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace TeamAAS_VP.Models
+{
+    public class PlcAddressConfig : BindableBase
+    {
+        private int _PlcNo;
+        public int PlcNo
+        {
+            get { return _PlcNo; }
+            set { SetProperty(ref _PlcNo, value); }
+        }
+
+        private PlcAddress _cameraShotCount = new PlcAddress();
+        /// <summary>
+        /// 相机拍照次数
+        /// </summary>
+        public PlcAddress CameraShotCount
+        {
+            get => _cameraShotCount;
+            set => SetProperty(ref _cameraShotCount, value);
+        }
+
+        private PlcAddress _upperCameraPickShotCount = new PlcAddress();
+        /// <summary>
+        /// 上相机取料拍照次数
+        /// </summary>
+        public PlcAddress UpperCameraPickShotCount
+        {
+            get => _upperCameraPickShotCount;
+            set => SetProperty(ref _upperCameraPickShotCount, value);
+        }
+
+        private PlcAddress _upperCameraPlaceShotCount = new PlcAddress();
+        /// <summary>
+        /// 上相机放料拍照次数
+        /// </summary>
+        public PlcAddress UpperCameraPlaceShotCount
+        {
+            get => _upperCameraPlaceShotCount;
+            set => SetProperty(ref _upperCameraPlaceShotCount, value);
+        }
+
+        private PlcAddress _upperCameraLockAssembleCount = new PlcAddress();
+        /// <summary>
+        /// 上相机锁附/组装次数
+        /// </summary>
+        public PlcAddress UpperCameraLockAssembleCount
+        {
+            get => _upperCameraLockAssembleCount;
+            set => SetProperty(ref _upperCameraLockAssembleCount, value);
+        }
+
+        private PlcAddress _pickFixedPositionProductCameraCount = new PlcAddress();
+        /// <summary>
+        /// 取料固定位置拍产品相机数量
+        /// </summary>
+        public PlcAddress PickFixedPositionProductCameraCount
+        {
+            get => _pickFixedPositionProductCameraCount;
+            set => SetProperty(ref _pickFixedPositionProductCameraCount, value);
+        }
+
+        private PlcAddress _lockFixedPositionProductCameraCount = new PlcAddress();
+        /// <summary>
+        /// 锁附/组装固定位置拍产品相机数量
+        /// </summary>
+        public PlcAddress LockFixedPositionProductCameraCount
+        {
+            get => _lockFixedPositionProductCameraCount;
+            set => SetProperty(ref _lockFixedPositionProductCameraCount, value);
+        }
+
+        private PlcAddress _fixedCameraDetectLockAssembleCount = new PlcAddress();
+        /// <summary>
+        /// 固定相机检测锁附/组装次数
+        /// </summary>
+        public PlcAddress FixedCameraDetectLockAssembleCount
+        {
+            get => _fixedCameraDetectLockAssembleCount;
+            set => SetProperty(ref _fixedCameraDetectLockAssembleCount, value);
+        }
+    }
+}

+ 17 - 0
TeamAAS-VM/Services/ConfigService.cs

@@ -49,6 +49,8 @@ namespace TeamAAS_VP.Services
         // 光源控制器配置
         private ObservableCollection<LightControllerConfig> LightControllers { get; set; }
 
+        private PlcAddressConfig PlcAddressConfig { get; set; }
+
         /// <summary>
         /// 初始化一个新的 <see cref="ConfigService"/> 实例。
         /// 构造函数不会自动加载配置文件(避免在构造期间进行 I/O 操作);可调用 <see cref="LoadAll"/> 或 <see cref="LoadAllAsync"/> 显式加载。
@@ -131,6 +133,15 @@ namespace TeamAAS_VP.Services
                 {
                     LightControllers = FileHelper.ReadJsonFile<ObservableCollection<LightControllerConfig>>(ConfigPaths.LightControllersConfigurationPath);
                 }
+                // load plc addresses
+                var plcAddressPath = "..//Config//PlcAddressConfiguration.cfg";
+                if (File.Exists(plcAddressPath))
+                    PlcAddressConfig = FileHelper.ReadJsonFile<PlcAddressConfig>(plcAddressPath);
+                else
+                {
+                    PlcAddressConfig = new PlcAddressConfig();
+                    FileHelper.WriteJsonFile(PlcAddressConfig, plcAddressPath);
+                }
             }
         }
 
@@ -158,6 +169,8 @@ namespace TeamAAS_VP.Services
                 FileHelper.WriteJsonFile(BgCommunicate, ConfigPaths.BgTcpIpConfigurationPath);
                 FileHelper.WriteJsonFile(BgModbusCommunicate, ConfigPaths.BgModbusTcpConfigurationPath);
                 FileHelper.WriteJsonFile(LightControllers, ConfigPaths.LightControllersConfigurationPath);
+                // save plc addresses
+                //FileHelper.WriteJsonFile(PlcAddressConfig, "..//Config//PlcAddressConfiguration.cfg");
             }
         }
 
@@ -903,6 +916,10 @@ namespace TeamAAS_VP.Services
 
         #endregion
 
+        #region Plc Addresses
+        public PlcAddressConfig GetPlcAddresses() { lock (_sync) { return PlcAddressConfig; } }
+        #endregion
+
         /// <summary>
         /// 释放资源并保存当前内存中的所有配置到文件。
         /// </summary>

+ 2 - 0
TeamAAS-VM/TeamAAS-VP.csproj

@@ -594,6 +594,8 @@
     <Compile Include="Models\DataPoint.cs" />
     <Compile Include="Models\Lights\ChannelConfig.cs" />
     <Compile Include="Models\Lights\LightControllerConfig.cs" />
+    <Compile Include="Models\PlcAddressConfig.cs" />
+    <Compile Include="Models\PLC\PlcAddress.cs" />
     <Compile Include="Models\ProductionRecord.cs" />
     <Compile Include="Models\ProductionStatQuery.cs" />
     <Compile Include="Models\ProductionStatResult.cs" />

+ 1 - 0
TeamAAS-VM/ViewModels/MainWindowViewModel.cs

@@ -296,6 +296,7 @@ namespace TeamAAS_VP.ViewModels
             }));
             await management.InitPlc();
 
+
             //-------------------SETP 4: 初始化Robot-----------------------------------------------------------------------
             curvalue += 1;
             _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>

+ 1 - 0
TeamAAS-VM/ViewModels/SettingViewModel.cs

@@ -1272,6 +1272,7 @@ namespace TeamAAS_VP.ViewModels
                 sysConfig.LockFixedPositionProductCameraCount = FixedLockAssembleCameraCount;
                 sysConfig.FixedCameraDetectLockAssembleCount = FixedCameraDetectLockAssembleCount;
                 _configService.SaveSystemConfiguration(sysConfig);
+                await management. WriteSystemParameterToPlcAsync(sysConfig);
                 _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
             }
         }