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