123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using System.Configuration;
- using AGV_WPF_Global;
- using System.Net;
- using System.Text.RegularExpressions;
- namespace AGV_WPF
- {
- /// <summary>
- /// COMSetting.xaml 的交互逻辑
- /// </summary>
- public partial class COMSetting : Window
- {
- /// <summary>
- /// 串口设置页面构造函数
- /// </summary>
- public COMSetting()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 确认修改消息响应
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnControl_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- bool resetflag = false;
- Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (!cfa.AppSettings.Settings["ControlCOMBaudrate"].Value.Equals(cbControlcombaudrate.Text.Trim()))
- {
- cfa.AppSettings.Settings["ControlCOMBaudrate"].Value = cbControlcombaudrate.Text.Trim();
- resetflag = true;
- }
- if (!cfa.AppSettings.Settings["ControlCOMDataBits"].Value.Equals(cbControlcomdatabits.Text.Trim()))
- {
- cfa.AppSettings.Settings["ControlCOMDataBits"].Value = cbControlcomdatabits.Text.Trim();
- resetflag = true;
- }
- if (!cfa.AppSettings.Settings["ControlCOMStopBits"].Value.Equals(cbControlcomstopbits.Text.Trim()))
- {
- cfa.AppSettings.Settings["ControlCOMStopBits"].Value = cbControlcomstopbits.Text.Trim();
- resetflag = true;
- }
- if (!cfa.AppSettings.Settings["ControlCOMParity"].Value.Equals(cbControlcomparity.Text.Trim()))
- {
- cfa.AppSettings.Settings["ControlCOMParity"].Value = cbControlcomparity.Text.Trim();
- resetflag = true;
- }
- if (comboBox_Type.SelectedIndex != int.Parse(cfa.AppSettings.Settings["CommType"].Value))
- {
- if (comboBox_Type.Text == "方式1")
- {
- //GlobalPara.type = 0;
- }
- else if(comboBox_Type.Text == "方式2")
- {
- //GlobalPara.type = 1;
- }
- cfa.AppSettings.Settings["CommType"].Value = comboBox_Type.SelectedIndex.ToString();
- resetflag = true;
- }
- if (!GlobalPara.HostIP.Equals(comBox_IP.Text.Trim()) || IsChanged)
- {
- GlobalPara.HostIP = comBox_IP.Text;
- cfa.AppSettings.Settings["HostIP"].Value = comBox_IP.Text;
- /************************************************************************/
- /* 以下是多串口配置文件写入代码,先将原先的串口配置全部删除 */
- /************************************************************************/
- foreach (string key in cfa.AppSettings.Settings.AllKeys)
- {
- if (Regex.IsMatch(key, @"^ControlCOMName[1-9]$"))
- cfa.AppSettings.Settings.Remove(key);
- }
- for (int i = 0; i < lbPorts.Items.Count; i++)
- {
- string keyName = string.Format("ControlCOMName{0}", i + 1);
- cfa.AppSettings.Settings.Add(keyName, lbPorts.Items[i].ToString());
- }
- resetflag = true;
- }
- if (!GlobalPara.WcfServerIP.Equals(comboBox_WCFIP.Text.Trim()) )
- {
- GlobalPara.WcfServerIP = comboBox_WCFIP.Text;
- cfa.AppSettings.Settings["WcfServerIP"].Value = comboBox_WCFIP.Text;
-
- resetflag = true;
- }
- if (!GlobalPara.TryIP.Equals(comboBox_RemoteIP.Text.Trim()))
- {
- GlobalPara.TryIP = comboBox_RemoteIP.Text;
- cfa.AppSettings.Settings["TryIP"].Value = comboBox_RemoteIP.Text;
-
- resetflag = true;
- }
- if (GlobalPara.IsEnableRemote!=chBox_EnableRemote.IsChecked)
- {
- GlobalPara.IsEnableRemote = (bool)chBox_EnableRemote.IsChecked;
- cfa.AppSettings.Settings[GlobalPara.strEnableRemote].Value = GlobalPara.IsEnableRemote.ToString() ;
- resetflag = true;
- }
- if (resetflag)
- {
- MessageBox.Show("修改成功!重启软件生效。");
- this.Close();
- }
- else
- {
- /*if (!GlobalPara.HostIP.Equals(comBox_IP.Text.Trim()) || IsChanged)
- {
- GlobalPara.HostIP = comBox_IP.Text;
- cfa.AppSettings.Settings["HostIP"].Value = comBox_IP.Text;
- / ************************************************************************ /
- / * 以下是多串口配置文件写入代码,先将原先的串口配置全部删除 * /
- / ************************************************************************ /
- foreach (string key in cfa.AppSettings.Settings.AllKeys)
- {
- if(Regex.IsMatch(key,@"^ControlCOMName[1-9]$"))
- cfa.AppSettings.Settings.Remove(key);
- }
- for (int i = 0; i < lbPorts.Items.Count;i++ )
- {
- string keyName = string.Format("ControlCOMName{0}",i+1);
- cfa.AppSettings.Settings.Add(keyName, lbPorts.Items[i].ToString());
- }
- MessageBox.Show("修改成功!");
- this.Close();
- }*/
- }
- cfa.Save(ConfigurationSaveMode.Minimal, false);
- }
- catch (System.Exception ex)
- {
- MessageBox.Show("修改失败!"+ex.Message);
- }
- }
- #region 退出系统
- /// <summary>
- /// 退出系统
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnExit_Click(object sender, RoutedEventArgs e)
- {
- this.Close();
- }
- #endregion
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (!config.AppSettings.Settings.AllKeys.Contains("CommType"))
- {
- config.AppSettings.Settings.Add("CommType", "");
- }
- if (!config.AppSettings.Settings.AllKeys.Contains("HostIP"))
- {
- config.AppSettings.Settings.Add("HostIP", "");
- }
- config.Save();
- string[] portList = System.IO.Ports.SerialPort.GetPortNames();
- Array.Sort(portList);
- for (int i = 0; i < portList.Length; i++)
- {
- //cbControlcomname.Items.Add(portList[i]);
- cbSerialPorts.Items.Add(portList[i]);
- }
-
- //获取配置文件中的串口号
- string[] configs = config.AppSettings.Settings.AllKeys;
- List<string> configPorts = new List<string>();
-
- for (int i = 0; i < configs.Length;i++ )
- {
- if (/*configs[i].Contains("ControlCOMName")&&*/Regex.IsMatch(configs[i], @"^ControlCOMName[1-9]$"))
- {
- configPorts.Add(configs[i]);
- }
- }
- configPorts.Sort();
- foreach (string str in configPorts)
- {
- //cbControlcomname.Items.Add(config.AppSettings.Settings[str].Value);
- lbPorts.Items.Add(config.AppSettings.Settings[str].Value);
- }
- foreach (IPAddress address in Dns.GetHostAddresses(Dns.GetHostName()))
- {
- comBox_IP.Items.Add(address.ToString());
- }
- foreach (IPAddress address in Dns.GetHostAddresses(Dns.GetHostName()))
- {
- comboBox_WCFIP.Items.Add(address.ToString());
- }
- foreach (IPAddress address in Dns.GetHostAddresses(Dns.GetHostName()))
- {
- comboBox_RemoteIP.Items.Add(address.ToString());
- }
- int temp=0;
- //cbControlcomname.Text = GlobalPara.Gcontrolcomname;
- cbControlcombaudrate.Text = ConfigurationManager.AppSettings["ControlCOMBaudrate"];
- cbControlcomdatabits.Text = ConfigurationManager.AppSettings["ControlCOMDataBits"];
- cbControlcomstopbits.Text = ConfigurationManager.AppSettings["ControlCOMStopBits"];
- cbControlcomparity.Text = ConfigurationManager.AppSettings["ControlCOMParity"];
- comBox_IP.Text = string.IsNullOrEmpty(ConfigurationManager.AppSettings["HostIP"]) ? comBox_IP.Items[0].ToString() : ConfigurationManager.AppSettings["HostIP"];
- comboBox_WCFIP.Text = string.IsNullOrEmpty(ConfigurationManager.AppSettings["WcfServerIP"]) ? comBox_IP.Items[0].ToString() : ConfigurationManager.AppSettings["WcfServerIP"];
- comboBox_RemoteIP.Text = string.IsNullOrEmpty(ConfigurationManager.AppSettings["TryIP"]) ? comBox_IP.Items[0].ToString() : ConfigurationManager.AppSettings["TryIP"];
- comboBox_Type.SelectedIndex = int.TryParse(ConfigurationManager.AppSettings["CommType"], out temp) ? temp : 0;
- GlobalPara.type = temp;
- if (comboBox_Type.Text == "方式1")//RS232
- {
- GlobalPara.type = 0;
- //cbControlcomname.IsEnabled = true;
- lbPorts.IsEnabled = true;
- cbSerialPorts.IsEnabled = true;
- cbControlcombaudrate.IsEnabled = true;
- cbControlcomdatabits.IsEnabled = true;
- cbControlcomstopbits.IsEnabled = true;
- cbControlcomparity.IsEnabled = true;
- comBox_IP.IsEnabled = false;
- }
- else if (comboBox_Type.Text == "方式2")
- {
- GlobalPara.type = 1;//网络通信
- //cbControlcomname.IsEnabled = false;
- lbPorts.IsEnabled = false;
- cbSerialPorts.IsEnabled = false;
- cbControlcombaudrate.IsEnabled = false;
- cbControlcomdatabits.IsEnabled = false;
- cbControlcomstopbits.IsEnabled = false;
- cbControlcomparity.IsEnabled = false;
- comBox_IP.IsEnabled = true;
- }
- chBox_EnableRemote.IsChecked = GlobalPara.IsEnableRemote;
- }
- private void comboBox_Type_DropDownClosed(object sender, EventArgs e)
- {
- if (comboBox_Type.Text == "方式1")//RS232
- {
- //GlobalPara.type = 0;
- //cbControlcomname.IsEnabled = true;
- lbPorts.IsEnabled = true;
- cbSerialPorts.IsEnabled = true;
- cbControlcombaudrate.IsEnabled = true;
- cbControlcomdatabits.IsEnabled = true;
- cbControlcomstopbits.IsEnabled = true;
- cbControlcomparity.IsEnabled = true;
- comBox_IP.IsEnabled = false;
- }
- else if (comboBox_Type.Text == "方式2")
- {
- //GlobalPara.type = 1;//网络通信
- //cbControlcomname.IsEnabled = false;
- lbPorts.IsEnabled = false;
- cbSerialPorts.IsEnabled = false;
- cbControlcombaudrate.IsEnabled = false;
- cbControlcomdatabits.IsEnabled = false;
- cbControlcomstopbits.IsEnabled = false;
- cbControlcomparity.IsEnabled = false;
- comBox_IP.IsEnabled = true;
- }
- }
- bool IsChanged = false;//串口列表是否变化标志
- private void btnAdd_Click(object sender, RoutedEventArgs e)
- {
- if (string.IsNullOrEmpty(cbSerialPorts.Text))
- {
- MessageBox.Show("此串口不能为空!");
- return;
- }
- for (int i = 0; i < lbPorts.Items.Count;i++ )
- {
- if (lbPorts.Items[i].ToString() == cbSerialPorts.Text)
- {
- MessageBox.Show("此串口已存在!");
- return;
- }
- }
- lbPorts.Items.Add(cbSerialPorts.Text);
- IsChanged = true;
- }
- private void btnDel_Click(object sender, RoutedEventArgs e)
- {
- if (lbPorts.SelectedIndex == -1)
- {
- MessageBox.Show("请选择需要删除的串口!");
- return;
- }
- lbPorts.Items.Remove(lbPorts.SelectedItem);
- IsChanged = true;
- }
- private void btnModify_Click(object sender, RoutedEventArgs e)
- {
- if (lbPorts.SelectedIndex == -1)
- {
- MessageBox.Show("请选择需要修改的串口!");
- return;
- }
- for (int i = 0; i < lbPorts.Items.Count; i++)
- {
- if (lbPorts.Items[i].ToString() == cbSerialPorts.Text)
- {
- MessageBox.Show("此串口已存在!");
- return;
- }
- }
- int index = lbPorts.Items.IndexOf(lbPorts.SelectedItem);
- lbPorts.Items[index] = cbSerialPorts.Text;
- IsChanged = true;
- }
- }
- }
|