alielie
2026-04-16 1afe548ac40cc7f975917ef9bf2e156595cf81f3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using HMI.Core.Util;
using HMI.UI.DpUtil;
using HMI.UI.Entity;
using HMI.UI.Util;
using HMI.Util;
using Panuon.WPF.UI;
using System;
using System.ComponentModel;
using System.IO.Ports;
 
namespace HMI.UI
{
    /// <summary>
    /// ScannerSetting.xaml 的交互逻辑
    /// </summary>
    public partial class ScannerSetting : WindowX
    {
        public ScannerSetting()
        {
            DataContext = this;
            InitializeComponent();
        }
 
        public string IniFile { get; set; }
        public ScannerConfig Data { get; set; }
 
        private bool _isProcessing;
 
        protected override void OnClosing(CancelEventArgs e)
        {
            //Press Continue Button
            if (DialogResult == true)
            {
                if (_isProcessing)
                {
                    e.Cancel = true;
                    return;
                }
                var okButton = WindowXModalDialog.GetOKButton(this);
                try
                {
                    _isProcessing = true;
                    ButtonHelper.SetIsPending(okButton, true);
                    if (!ValidateForm())
                    {
                        e.Cancel = true;
                        return;
                    }
 
                    SaveOrUpdateData();
                }
                catch (Exception ex)
                {
                    ToastHelper.Error(this, $"Error!{ex.Message}");
                    e.Cancel = true;
                    return;
                }
                finally
                {
                    _isProcessing = false;
                    ButtonHelper.SetIsPending(okButton, false);
                }
            }
            base.OnClosing(e);
        }
 
 
        private bool ValidateForm()
        {
            bool flag = FormGroupHelper.ValidateForm(formPanel);
            if (flag)
            {
                Data.PortName = CbPortName.SelectedItem?.ToString();
                Data.BaudRate = (int)(NiBaudRate.Value ?? 0);
                Data.DataBits = (int)(NiDataBits.Value ?? 0);
                Data.StopBits = (StopBits)CbStopBits.SelectedItem;
                Data.DtrEnable = CxDtrEnable.IsChecked ?? false;
                Data.RtsEnable = CxRtsEnable.IsChecked ?? false;
            }
            return flag;
        }
 
        private bool _isEnabled = true;
        public bool SbIsEnabled
        {
            get => _isEnabled;
            set
            {
                Set(ref _isEnabled, value);
            }
        }
 
        private string section;
        public bool? Open(bool showPortName, string section, string iniFile)
        {
            IniFile = iniFile;
            string[] ports = SerialPort.GetPortNames();
            CbPortName.ItemsSource = ports;
 
            this.section = section;
            this.Title = section == "Config" ? this.lang("Tr.CommonSetting") : (section + this.lang("Tr.ScannerSetting"));
            if (showPortName)
            {
                FgPortName.Visibility = System.Windows.Visibility.Visible;
                FormGroupHelper.SetIsRequired(FgPortName, true);
                SbIsEnabled = true;
            }
            else
            {
                FgPortName.Visibility = System.Windows.Visibility.Collapsed;
                FormGroupHelper.SetIsRequired(FgPortName, false);
                SbIsEnabled = true;
            }
 
            var data = ScannerConfig.ReadFromIni(IniFile, "Config");
            string portName = IniFileHelper.ReadIniData(section, "PortName", "", IniFile);
            if (string.IsNullOrEmpty(portName))
            {
                if (section.StartsWith("A"))
                {
                    portName = "COM1";
                }
                else
                {
                    portName = "COM2";
                }
            }
            data.PortName = portName;
 
            Data = data;
            CbPortName.SelectedItem = Data.PortName;
            NiBaudRate.Value = Data.BaudRate;
            NiDataBits.Value = Data.DataBits;
            CbStopBits.SelectedItem = Data.StopBits;
            CxDtrEnable.IsChecked = Data.DtrEnable;
            CxRtsEnable.IsChecked = Data.RtsEnable;
            return this.ShowDialog();
        }
 
        private void SaveOrUpdateData()
        {
            IniFileHelper.WriteIniData(section, "PortName", Data.PortName, IniFile);
            if (section == "Config")
            {
                IniFileHelper.WriteIniData(section, "BaudRate", Data.BaudRate.ToString(), IniFile);
                IniFileHelper.WriteIniData(section, "DataBits", Data.DataBits.ToString(), IniFile);
                IniFileHelper.WriteIniData(section, "StopBits", Data.StopBits.ToString(), IniFile);
                IniFileHelper.WriteIniData(section, "DtrEnable", Data.DtrEnable.ToString(), IniFile);
                IniFileHelper.WriteIniData(section, "RtsEnable", Data.RtsEnable.ToString(), IniFile);
            }
        }
    }
}