alielie
2025-04-10 6bcafc823e836ecd5790f87178a906333699f868
Plugin.DOControl/DOControl.xaml.cs
@@ -1,4 +1,5 @@
using HMI.Base;
using ConnectionUtil;
using HMI.Base;
using HMI.Core.Setting;
using HMI.Core.Util;
using HMI.UI;
@@ -13,7 +14,6 @@
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
namespace Plugin.DOControl
@@ -26,7 +26,8 @@
        /// <summary>
        /// 实例化服务器接口函数
        /// </summary>
        private OpcInterface.OpcInterface lTSCADAApi = new OpcInterface.OpcInterface();
        //private OpcInterface.OpcInterface lTSCADAApi = new OpcInterface.OpcInterface();
        private PipeClient pipeClient = new PipeClient("DAService");
        private ObservableCollection<string> groups;
        public ObservableCollection<string> Groups
@@ -124,15 +125,14 @@
        private void LoadData()
        {
            ObservableCollection<DOControlData> datas = new ObservableCollection<DOControlData>();
            try
            {
                motorMonitorTimer.Enabled = false;
                ObservableCollection<DOControlData> datas = new ObservableCollection<DOControlData>();
                if (File.Exists(FilePath))
                {
                    datas = XmlHelper.DeserializeFromXml<ObservableCollection<DOControlData>>(FilePath);
                }
                Datasource = datas;
                GroupMap = datas.GroupBy(x => x.Group)
                   .ToDictionary(
                       group => group.Key,
@@ -157,6 +157,7 @@
            {
                motorMonitorTimer.Enabled = true;
            }
            Datasource = datas;
        }
        private void MotorMonitorTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
@@ -174,7 +175,7 @@
                        string value = GetPlcValue(data.Address);
                        switch (data.ControlType)
                        {
                            case ControlType.Button:
                            case ControlType.BOOL:
                                var color = Application.Current.FindResource("VerBar") as Brush;
                                if (value == data.PressDownValue)
                                {
@@ -183,12 +184,12 @@
                            ((Button)ui).Background = color;
                                ((Button)ui).Content = value;
                                break;
                            case ControlType.Int16:
                            case ControlType.Int32:
                            case ControlType.Real:
                            case ControlType.LReal:
                            case ControlType.String:
                            case ControlType.WString:
                            case ControlType.INT:
                            case ControlType.WORD:
                            case ControlType.REAL:
                            case ControlType.LREAL:
                            case ControlType.STRING:
                            case ControlType.WSTRING:
                                ((Button)ui).Content = value;
                                break;
                            default:
@@ -235,19 +236,14 @@
                        FrameworkElement ui = null;
                        switch (data.ControlType)
                        {
                            case ControlType.Button:
                            case ControlType.ClickButton:
                            case ControlType.BOOL:
                            case ControlType.PULSE:
                                ui = CreateButton(data);
                                break;
                            case ControlType.Int16:
                            case ControlType.Int32:
                            case ControlType.Real:
                            case ControlType.LReal:
                            case ControlType.String:
                            case ControlType.WString:
                                ui = CreateNumberInput(data);
                            case ControlType.TIME:
                                break;
                            default:
                                ui = CreateInput(data);
                                break;
                        }
                        if (ui != null)
@@ -294,11 +290,11 @@
                MinWidth = 180,
                MinHeight = 40
            };
            if (data.ControlType == ControlType.Button)
            if (data.ControlType == ControlType.BOOL)
            {
                btn.Click += ControlButton_Click;
            }
            else if (data.ControlType == ControlType.ClickButton)
            else if (data.ControlType == ControlType.PULSE)
            {
                btn.Content = data.Address;
                btn.PreviewMouseLeftButtonDown += Btn_MouseLeftButtonDown;
@@ -341,7 +337,7 @@
            }
        }
        private Button CreateNumberInput(DOControlData data)
        private Button CreateInput(DOControlData data)
        {
            Style btnStyle = Application.Current.FindResource("PrimaryButton") as Style;
            var tb = new Button
@@ -361,7 +357,7 @@
                return tb;
            }
            if (data.ControlType == ControlType.String || data.ControlType == ControlType.WString)
            if (data.ControlType == ControlType.STRING || data.ControlType == ControlType.WSTRING)
            {
                tb.Click += StringInput_Click;
            }
@@ -380,22 +376,25 @@
        /// <param name="value">值</param>
        private void SetPlcValue(string key, string value)
        {
            if (!lTSCADAApi.DoFctWriteSingle(key, value, out string errMsg))
            {
                throw new Exception(errMsg);
            }
            //if (!lTSCADAApi.DoFctWriteSingle(key, value, out string errMsg))
            //{
            //    ToastHelper.Error(this, errMsg);
            //}
            pipeClient.SendMessage("asdsadasda", out _);
        }
        private string GetPlcValue(string key)
        {
            if (lTSCADAApi.DoFctReadSingle(key, out string Val, out string errMsg))
            {
                return Val;
            }
            else
            {
            //if (lTSCADAApi.DoFctReadSingle(key, out string Val, out string errMsg))
            //{
            //    return Val;
            //}
            //else
            //{
            //    return null;
            //}
                return null;
            }
        }
        private void ControlButton_Click(object sender, RoutedEventArgs args)
@@ -437,7 +436,8 @@
            NumberPanel np = new NumberPanel
            {
                Title = data.Name,
                Owner = Window.GetWindow(this)
                Owner = Window.GetWindow(this),
                DotEnabled = data.ControlType == ControlType.REAL || data.ControlType == ControlType.LREAL
            };
            if (np.Open(tb.Content?.ToString()) == true)
            {
@@ -450,19 +450,34 @@
            LoadData();
            if (SystemSetting.Instance.PlcEnabled)
            {
                if (lTSCADAApi.Connect2("127.0.0.1", 6543, 18888, out string errMsg))
                //if (lTSCADAApi.Connect2("127.0.0.1", 6543, 18888, out string errMsg))
                //{
                //    motorMonitorTimer.Enabled = true;
                //    RefreshControl();
                //}
                //else
                //{
                //    MsgBoxHelper2.Error(errMsg);
                //}
                try
                {
                    motorMonitorTimer.Enabled = true;
                    RefreshControl();
                    pipeClient.OnConnected += PipeClient_OnConnected;
                    pipeClient.Connect();
                }
                else
                catch (Exception ex)
                {
                    MsgBoxHelper2.Error(errMsg);
                    MsgBoxHelper.Error(this, ex.Message);
                }
            }
            Loaded -= UserControl_Loaded;
        }
        private void PipeClient_OnConnected(object sender, EventArgs e)
        {
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;