using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using PaintDotNet.Base.CommTool; using PaintDotNet.Base.SettingModel; using StageController; using StageController.M3H; namespace PaintDotNet.Hardware { public class MotorizedLightDialog : Form { #region /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } private void InitializeLanguageText() { this.groupBox1.Text = PdnResources.GetString("Menu.operation.text"); this.btnClose.Text = PdnResources.GetString("Menu.File.Close.Text"); this.btnCommit.Text = PdnResources.GetString("Menu.ensure.text"); this.groupBox2.Text = PdnResources.GetString("Menu.Setting.Text"); this.label2.Text = PdnResources.GetString("Menu.Baudrate.text") + ":"; this.label1.Text = PdnResources.GetString("Menu.Theportnumber.Text") + ":"; this.groupBox3.Text = PdnResources.GetString("Menu.Lightingsystem.Text"); this.btnPolarized.Text = PdnResources.GetString("Menu.Polarizedlight.Text"); this.btnBright.Text = PdnResources.GetString("Menu.Mingfield.Text"); this.Text = PdnResources.GetString("Menu.HardwareControl.MotorizedLight.Text"); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.btnClose = new System.Windows.Forms.Button(); this.btnCommit = new System.Windows.Forms.Button(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.txtBaudRate = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.cmbPort = new System.Windows.Forms.ComboBox(); this.label1 = new System.Windows.Forms.Label(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.btnBright = new System.Windows.Forms.Button(); this.btnPolarized = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.btnClose); this.groupBox1.Controls.Add(this.btnCommit); this.groupBox1.Location = new System.Drawing.Point(9, 8); this.groupBox1.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.groupBox1.Name = "groupBox1"; this.groupBox1.Padding = new System.Windows.Forms.Padding(2, 2, 2, 2); this.groupBox1.Size = new System.Drawing.Size(354, 80); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "操作"; // // btnClose // this.btnClose.Location = new System.Drawing.Point(276, 28); this.btnClose.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.btnClose.Name = "btnClose"; this.btnClose.Size = new System.Drawing.Size(59, 29); this.btnClose.TabIndex = 1; this.btnClose.Text = "关闭"; this.btnClose.UseVisualStyleBackColor = true; this.btnClose.Click += new System.EventHandler(this.btnClose_Click); // // btnCommit // this.btnCommit.Location = new System.Drawing.Point(192, 28); this.btnCommit.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.btnCommit.Name = "btnCommit"; this.btnCommit.Size = new System.Drawing.Size(59, 29); this.btnCommit.TabIndex = 0; this.btnCommit.Text = "确定"; this.btnCommit.UseVisualStyleBackColor = true; this.btnCommit.Click += new System.EventHandler(this.btnCommit_Click); // // groupBox2 // this.groupBox2.Controls.Add(this.txtBaudRate); this.groupBox2.Controls.Add(this.label2); this.groupBox2.Controls.Add(this.cmbPort); this.groupBox2.Controls.Add(this.label1); this.groupBox2.Location = new System.Drawing.Point(9, 93); this.groupBox2.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.groupBox2.Name = "groupBox2"; this.groupBox2.Padding = new System.Windows.Forms.Padding(2, 2, 2, 2); this.groupBox2.Size = new System.Drawing.Size(354, 80); this.groupBox2.TabIndex = 1; this.groupBox2.TabStop = false; this.groupBox2.Text = "设置"; // // txtBaudRate // this.txtBaudRate.Location = new System.Drawing.Point(251, 35); this.txtBaudRate.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.txtBaudRate.Name = "txtBaudRate"; this.txtBaudRate.Size = new System.Drawing.Size(76, 21); this.txtBaudRate.TabIndex = 3; this.txtBaudRate.Text = "9600"; this.txtBaudRate.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtBaudRate_KeyPress); // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(190, 39); this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(53, 12); this.label2.TabIndex = 2; this.label2.Text = "波特率:"; // // cmbPort // this.cmbPort.FormattingEnabled = true; this.cmbPort.Location = new System.Drawing.Point(69, 37); this.cmbPort.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.cmbPort.Name = "cmbPort"; this.cmbPort.Size = new System.Drawing.Size(92, 20); this.cmbPort.TabIndex = 1; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(14, 39); this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(53, 12); this.label1.TabIndex = 0; this.label1.Text = "端口号:"; // // groupBox3 // this.groupBox3.Controls.Add(this.btnBright); this.groupBox3.Controls.Add(this.btnPolarized); this.groupBox3.Location = new System.Drawing.Point(9, 178); this.groupBox3.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.groupBox3.Name = "groupBox3"; this.groupBox3.Padding = new System.Windows.Forms.Padding(2, 2, 2, 2); this.groupBox3.Size = new System.Drawing.Size(354, 80); this.groupBox3.TabIndex = 2; this.groupBox3.TabStop = false; this.groupBox3.Text = "照明方式"; // // btnBright // this.btnBright.Location = new System.Drawing.Point(213, 41); this.btnBright.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.btnBright.Name = "btnBright"; this.btnBright.Size = new System.Drawing.Size(56, 23); this.btnBright.TabIndex = 1; this.btnBright.Text = "明场"; this.btnBright.UseVisualStyleBackColor = true; this.btnBright.Click += new System.EventHandler(this.btnBright_Click); // // btnPolarized // this.btnPolarized.Location = new System.Drawing.Point(69, 41); this.btnPolarized.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.btnPolarized.Name = "btnPolarized"; this.btnPolarized.Size = new System.Drawing.Size(56, 23); this.btnPolarized.TabIndex = 0; this.btnPolarized.Text = "偏光"; this.btnPolarized.UseVisualStyleBackColor = true; this.btnPolarized.Click += new System.EventHandler(this.btnPolarized_Click); // // MotorizedLightDialog // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(373, 267); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "MotorizedLightDialog"; this.Text = "电动偏光"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MotorizedLightDialog_FormClosing); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); this.groupBox3.ResumeLayout(false); this.ResumeLayout(false); } #endregion private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button btnClose; private System.Windows.Forms.Button btnCommit; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.TextBox txtBaudRate; private System.Windows.Forms.Label label2; private System.Windows.Forms.ComboBox cmbPort; private System.Windows.Forms.Label label1; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.Button btnBright; private System.Windows.Forms.Button btnPolarized; #endregion private MotorizedLightModel m_motorizedLightModel; private string m_portName; private int m_baudRate; private bool m_isOpen; public MotorizedLightDialog() { InitializeComponent(); InitializeLanguageText(); GetSerialLst(); InitializeData(); } private void InitializeData() { m_motorizedLightModel = Startup.instance.motorizedLightModel; m_portName = m_motorizedLightModel.PortName; m_baudRate = m_motorizedLightModel.BaudRate; cmbPort.SelectedItem = m_portName; txtBaudRate.Text = m_baudRate.ToString(); } private void Open() { if (this.cmbPort.Text.Trim().Length == 0) { MessageBox.Show(PdnResources.GetString("Menu.Theportnumbercannobeempty.Text")); return; } if (this.txtBaudRate.Text.Trim().Length == 0) { MessageBox.Show(PdnResources.GetString("Menu.Baudrempty.text")); return; } try { m_portName = this.cmbPort.Text.Trim(); m_baudRate = int.Parse(this.txtBaudRate.Text.Trim()); //m_axisController.Open(m_portName, m_baudRate, Parity.None, 8, StopBits.One); m_isOpen = true; } catch (Exception ex) { m_isOpen = false; // 串口无法打开时,打开参数设置界面 MessageBox.Show(ex.Message); } } private void GetSerialLst() { //清除cmbPort显示 cmbPort.SelectedIndex = -1; cmbPort.Items.Clear(); //获取串口列表 string[] serialLst = SerialPort.GetPortNames(); if (serialLst.Length > 0) { //取串口列表进行排序 Array.Sort(serialLst); //将串口列表输出到comboBox cmbPort.Items.AddRange(serialLst); cmbPort.SelectedIndex = 0; } } public const int WM_DEVICE_CHANGE = 0x219; //设备改变 public const int DBT_DEVICEARRIVAL = 0x8000; //设备插入 public const int DBT_DEVICE_REMOVE_COMPLETE = 0x8004; //设备移除 //消息处理 protected override void WndProc(ref Message m) { switch (m.Msg) //判断消息类型 { case WM_DEVICE_CHANGE: //设备改变消息 { GetSerialLst(); //设备改变时重新获取串口列表 } break; } base.WndProc(ref m); } private void CallbackDataReceived(CommandBase command, Result result, string msg) { //m_axisController.DataReceived -= new DataReceiveEvent(CallbackDataReceived); if (this == null || this.IsDisposed) { return; } switch (result) { case Result.ERR_PARSE: MessageBox.Show(PdnResources.GetString("Menu.Replydataarsingerror.Text")+":" + msg); return; case Result.ERR_TIMEOUT: MessageBox.Show(PdnResources.GetString("Menu.Theconsoleresponsetimeout.Text")); return; } if (command is CommandMotorizedLight) { MessageBox.Show(PdnResources.GetString("Menu.Successfullysaved.text")); } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void MotorizedLightDialog_FormClosing(object sender, FormClosingEventArgs e) { } private void btnCommit_Click(object sender, EventArgs e) { this.Open(); if (m_isOpen) { m_motorizedLightModel.PortName = m_portName; m_motorizedLightModel.BaudRate = m_baudRate; string stageModelXml = XmlSerializeHelper.XmlSerialize(m_motorizedLightModel); string filePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\MotorizedLightModel.xml"; if (!FileOperationHelper.WriteStringToFile(stageModelXml, filePath, FileMode.Create)) { MessageBox.Show(PdnResources.GetString("Menu.Savefailed.text")); } else { MessageBox.Show(PdnResources.GetString("Menu.Successfullysaved.text")); } } } private void txtBaudRate_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)) { e.Handled = true; return; } } private void btnPolarized_Click(object sender, EventArgs e) { this.Open(); if (m_isOpen) { //m_axisController.DataReceived += new DataReceiveEvent(CallbackDataReceived); //CommandMotorizedLight command = new CommandMotorizedLight(LightType.POLARIZED); //m_axisController.AddCommand(command); } } private void btnBright_Click(object sender, EventArgs e) { this.Open(); if (m_isOpen) { //m_axisController.DataReceived += new DataReceiveEvent(CallbackDataReceived); //CommandMotorizedLight command = new CommandMotorizedLight(LightType.BRIGHT); //m_axisController.AddCommand(command); } } } }