Keine Beschreibung

Settings.xaml.cs 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.IO.Ports;
  14. using System.Configuration;
  15. using System.Data.SqlClient;
  16. using System.Data;
  17. using System.IO;
  18. using System.Runtime.Serialization.Formatters.Binary;
  19. namespace WpfApplication19
  20. {
  21. /// <summary>
  22. /// Interaction logic for Settings.xaml
  23. /// </summary>
  24. public partial class Settings : Window
  25. {
  26. SerialPort comport = new SerialPort();
  27. MainWindow _win;
  28. int _currentMachine;
  29. public Settings()
  30. {
  31. InitializeComponent();
  32. }
  33. private void init(object sender, RoutedEventArgs e)
  34. {
  35. _win = (MainWindow)Application.Current.MainWindow;
  36. //Console.WriteLine("hello");
  37. ComboBoxItem cb = new ComboBoxItem();
  38. cb.Tag = "None";
  39. cb.Content = "Select COM Port";
  40. //COMPorts.Items.Add(cb);
  41. Dictionary<string, string> friendlyPorts = Utils.BuildPortNameHash(SerialPort.GetPortNames());
  42. foreach (KeyValuePair<string, string> kvp in friendlyPorts)
  43. {
  44. //Console.WriteLine("Port '{0}' is better known as '{1}'", kvp.Key, kvp.Value);
  45. cb = new ComboBoxItem();
  46. cb.Tag = kvp.Key;
  47. cb.Content = "Port " + kvp.Key + " " + kvp.Value;
  48. COMPorts.Items.Add(cb);
  49. }
  50. foreach (ComboBoxItem item in COMPorts.Items)
  51. {
  52. if (item.Tag.ToString() == Utils.getSetting("machinePort"))
  53. {
  54. COMPorts.SelectedItem = item;
  55. //MessageBox.Show("help");
  56. }
  57. }
  58. //configDbCs.Text = Utils.getSetting("configDbCS");
  59. //masterDbCs.Text = Utils.getSetting("masterDbCS");
  60. //transDbCs.Text = Utils.getSetting("tranDbCS");
  61. productionDbCS.Text = Utils.getSetting("productionDbCS");
  62. //webServerURL.Text = Utils.getSetting("webServerURL");
  63. /*
  64. var baudRateValue = Utils.getSetting("baudRate");
  65. var dataBitsValue = Utils.getSetting("dataBits");
  66. var stopBitsValue = Utils.getSetting("stopBits");
  67. var parityValue = Utils.getSetting("parity");*/
  68. _currentMachine = Convert.ToInt32(Utils.getSetting("currentMachine"));
  69. /*
  70. setSelectBox(baudRateValue, baudRate);
  71. setSelectBox(dataBitsValue, dataBits);
  72. setSelectBox(stopBitsValue, stopBits);
  73. setSelectBox(parityValue, parity);
  74. */
  75. //Console.ReadLine();
  76. initMachineList();
  77. }
  78. void initMachineList()
  79. {
  80. SqlDataReader reader = Utils.Query(_win.proConn, "select * from machines");
  81. while (reader.Read())
  82. {
  83. var id = Convert.ToInt32(reader["id"]);
  84. var cbi = new ComboBoxItem() { Tag = id, Content = reader["name"].ToString() };
  85. MachineCB.Items.Add(cbi);
  86. if (id == _currentMachine)
  87. {
  88. MachineCB.SelectedItem = cbi;
  89. }
  90. }
  91. reader.Close();
  92. }
  93. private static void setSelectBox(string v, ComboBox cb)
  94. {
  95. foreach (ComboBoxItem item in cb.Items)
  96. {
  97. if (item.Content.ToString() == v)
  98. {
  99. cb.SelectedItem = item;
  100. //MessageBox.Show("help");
  101. }
  102. }
  103. }
  104. private void save_Click(object sender, RoutedEventArgs e)
  105. {
  106. ComboBoxItem cb = (ComboBoxItem)COMPorts.SelectedItem;
  107. if(cb != null){
  108. //debugText.Text = cb.Tag.ToString();
  109. Utils.UpdateSetting("machinePort",cb.Tag.ToString());
  110. }
  111. //Utils.UpdateSetting("configDbCS", configDbCs.Text);
  112. //Utils.UpdateSetting("masterDbCS", masterDbCs.Text);
  113. //Utils.UpdateSetting("tranDbCS", transDbCs.Text);
  114. Utils.UpdateSetting("productionDbCS", productionDbCS.Text);
  115. //Utils.UpdateSetting("baudRate", baudRate.Text);
  116. //Utils.UpdateSetting("dataBits", dataBits.Text);
  117. //Utils.UpdateSetting("stopBits", stopBits.Text);
  118. //Utils.UpdateSetting("parity", parity.Text);
  119. var cbi = MachineCB.SelectedItem as ComboBoxItem;
  120. Utils.UpdateSetting("currentMachine", cbi.Tag.ToString());
  121. //Utils.UpdateSetting("webServerURL", webServerURL.Text);
  122. this.Close();
  123. }
  124. private void testConnection_Click(object sender, RoutedEventArgs e)
  125. {
  126. connectDB();
  127. }
  128. private void connectDB()
  129. {
  130. }
  131. private void testConfig_Click(object sender, RoutedEventArgs e)
  132. {
  133. //testConnection(configDbCs.Text);
  134. }
  135. private void testConnection(string cs)
  136. {
  137. using (SqlConnection sqlConn = new SqlConnection())
  138. {
  139. try
  140. {
  141. sqlConn.ConnectionString = cs;
  142. sqlConn.Open();
  143. MessageBox.Show("Connectoin Success!!");
  144. }
  145. catch (SqlException se)
  146. {
  147. MessageBox.Show(se.Message);
  148. }
  149. }
  150. }
  151. private void portTest_Click(object sender, RoutedEventArgs e)
  152. {
  153. bool error = false;
  154. //comport.BaudRate = int.Parse(baudRate.Text);
  155. //comport.DataBits = int.Parse(dataBits.Text);
  156. //comport.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits.Text);
  157. //comport.Parity;
  158. ComboBoxItem cbi = (ComboBoxItem)COMPorts.SelectedItem;
  159. comport.PortName = cbi.Tag.ToString();
  160. Console.WriteLine("br : {0}", comport.BaudRate);
  161. Console.WriteLine("dbit: {0}", comport.DataBits);
  162. Console.WriteLine("stb: {0}", comport.StopBits);
  163. Console.WriteLine("par: {0}", comport.Parity);
  164. /*
  165. comport.BaudRate = int.Parse(baudRate.Text);
  166. comport.DataBits = int.Parse(dataBits.Text);
  167. comport.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits.Text);
  168. comport.Parity = (Parity)Enum.Parse(typeof(Parity), parity.Text);
  169. */
  170. var w = (MainWindow)Application.Current.MainWindow;
  171. if((w.comport.PortName == comport.PortName) && w.comport.IsOpen == true){
  172. MessageBox.Show("Open Port Success");
  173. return;
  174. }
  175. try
  176. {
  177. comport.Open();
  178. MessageBox.Show("Open Port Success");
  179. comport.Close();
  180. }
  181. catch (UnauthorizedAccessException) { error = true; }
  182. catch (IOException) { error = true; }
  183. catch (ArgumentException) { error = true; }
  184. if (error)
  185. {
  186. MessageBox.Show("Open Port Fail");
  187. }
  188. }
  189. private void testPro_Click(object sender, RoutedEventArgs e)
  190. {
  191. testConnection(productionDbCS.Text);
  192. }
  193. private void cancelBtn_Click(object sender, RoutedEventArgs e)
  194. {
  195. this.Close();
  196. }
  197. }
  198. }