COMSetting.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. using System.Configuration;
  14. using AGV_WPF_Global;
  15. using System.Net;
  16. using System.Text.RegularExpressions;
  17. namespace AGV_WPF
  18. {
  19. /// <summary>
  20. /// COMSetting.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class COMSetting : Window
  23. {
  24. /// <summary>
  25. /// 串口设置页面构造函数
  26. /// </summary>
  27. public COMSetting()
  28. {
  29. InitializeComponent();
  30. }
  31. /// <summary>
  32. /// 确认修改消息响应
  33. /// </summary>
  34. /// <param name="sender"></param>
  35. /// <param name="e"></param>
  36. private void btnControl_Click(object sender, RoutedEventArgs e)
  37. {
  38. try
  39. {
  40. bool resetflag = false;
  41. Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  42. if (!cfa.AppSettings.Settings["ControlCOMBaudrate"].Value.Equals(cbControlcombaudrate.Text.Trim()))
  43. {
  44. cfa.AppSettings.Settings["ControlCOMBaudrate"].Value = cbControlcombaudrate.Text.Trim();
  45. resetflag = true;
  46. }
  47. if (!cfa.AppSettings.Settings["ControlCOMDataBits"].Value.Equals(cbControlcomdatabits.Text.Trim()))
  48. {
  49. cfa.AppSettings.Settings["ControlCOMDataBits"].Value = cbControlcomdatabits.Text.Trim();
  50. resetflag = true;
  51. }
  52. if (!cfa.AppSettings.Settings["ControlCOMStopBits"].Value.Equals(cbControlcomstopbits.Text.Trim()))
  53. {
  54. cfa.AppSettings.Settings["ControlCOMStopBits"].Value = cbControlcomstopbits.Text.Trim();
  55. resetflag = true;
  56. }
  57. if (!cfa.AppSettings.Settings["ControlCOMParity"].Value.Equals(cbControlcomparity.Text.Trim()))
  58. {
  59. cfa.AppSettings.Settings["ControlCOMParity"].Value = cbControlcomparity.Text.Trim();
  60. resetflag = true;
  61. }
  62. if (comboBox_Type.SelectedIndex != int.Parse(cfa.AppSettings.Settings["CommType"].Value))
  63. {
  64. if (comboBox_Type.Text == "方式1")
  65. {
  66. //GlobalPara.type = 0;
  67. }
  68. else if(comboBox_Type.Text == "方式2")
  69. {
  70. //GlobalPara.type = 1;
  71. }
  72. cfa.AppSettings.Settings["CommType"].Value = comboBox_Type.SelectedIndex.ToString();
  73. resetflag = true;
  74. }
  75. if (!GlobalPara.HostIP.Equals(comBox_IP.Text.Trim()) || IsChanged)
  76. {
  77. GlobalPara.HostIP = comBox_IP.Text;
  78. cfa.AppSettings.Settings["HostIP"].Value = comBox_IP.Text;
  79. /************************************************************************/
  80. /* 以下是多串口配置文件写入代码,先将原先的串口配置全部删除 */
  81. /************************************************************************/
  82. foreach (string key in cfa.AppSettings.Settings.AllKeys)
  83. {
  84. if (Regex.IsMatch(key, @"^ControlCOMName[1-9]$"))
  85. cfa.AppSettings.Settings.Remove(key);
  86. }
  87. for (int i = 0; i < lbPorts.Items.Count; i++)
  88. {
  89. string keyName = string.Format("ControlCOMName{0}", i + 1);
  90. cfa.AppSettings.Settings.Add(keyName, lbPorts.Items[i].ToString());
  91. }
  92. resetflag = true;
  93. }
  94. if (!GlobalPara.WcfServerIP.Equals(comboBox_WCFIP.Text.Trim()) )
  95. {
  96. GlobalPara.WcfServerIP = comboBox_WCFIP.Text;
  97. cfa.AppSettings.Settings["WcfServerIP"].Value = comboBox_WCFIP.Text;
  98. resetflag = true;
  99. }
  100. if (!GlobalPara.TryIP.Equals(comboBox_RemoteIP.Text.Trim()))
  101. {
  102. GlobalPara.TryIP = comboBox_RemoteIP.Text;
  103. cfa.AppSettings.Settings["TryIP"].Value = comboBox_RemoteIP.Text;
  104. resetflag = true;
  105. }
  106. if (GlobalPara.IsEnableRemote!=chBox_EnableRemote.IsChecked)
  107. {
  108. GlobalPara.IsEnableRemote = (bool)chBox_EnableRemote.IsChecked;
  109. cfa.AppSettings.Settings[GlobalPara.strEnableRemote].Value = GlobalPara.IsEnableRemote.ToString() ;
  110. resetflag = true;
  111. }
  112. if (resetflag)
  113. {
  114. MessageBox.Show("修改成功!重启软件生效。");
  115. this.Close();
  116. }
  117. else
  118. {
  119. /*if (!GlobalPara.HostIP.Equals(comBox_IP.Text.Trim()) || IsChanged)
  120. {
  121. GlobalPara.HostIP = comBox_IP.Text;
  122. cfa.AppSettings.Settings["HostIP"].Value = comBox_IP.Text;
  123. / ************************************************************************ /
  124. / * 以下是多串口配置文件写入代码,先将原先的串口配置全部删除 * /
  125. / ************************************************************************ /
  126. foreach (string key in cfa.AppSettings.Settings.AllKeys)
  127. {
  128. if(Regex.IsMatch(key,@"^ControlCOMName[1-9]$"))
  129. cfa.AppSettings.Settings.Remove(key);
  130. }
  131. for (int i = 0; i < lbPorts.Items.Count;i++ )
  132. {
  133. string keyName = string.Format("ControlCOMName{0}",i+1);
  134. cfa.AppSettings.Settings.Add(keyName, lbPorts.Items[i].ToString());
  135. }
  136. MessageBox.Show("修改成功!");
  137. this.Close();
  138. }*/
  139. }
  140. cfa.Save(ConfigurationSaveMode.Minimal, false);
  141. }
  142. catch (System.Exception ex)
  143. {
  144. MessageBox.Show("修改失败!"+ex.Message);
  145. }
  146. }
  147. #region 退出系统
  148. /// <summary>
  149. /// 退出系统
  150. /// </summary>
  151. /// <param name="sender"></param>
  152. /// <param name="e"></param>
  153. private void btnExit_Click(object sender, RoutedEventArgs e)
  154. {
  155. this.Close();
  156. }
  157. #endregion
  158. private void Window_Loaded(object sender, RoutedEventArgs e)
  159. {
  160. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  161. if (!config.AppSettings.Settings.AllKeys.Contains("CommType"))
  162. {
  163. config.AppSettings.Settings.Add("CommType", "");
  164. }
  165. if (!config.AppSettings.Settings.AllKeys.Contains("HostIP"))
  166. {
  167. config.AppSettings.Settings.Add("HostIP", "");
  168. }
  169. config.Save();
  170. string[] portList = System.IO.Ports.SerialPort.GetPortNames();
  171. Array.Sort(portList);
  172. for (int i = 0; i < portList.Length; i++)
  173. {
  174. //cbControlcomname.Items.Add(portList[i]);
  175. cbSerialPorts.Items.Add(portList[i]);
  176. }
  177. //获取配置文件中的串口号
  178. string[] configs = config.AppSettings.Settings.AllKeys;
  179. List<string> configPorts = new List<string>();
  180. for (int i = 0; i < configs.Length;i++ )
  181. {
  182. if (/*configs[i].Contains("ControlCOMName")&&*/Regex.IsMatch(configs[i], @"^ControlCOMName[1-9]$"))
  183. {
  184. configPorts.Add(configs[i]);
  185. }
  186. }
  187. configPorts.Sort();
  188. foreach (string str in configPorts)
  189. {
  190. //cbControlcomname.Items.Add(config.AppSettings.Settings[str].Value);
  191. lbPorts.Items.Add(config.AppSettings.Settings[str].Value);
  192. }
  193. foreach (IPAddress address in Dns.GetHostAddresses(Dns.GetHostName()))
  194. {
  195. comBox_IP.Items.Add(address.ToString());
  196. }
  197. foreach (IPAddress address in Dns.GetHostAddresses(Dns.GetHostName()))
  198. {
  199. comboBox_WCFIP.Items.Add(address.ToString());
  200. }
  201. foreach (IPAddress address in Dns.GetHostAddresses(Dns.GetHostName()))
  202. {
  203. comboBox_RemoteIP.Items.Add(address.ToString());
  204. }
  205. int temp=0;
  206. //cbControlcomname.Text = GlobalPara.Gcontrolcomname;
  207. cbControlcombaudrate.Text = ConfigurationManager.AppSettings["ControlCOMBaudrate"];
  208. cbControlcomdatabits.Text = ConfigurationManager.AppSettings["ControlCOMDataBits"];
  209. cbControlcomstopbits.Text = ConfigurationManager.AppSettings["ControlCOMStopBits"];
  210. cbControlcomparity.Text = ConfigurationManager.AppSettings["ControlCOMParity"];
  211. comBox_IP.Text = string.IsNullOrEmpty(ConfigurationManager.AppSettings["HostIP"]) ? comBox_IP.Items[0].ToString() : ConfigurationManager.AppSettings["HostIP"];
  212. comboBox_WCFIP.Text = string.IsNullOrEmpty(ConfigurationManager.AppSettings["WcfServerIP"]) ? comBox_IP.Items[0].ToString() : ConfigurationManager.AppSettings["WcfServerIP"];
  213. comboBox_RemoteIP.Text = string.IsNullOrEmpty(ConfigurationManager.AppSettings["TryIP"]) ? comBox_IP.Items[0].ToString() : ConfigurationManager.AppSettings["TryIP"];
  214. comboBox_Type.SelectedIndex = int.TryParse(ConfigurationManager.AppSettings["CommType"], out temp) ? temp : 0;
  215. GlobalPara.type = temp;
  216. if (comboBox_Type.Text == "方式1")//RS232
  217. {
  218. GlobalPara.type = 0;
  219. //cbControlcomname.IsEnabled = true;
  220. lbPorts.IsEnabled = true;
  221. cbSerialPorts.IsEnabled = true;
  222. cbControlcombaudrate.IsEnabled = true;
  223. cbControlcomdatabits.IsEnabled = true;
  224. cbControlcomstopbits.IsEnabled = true;
  225. cbControlcomparity.IsEnabled = true;
  226. comBox_IP.IsEnabled = false;
  227. }
  228. else if (comboBox_Type.Text == "方式2")
  229. {
  230. GlobalPara.type = 1;//网络通信
  231. //cbControlcomname.IsEnabled = false;
  232. lbPorts.IsEnabled = false;
  233. cbSerialPorts.IsEnabled = false;
  234. cbControlcombaudrate.IsEnabled = false;
  235. cbControlcomdatabits.IsEnabled = false;
  236. cbControlcomstopbits.IsEnabled = false;
  237. cbControlcomparity.IsEnabled = false;
  238. comBox_IP.IsEnabled = true;
  239. }
  240. chBox_EnableRemote.IsChecked = GlobalPara.IsEnableRemote;
  241. }
  242. private void comboBox_Type_DropDownClosed(object sender, EventArgs e)
  243. {
  244. if (comboBox_Type.Text == "方式1")//RS232
  245. {
  246. //GlobalPara.type = 0;
  247. //cbControlcomname.IsEnabled = true;
  248. lbPorts.IsEnabled = true;
  249. cbSerialPorts.IsEnabled = true;
  250. cbControlcombaudrate.IsEnabled = true;
  251. cbControlcomdatabits.IsEnabled = true;
  252. cbControlcomstopbits.IsEnabled = true;
  253. cbControlcomparity.IsEnabled = true;
  254. comBox_IP.IsEnabled = false;
  255. }
  256. else if (comboBox_Type.Text == "方式2")
  257. {
  258. //GlobalPara.type = 1;//网络通信
  259. //cbControlcomname.IsEnabled = false;
  260. lbPorts.IsEnabled = false;
  261. cbSerialPorts.IsEnabled = false;
  262. cbControlcombaudrate.IsEnabled = false;
  263. cbControlcomdatabits.IsEnabled = false;
  264. cbControlcomstopbits.IsEnabled = false;
  265. cbControlcomparity.IsEnabled = false;
  266. comBox_IP.IsEnabled = true;
  267. }
  268. }
  269. bool IsChanged = false;//串口列表是否变化标志
  270. private void btnAdd_Click(object sender, RoutedEventArgs e)
  271. {
  272. if (string.IsNullOrEmpty(cbSerialPorts.Text))
  273. {
  274. MessageBox.Show("此串口不能为空!");
  275. return;
  276. }
  277. for (int i = 0; i < lbPorts.Items.Count;i++ )
  278. {
  279. if (lbPorts.Items[i].ToString() == cbSerialPorts.Text)
  280. {
  281. MessageBox.Show("此串口已存在!");
  282. return;
  283. }
  284. }
  285. lbPorts.Items.Add(cbSerialPorts.Text);
  286. IsChanged = true;
  287. }
  288. private void btnDel_Click(object sender, RoutedEventArgs e)
  289. {
  290. if (lbPorts.SelectedIndex == -1)
  291. {
  292. MessageBox.Show("请选择需要删除的串口!");
  293. return;
  294. }
  295. lbPorts.Items.Remove(lbPorts.SelectedItem);
  296. IsChanged = true;
  297. }
  298. private void btnModify_Click(object sender, RoutedEventArgs e)
  299. {
  300. if (lbPorts.SelectedIndex == -1)
  301. {
  302. MessageBox.Show("请选择需要修改的串口!");
  303. return;
  304. }
  305. for (int i = 0; i < lbPorts.Items.Count; i++)
  306. {
  307. if (lbPorts.Items[i].ToString() == cbSerialPorts.Text)
  308. {
  309. MessageBox.Show("此串口已存在!");
  310. return;
  311. }
  312. }
  313. int index = lbPorts.Items.IndexOf(lbPorts.SelectedItem);
  314. lbPorts.Items[index] = cbSerialPorts.Text;
  315. IsChanged = true;
  316. }
  317. }
  318. }