MotorizedLightDialog.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.IO.Ports;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using PaintDotNet.Base.CommTool;
  13. using PaintDotNet.Base.SettingModel;
  14. using StageController;
  15. using StageController.M3H;
  16. namespace PaintDotNet.Hardware
  17. {
  18. public class MotorizedLightDialog : Form
  19. {
  20. #region
  21. /// <summary>
  22. /// Required designer variable.
  23. /// </summary>
  24. private System.ComponentModel.IContainer components = null;
  25. /// <summary>
  26. /// Clean up any resources being used.
  27. /// </summary>
  28. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  29. protected override void Dispose(bool disposing)
  30. {
  31. if (disposing && (components != null))
  32. {
  33. components.Dispose();
  34. }
  35. base.Dispose(disposing);
  36. }
  37. private void InitializeLanguageText()
  38. {
  39. this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
  40. this.btnClose.Text = PdnResources.GetString("Menu.File.Close.Text");
  41. this.btnCommit.Text = PdnResources.GetString("Menu.ensure.text");
  42. this.groupBox2.Text = PdnResources.GetString("Menu.Setting.Text");
  43. this.label2.Text = PdnResources.GetString("Menu.Baudrate.text") + ":";
  44. this.label1.Text = PdnResources.GetString("Menu.Theportnumber.Text") + ":";
  45. this.groupBox3.Text = PdnResources.GetString("Menu.Lightingsystem.Text");
  46. this.btnPolarized.Text = PdnResources.GetString("Menu.Polarizedlight.Text");
  47. this.btnBright.Text = PdnResources.GetString("Menu.Mingfield.Text");
  48. this.Text = PdnResources.GetString("Menu.HardwareControl.MotorizedLight.Text");
  49. }
  50. #region Windows Form Designer generated code
  51. /// <summary>
  52. /// Required method for Designer support - do not modify
  53. /// the contents of this method with the code editor.
  54. /// </summary>
  55. private void InitializeComponent()
  56. {
  57. this.groupBox1 = new System.Windows.Forms.GroupBox();
  58. this.btnClose = new System.Windows.Forms.Button();
  59. this.btnCommit = new System.Windows.Forms.Button();
  60. this.groupBox2 = new System.Windows.Forms.GroupBox();
  61. this.txtBaudRate = new System.Windows.Forms.TextBox();
  62. this.label2 = new System.Windows.Forms.Label();
  63. this.cmbPort = new System.Windows.Forms.ComboBox();
  64. this.label1 = new System.Windows.Forms.Label();
  65. this.groupBox3 = new System.Windows.Forms.GroupBox();
  66. this.btnBright = new System.Windows.Forms.Button();
  67. this.btnPolarized = new System.Windows.Forms.Button();
  68. this.groupBox1.SuspendLayout();
  69. this.groupBox2.SuspendLayout();
  70. this.groupBox3.SuspendLayout();
  71. this.SuspendLayout();
  72. //
  73. // groupBox1
  74. //
  75. this.groupBox1.Controls.Add(this.btnClose);
  76. this.groupBox1.Controls.Add(this.btnCommit);
  77. this.groupBox1.Location = new System.Drawing.Point(9, 8);
  78. this.groupBox1.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
  79. this.groupBox1.Name = "groupBox1";
  80. this.groupBox1.Padding = new System.Windows.Forms.Padding(2, 2, 2, 2);
  81. this.groupBox1.Size = new System.Drawing.Size(354, 80);
  82. this.groupBox1.TabIndex = 0;
  83. this.groupBox1.TabStop = false;
  84. this.groupBox1.Text = "操作";
  85. //
  86. // btnClose
  87. //
  88. this.btnClose.Location = new System.Drawing.Point(276, 28);
  89. this.btnClose.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
  90. this.btnClose.Name = "btnClose";
  91. this.btnClose.Size = new System.Drawing.Size(59, 29);
  92. this.btnClose.TabIndex = 1;
  93. this.btnClose.Text = "关闭";
  94. this.btnClose.UseVisualStyleBackColor = true;
  95. this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
  96. //
  97. // btnCommit
  98. //
  99. this.btnCommit.Location = new System.Drawing.Point(192, 28);
  100. this.btnCommit.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
  101. this.btnCommit.Name = "btnCommit";
  102. this.btnCommit.Size = new System.Drawing.Size(59, 29);
  103. this.btnCommit.TabIndex = 0;
  104. this.btnCommit.Text = "确定";
  105. this.btnCommit.UseVisualStyleBackColor = true;
  106. this.btnCommit.Click += new System.EventHandler(this.btnCommit_Click);
  107. //
  108. // groupBox2
  109. //
  110. this.groupBox2.Controls.Add(this.txtBaudRate);
  111. this.groupBox2.Controls.Add(this.label2);
  112. this.groupBox2.Controls.Add(this.cmbPort);
  113. this.groupBox2.Controls.Add(this.label1);
  114. this.groupBox2.Location = new System.Drawing.Point(9, 93);
  115. this.groupBox2.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
  116. this.groupBox2.Name = "groupBox2";
  117. this.groupBox2.Padding = new System.Windows.Forms.Padding(2, 2, 2, 2);
  118. this.groupBox2.Size = new System.Drawing.Size(354, 80);
  119. this.groupBox2.TabIndex = 1;
  120. this.groupBox2.TabStop = false;
  121. this.groupBox2.Text = "设置";
  122. //
  123. // txtBaudRate
  124. //
  125. this.txtBaudRate.Location = new System.Drawing.Point(251, 35);
  126. this.txtBaudRate.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
  127. this.txtBaudRate.Name = "txtBaudRate";
  128. this.txtBaudRate.Size = new System.Drawing.Size(76, 21);
  129. this.txtBaudRate.TabIndex = 3;
  130. this.txtBaudRate.Text = "9600";
  131. this.txtBaudRate.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtBaudRate_KeyPress);
  132. //
  133. // label2
  134. //
  135. this.label2.AutoSize = true;
  136. this.label2.Location = new System.Drawing.Point(190, 39);
  137. this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
  138. this.label2.Name = "label2";
  139. this.label2.Size = new System.Drawing.Size(53, 12);
  140. this.label2.TabIndex = 2;
  141. this.label2.Text = "波特率:";
  142. //
  143. // cmbPort
  144. //
  145. this.cmbPort.FormattingEnabled = true;
  146. this.cmbPort.Location = new System.Drawing.Point(69, 37);
  147. this.cmbPort.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
  148. this.cmbPort.Name = "cmbPort";
  149. this.cmbPort.Size = new System.Drawing.Size(92, 20);
  150. this.cmbPort.TabIndex = 1;
  151. //
  152. // label1
  153. //
  154. this.label1.AutoSize = true;
  155. this.label1.Location = new System.Drawing.Point(14, 39);
  156. this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
  157. this.label1.Name = "label1";
  158. this.label1.Size = new System.Drawing.Size(53, 12);
  159. this.label1.TabIndex = 0;
  160. this.label1.Text = "端口号:";
  161. //
  162. // groupBox3
  163. //
  164. this.groupBox3.Controls.Add(this.btnBright);
  165. this.groupBox3.Controls.Add(this.btnPolarized);
  166. this.groupBox3.Location = new System.Drawing.Point(9, 178);
  167. this.groupBox3.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
  168. this.groupBox3.Name = "groupBox3";
  169. this.groupBox3.Padding = new System.Windows.Forms.Padding(2, 2, 2, 2);
  170. this.groupBox3.Size = new System.Drawing.Size(354, 80);
  171. this.groupBox3.TabIndex = 2;
  172. this.groupBox3.TabStop = false;
  173. this.groupBox3.Text = "照明方式";
  174. //
  175. // btnBright
  176. //
  177. this.btnBright.Location = new System.Drawing.Point(213, 41);
  178. this.btnBright.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
  179. this.btnBright.Name = "btnBright";
  180. this.btnBright.Size = new System.Drawing.Size(56, 23);
  181. this.btnBright.TabIndex = 1;
  182. this.btnBright.Text = "明场";
  183. this.btnBright.UseVisualStyleBackColor = true;
  184. this.btnBright.Click += new System.EventHandler(this.btnBright_Click);
  185. //
  186. // btnPolarized
  187. //
  188. this.btnPolarized.Location = new System.Drawing.Point(69, 41);
  189. this.btnPolarized.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
  190. this.btnPolarized.Name = "btnPolarized";
  191. this.btnPolarized.Size = new System.Drawing.Size(56, 23);
  192. this.btnPolarized.TabIndex = 0;
  193. this.btnPolarized.Text = "偏光";
  194. this.btnPolarized.UseVisualStyleBackColor = true;
  195. this.btnPolarized.Click += new System.EventHandler(this.btnPolarized_Click);
  196. //
  197. // MotorizedLightDialog
  198. //
  199. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  200. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  201. this.ClientSize = new System.Drawing.Size(373, 267);
  202. this.Controls.Add(this.groupBox3);
  203. this.Controls.Add(this.groupBox2);
  204. this.Controls.Add(this.groupBox1);
  205. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  206. this.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
  207. this.MaximizeBox = false;
  208. this.MinimizeBox = false;
  209. this.Name = "MotorizedLightDialog";
  210. this.Text = "电动偏光";
  211. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MotorizedLightDialog_FormClosing);
  212. this.groupBox1.ResumeLayout(false);
  213. this.groupBox2.ResumeLayout(false);
  214. this.groupBox2.PerformLayout();
  215. this.groupBox3.ResumeLayout(false);
  216. this.ResumeLayout(false);
  217. }
  218. #endregion
  219. private System.Windows.Forms.GroupBox groupBox1;
  220. private System.Windows.Forms.Button btnClose;
  221. private System.Windows.Forms.Button btnCommit;
  222. private System.Windows.Forms.GroupBox groupBox2;
  223. private System.Windows.Forms.TextBox txtBaudRate;
  224. private System.Windows.Forms.Label label2;
  225. private System.Windows.Forms.ComboBox cmbPort;
  226. private System.Windows.Forms.Label label1;
  227. private System.Windows.Forms.GroupBox groupBox3;
  228. private System.Windows.Forms.Button btnBright;
  229. private System.Windows.Forms.Button btnPolarized;
  230. #endregion
  231. private MotorizedLightModel m_motorizedLightModel;
  232. private string m_portName;
  233. private int m_baudRate;
  234. private bool m_isOpen;
  235. public MotorizedLightDialog()
  236. {
  237. InitializeComponent();
  238. InitializeLanguageText();
  239. GetSerialLst();
  240. InitializeData();
  241. }
  242. private void InitializeData()
  243. {
  244. m_motorizedLightModel = Startup.instance.motorizedLightModel;
  245. m_portName = m_motorizedLightModel.PortName;
  246. m_baudRate = m_motorizedLightModel.BaudRate;
  247. cmbPort.SelectedItem = m_portName;
  248. txtBaudRate.Text = m_baudRate.ToString();
  249. }
  250. private void Open()
  251. {
  252. if (this.cmbPort.Text.Trim().Length == 0)
  253. {
  254. MessageBox.Show(PdnResources.GetString("Menu.Theportnumbercannobeempty.Text"));
  255. return;
  256. }
  257. if (this.txtBaudRate.Text.Trim().Length == 0)
  258. {
  259. MessageBox.Show(PdnResources.GetString("Menu.Baudrempty.text"));
  260. return;
  261. }
  262. try
  263. {
  264. m_portName = this.cmbPort.Text.Trim();
  265. m_baudRate = int.Parse(this.txtBaudRate.Text.Trim());
  266. //m_axisController.Open(m_portName, m_baudRate, Parity.None, 8, StopBits.One);
  267. m_isOpen = true;
  268. }
  269. catch (Exception ex)
  270. {
  271. m_isOpen = false;
  272. // 串口无法打开时,打开参数设置界面
  273. MessageBox.Show(ex.Message);
  274. }
  275. }
  276. private void GetSerialLst()
  277. {
  278. //清除cmbPort显示
  279. cmbPort.SelectedIndex = -1;
  280. cmbPort.Items.Clear();
  281. //获取串口列表
  282. string[] serialLst = SerialPort.GetPortNames();
  283. if (serialLst.Length > 0)
  284. {
  285. //取串口列表进行排序
  286. Array.Sort(serialLst);
  287. //将串口列表输出到comboBox
  288. cmbPort.Items.AddRange(serialLst);
  289. cmbPort.SelectedIndex = 0;
  290. }
  291. }
  292. public const int WM_DEVICE_CHANGE = 0x219; //设备改变
  293. public const int DBT_DEVICEARRIVAL = 0x8000; //设备插入
  294. public const int DBT_DEVICE_REMOVE_COMPLETE = 0x8004; //设备移除
  295. //消息处理
  296. protected override void WndProc(ref Message m)
  297. {
  298. switch (m.Msg) //判断消息类型
  299. {
  300. case WM_DEVICE_CHANGE: //设备改变消息
  301. {
  302. GetSerialLst(); //设备改变时重新获取串口列表
  303. }
  304. break;
  305. }
  306. base.WndProc(ref m);
  307. }
  308. private void CallbackDataReceived(CommandBase command, Result result, string msg)
  309. {
  310. //m_axisController.DataReceived -= new DataReceiveEvent(CallbackDataReceived);
  311. if (this == null || this.IsDisposed)
  312. {
  313. return;
  314. }
  315. switch (result)
  316. {
  317. case Result.ERR_PARSE:
  318. MessageBox.Show(PdnResources.GetString("Menu.Replydataarsingerror.Text")+":" + msg);
  319. return;
  320. case Result.ERR_TIMEOUT:
  321. MessageBox.Show(PdnResources.GetString("Menu.Theconsoleresponsetimeout.Text"));
  322. return;
  323. }
  324. if (command is CommandMotorizedLight)
  325. {
  326. MessageBox.Show(PdnResources.GetString("Menu.Successfullysaved.text"));
  327. }
  328. }
  329. private void btnClose_Click(object sender, EventArgs e)
  330. {
  331. this.Close();
  332. }
  333. private void MotorizedLightDialog_FormClosing(object sender, FormClosingEventArgs e)
  334. {
  335. }
  336. private void btnCommit_Click(object sender, EventArgs e)
  337. {
  338. this.Open();
  339. if (m_isOpen)
  340. {
  341. m_motorizedLightModel.PortName = m_portName;
  342. m_motorizedLightModel.BaudRate = m_baudRate;
  343. string stageModelXml = XmlSerializeHelper.XmlSerialize<MotorizedLightModel>(m_motorizedLightModel);
  344. string filePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\MotorizedLightModel.xml";
  345. if (!FileOperationHelper.WriteStringToFile(stageModelXml, filePath, FileMode.Create))
  346. {
  347. MessageBox.Show(PdnResources.GetString("Menu.Savefailed.text"));
  348. }
  349. else
  350. {
  351. MessageBox.Show(PdnResources.GetString("Menu.Successfullysaved.text"));
  352. }
  353. }
  354. }
  355. private void txtBaudRate_KeyPress(object sender, KeyPressEventArgs e)
  356. {
  357. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
  358. {
  359. e.Handled = true;
  360. return;
  361. }
  362. }
  363. private void btnPolarized_Click(object sender, EventArgs e)
  364. {
  365. this.Open();
  366. if (m_isOpen)
  367. {
  368. //m_axisController.DataReceived += new DataReceiveEvent(CallbackDataReceived);
  369. //CommandMotorizedLight command = new CommandMotorizedLight(LightType.POLARIZED);
  370. //m_axisController.AddCommand(command);
  371. }
  372. }
  373. private void btnBright_Click(object sender, EventArgs e)
  374. {
  375. this.Open();
  376. if (m_isOpen)
  377. {
  378. //m_axisController.DataReceived += new DataReceiveEvent(CallbackDataReceived);
  379. //CommandMotorizedLight command = new CommandMotorizedLight(LightType.BRIGHT);
  380. //m_axisController.AddCommand(command);
  381. }
  382. }
  383. }
  384. }