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 partial class SetVoltageDialog : 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); } #region Windows Form Designer generated code 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("Form.OkButton.Text"); this.groupBox2.Text = PdnResources.GetString("Menu.Setting.Text"); this.label10.Text = PdnResources.GetString("Menu.Baudrate.text") + ":"; this.label11.Text = PdnResources.GetString("Menu.Theportnumber.Text") + ":"; this.label9.Text = PdnResources.GetString("Menu.Powerondefaultvoltagevalue.Text") + ":"; this.btnSetVoltage.Text = PdnResources.GetString("Menu.Settothedefaultvalue.Text"); this.label3.Text = PdnResources.GetString("Menu.Keysteppingvoltagevalue.Text") + ":"; this.label2.Text = PdnResources.GetString("Menu.Theknobturnsthesteppingvoltagevalue.Text") + ":"; this.label1.Text = PdnResources.GetString("Menu.Outputcontrolsource.Text") + ":"; this.groupBox3.Text = PdnResources.GetString("Menu.Thecurrentstate.Text"); this.btnSearch.Text = PdnResources.GetString("Menu.Generalanalysis.Integrator.Inquire.text"); this.label8.Text = PdnResources.GetString("Menu.Theoutputcurrent.Text") + ":"; this.label7.Text = PdnResources.GetString("Menu.Theoutputvoltage.Text") + ":"; this.label6.Text = PdnResources.GetString("Menu.Theinputvoltage.Text") + ":"; this.label5.Text = PdnResources.GetString("Menu.Thepowersupplystatus.Text") + ":"; this.label4.Text = PdnResources.GetString("Menu.potentiometer.Text") + ":"; this.Text = PdnResources.GetString("Menu.HardwareControl.SetVoltage.Text"); } /// /// 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.pnlVoltage = new System.Windows.Forms.Panel(); this.btnDel1 = new System.Windows.Forms.Button(); this.btnVoltage1 = new System.Windows.Forms.Button(); this.label14 = new System.Windows.Forms.Label(); this.label13 = new System.Windows.Forms.Label(); this.label12 = new System.Windows.Forms.Label(); this.txtBaudRate = new System.Windows.Forms.TextBox(); this.label10 = new System.Windows.Forms.Label(); this.cmbPort = new System.Windows.Forms.ComboBox(); this.label11 = new System.Windows.Forms.Label(); this.txtDefaultVoltage = new System.Windows.Forms.TextBox(); this.label9 = new System.Windows.Forms.Label(); this.btnSetVoltage = new System.Windows.Forms.Button(); this.trbVoltage = new System.Windows.Forms.TrackBar(); this.txtVoltage = new System.Windows.Forms.TextBox(); this.txtButtonStepping = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.txtEC11Stepping = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.rdoBoth = new System.Windows.Forms.RadioButton(); this.rdoEC11 = new System.Windows.Forms.RadioButton(); this.rdoUSB = new System.Windows.Forms.RadioButton(); this.label1 = new System.Windows.Forms.Label(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.lblPower = new System.Windows.Forms.Label(); this.lblVoltageOut = new System.Windows.Forms.Label(); this.lblState = new System.Windows.Forms.Label(); this.lblVoltageIn = new System.Windows.Forms.Label(); this.lblPotentiometer = new System.Windows.Forms.Label(); this.btnSearch = new System.Windows.Forms.Button(); this.label8 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.pnlVoltage.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.trbVoltage)).BeginInit(); 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(12, 12); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(776, 88); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; // // btnClose // this.btnClose.Location = new System.Drawing.Point(663, 32); this.btnClose.Name = "btnClose"; this.btnClose.Size = new System.Drawing.Size(79, 36); this.btnClose.TabIndex = 3; this.btnClose.UseVisualStyleBackColor = true; this.btnClose.Click += new System.EventHandler(this.btnClose_Click); // // btnCommit // this.btnCommit.Location = new System.Drawing.Point(550, 32); this.btnCommit.Name = "btnCommit"; this.btnCommit.Size = new System.Drawing.Size(79, 36); this.btnCommit.TabIndex = 2; this.btnCommit.UseVisualStyleBackColor = true; this.btnCommit.Click += new System.EventHandler(this.btnCommit_Click); // // groupBox2 // this.groupBox2.Controls.Add(this.pnlVoltage); this.groupBox2.Controls.Add(this.label14); this.groupBox2.Controls.Add(this.label13); this.groupBox2.Controls.Add(this.label12); this.groupBox2.Controls.Add(this.txtBaudRate); this.groupBox2.Controls.Add(this.label10); this.groupBox2.Controls.Add(this.cmbPort); this.groupBox2.Controls.Add(this.label11); this.groupBox2.Controls.Add(this.txtDefaultVoltage); this.groupBox2.Controls.Add(this.label9); this.groupBox2.Controls.Add(this.btnSetVoltage); this.groupBox2.Controls.Add(this.trbVoltage); this.groupBox2.Controls.Add(this.txtVoltage); this.groupBox2.Controls.Add(this.txtButtonStepping); this.groupBox2.Controls.Add(this.label3); this.groupBox2.Controls.Add(this.txtEC11Stepping); this.groupBox2.Controls.Add(this.label2); this.groupBox2.Controls.Add(this.rdoBoth); this.groupBox2.Controls.Add(this.rdoEC11); this.groupBox2.Controls.Add(this.rdoUSB); this.groupBox2.Controls.Add(this.label1); this.groupBox2.Location = new System.Drawing.Point(12, 210); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(776, 313); this.groupBox2.TabIndex = 1; this.groupBox2.TabStop = false; // // pnlVoltage // this.pnlVoltage.Controls.Add(this.btnDel1); this.pnlVoltage.Controls.Add(this.btnVoltage1); this.pnlVoltage.Location = new System.Drawing.Point(32, 250); this.pnlVoltage.Name = "pnlVoltage"; this.pnlVoltage.Size = new System.Drawing.Size(722, 57); this.pnlVoltage.TabIndex = 30; // // btnDel1 // this.btnDel1.Location = new System.Drawing.Point(78, 0); this.btnDel1.Name = "btnDel1"; this.btnDel1.Size = new System.Drawing.Size(38, 31); this.btnDel1.TabIndex = 14; this.btnDel1.Text = "X"; this.btnDel1.UseVisualStyleBackColor = true; // // btnVoltage1 // this.btnVoltage1.Location = new System.Drawing.Point(0, 0); this.btnVoltage1.Name = "btnVoltage1"; this.btnVoltage1.Size = new System.Drawing.Size(78, 31); this.btnVoltage1.TabIndex = 13; this.btnVoltage1.Text = "0.85V"; this.btnVoltage1.UseVisualStyleBackColor = true; // // label14 // this.label14.AutoSize = true; this.label14.Location = new System.Drawing.Point(583, 164); this.label14.Name = "label14"; this.label14.Size = new System.Drawing.Size(15, 15); this.label14.TabIndex = 29; this.label14.Text = "V"; // // label13 // this.label13.AutoSize = true; this.label13.Location = new System.Drawing.Point(300, 164); this.label13.Name = "label13"; this.label13.Size = new System.Drawing.Size(15, 15); this.label13.TabIndex = 28; this.label13.Text = "V"; // // label12 // this.label12.AutoSize = true; this.label12.Location = new System.Drawing.Point(298, 86); this.label12.Name = "label12"; this.label12.Size = new System.Drawing.Size(15, 15); this.label12.TabIndex = 27; this.label12.Text = "V"; // // txtBaudRate // this.txtBaudRate.Location = new System.Drawing.Point(348, 35); this.txtBaudRate.Name = "txtBaudRate"; this.txtBaudRate.Size = new System.Drawing.Size(100, 25); this.txtBaudRate.TabIndex = 26; this.txtBaudRate.Text = "9600"; this.txtBaudRate.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtBaudRate_KeyPress); // // label10 // this.label10.AutoSize = true; this.label10.Location = new System.Drawing.Point(266, 40); this.label10.Name = "label10"; this.label10.Size = new System.Drawing.Size(67, 15); this.label10.TabIndex = 25; // // cmbPort // this.cmbPort.FormattingEnabled = true; this.cmbPort.Location = new System.Drawing.Point(105, 37); this.cmbPort.Name = "cmbPort"; this.cmbPort.Size = new System.Drawing.Size(121, 23); this.cmbPort.TabIndex = 24; // // label11 // this.label11.AutoSize = true; this.label11.Location = new System.Drawing.Point(32, 40); this.label11.Name = "label11"; this.label11.Size = new System.Drawing.Size(67, 15); this.label11.TabIndex = 23; // // txtDefaultVoltage // this.txtDefaultVoltage.Location = new System.Drawing.Point(215, 81); this.txtDefaultVoltage.Name = "txtDefaultVoltage"; this.txtDefaultVoltage.Size = new System.Drawing.Size(79, 25); this.txtDefaultVoltage.TabIndex = 22; this.txtDefaultVoltage.Text = "10.25"; this.txtDefaultVoltage.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.CheckKeyPress); // // label9 // this.label9.AutoSize = true; this.label9.Location = new System.Drawing.Point(29, 84); this.label9.Name = "label9"; this.label9.Size = new System.Drawing.Size(127, 15); this.label9.TabIndex = 21; // // btnSetVoltage // this.btnSetVoltage.Location = new System.Drawing.Point(611, 195); this.btnSetVoltage.Name = "btnSetVoltage"; this.btnSetVoltage.Size = new System.Drawing.Size(143, 35); this.btnSetVoltage.TabIndex = 10; this.btnSetVoltage.UseVisualStyleBackColor = true; this.btnSetVoltage.Click += new System.EventHandler(this.btnSetVoltage_Click); // // trbVoltage // this.trbVoltage.AutoSize = false; this.trbVoltage.Location = new System.Drawing.Point(138, 205); this.trbVoltage.Maximum = 1220; this.trbVoltage.Minimum = 960; this.trbVoltage.Name = "trbVoltage"; this.trbVoltage.Size = new System.Drawing.Size(467, 25); this.trbVoltage.TabIndex = 9; this.trbVoltage.TickStyle = System.Windows.Forms.TickStyle.None; this.trbVoltage.Value = 960; this.trbVoltage.ValueChanged += new System.EventHandler(this.trbVoltage_ValueChanged); // // txtVoltage // this.txtVoltage.Location = new System.Drawing.Point(32, 205); this.txtVoltage.Name = "txtVoltage"; this.txtVoltage.ReadOnly = true; this.txtVoltage.Size = new System.Drawing.Size(100, 25); this.txtVoltage.TabIndex = 8; this.txtVoltage.Text = "9.6"; // // txtButtonStepping // this.txtButtonStepping.Location = new System.Drawing.Point(477, 159); this.txtButtonStepping.Name = "txtButtonStepping"; this.txtButtonStepping.Size = new System.Drawing.Size(100, 25); this.txtButtonStepping.TabIndex = 7; this.txtButtonStepping.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.CheckKeyPress); // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(333, 162); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(127, 15); this.label3.TabIndex = 6; // // txtEC11Stepping // this.txtEC11Stepping.Location = new System.Drawing.Point(215, 159); this.txtEC11Stepping.Name = "txtEC11Stepping"; this.txtEC11Stepping.Size = new System.Drawing.Size(79, 25); this.txtEC11Stepping.TabIndex = 5; this.txtEC11Stepping.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.CheckKeyPress); // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(29, 162); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(189, 15); this.label2.TabIndex = 4; // // rdoBoth // this.rdoBoth.AutoSize = true; this.rdoBoth.Location = new System.Drawing.Point(319, 123); this.rdoBoth.Name = "rdoBoth"; this.rdoBoth.Size = new System.Drawing.Size(108, 19); this.rdoBoth.TabIndex = 3; this.rdoBoth.Text = "USB && EC11"; this.rdoBoth.UseVisualStyleBackColor = true; // // rdoEC11 // this.rdoEC11.AutoSize = true; this.rdoEC11.Location = new System.Drawing.Point(239, 123); this.rdoEC11.Name = "rdoEC11"; this.rdoEC11.Size = new System.Drawing.Size(60, 19); this.rdoEC11.TabIndex = 2; this.rdoEC11.Text = "EC11"; this.rdoEC11.UseVisualStyleBackColor = true; // // rdoUSB // this.rdoUSB.AutoSize = true; this.rdoUSB.Checked = true; this.rdoUSB.Location = new System.Drawing.Point(157, 123); this.rdoUSB.Name = "rdoUSB"; this.rdoUSB.Size = new System.Drawing.Size(52, 19); this.rdoUSB.TabIndex = 1; this.rdoUSB.TabStop = true; this.rdoUSB.Text = "USB"; this.rdoUSB.UseVisualStyleBackColor = true; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(29, 123); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(97, 15); this.label1.TabIndex = 0; // // groupBox3 // this.groupBox3.Controls.Add(this.lblPower); this.groupBox3.Controls.Add(this.lblVoltageOut); this.groupBox3.Controls.Add(this.lblState); this.groupBox3.Controls.Add(this.lblVoltageIn); this.groupBox3.Controls.Add(this.lblPotentiometer); this.groupBox3.Controls.Add(this.btnSearch); this.groupBox3.Controls.Add(this.label8); this.groupBox3.Controls.Add(this.label7); this.groupBox3.Controls.Add(this.label6); this.groupBox3.Controls.Add(this.label5); this.groupBox3.Controls.Add(this.label4); this.groupBox3.Location = new System.Drawing.Point(12, 106); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(776, 98); this.groupBox3.TabIndex = 2; this.groupBox3.TabStop = false; // // lblPower // this.lblPower.AutoSize = true; this.lblPower.Location = new System.Drawing.Point(466, 63); this.lblPower.Name = "lblPower"; this.lblPower.Size = new System.Drawing.Size(0, 15); this.lblPower.TabIndex = 10; // // lblVoltageOut // this.lblVoltageOut.AutoSize = true; this.lblVoltageOut.Location = new System.Drawing.Point(283, 63); this.lblVoltageOut.Name = "lblVoltageOut"; this.lblVoltageOut.Size = new System.Drawing.Size(0, 15); this.lblVoltageOut.TabIndex = 9; // // lblState // this.lblState.AutoSize = true; this.lblState.Location = new System.Drawing.Point(283, 33); this.lblState.Name = "lblState"; this.lblState.Size = new System.Drawing.Size(0, 15); this.lblState.TabIndex = 8; // // lblVoltageIn // this.lblVoltageIn.AutoSize = true; this.lblVoltageIn.Location = new System.Drawing.Point(107, 63); this.lblVoltageIn.Name = "lblVoltageIn"; this.lblVoltageIn.Size = new System.Drawing.Size(0, 15); this.lblVoltageIn.TabIndex = 7; // // lblPotentiometer // this.lblPotentiometer.AutoSize = true; this.lblPotentiometer.Location = new System.Drawing.Point(107, 33); this.lblPotentiometer.Name = "lblPotentiometer"; this.lblPotentiometer.Size = new System.Drawing.Size(0, 15); this.lblPotentiometer.TabIndex = 6; // // btnSearch // this.btnSearch.Location = new System.Drawing.Point(651, 38); this.btnSearch.Name = "btnSearch"; this.btnSearch.Size = new System.Drawing.Size(91, 33); this.btnSearch.TabIndex = 5; this.btnSearch.UseVisualStyleBackColor = true; this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click); // // label8 // this.label8.AutoSize = true; this.label8.Location = new System.Drawing.Point(378, 63); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(82, 15); this.label8.TabIndex = 4; // // label7 // this.label7.AutoSize = true; this.label7.Location = new System.Drawing.Point(195, 63); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(82, 15); this.label7.TabIndex = 3; // // label6 // this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point(29, 63); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(82, 15); this.label6.TabIndex = 2; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(195, 33); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(82, 15); this.label5.TabIndex = 1; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(29, 33); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(67, 15); this.label4.TabIndex = 0; // // SetVoltageDialog // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(799, 535); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "SetVoltageDialog"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.SetVoltageDialog_FormClosing); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); this.pnlVoltage.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.trbVoltage)).EndInit(); this.groupBox3.ResumeLayout(false); this.groupBox3.PerformLayout(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox txtButtonStepping; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox txtEC11Stepping; private System.Windows.Forms.Label label2; private System.Windows.Forms.RadioButton rdoBoth; private System.Windows.Forms.RadioButton rdoEC11; private System.Windows.Forms.RadioButton rdoUSB; private System.Windows.Forms.Button btnClose; private System.Windows.Forms.Button btnCommit; private System.Windows.Forms.TextBox txtVoltage; private System.Windows.Forms.Button btnSetVoltage; private System.Windows.Forms.TrackBar trbVoltage; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.Button btnSearch; private System.Windows.Forms.Label label8; private System.Windows.Forms.Label label7; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox txtDefaultVoltage; private System.Windows.Forms.Label label9; private System.Windows.Forms.TextBox txtBaudRate; private System.Windows.Forms.Label label10; private System.Windows.Forms.ComboBox cmbPort; private System.Windows.Forms.Label label11; private System.Windows.Forms.Label label14; private System.Windows.Forms.Label label13; private System.Windows.Forms.Label label12; private System.Windows.Forms.Panel pnlVoltage; private System.Windows.Forms.Button btnDel1; private System.Windows.Forms.Button btnVoltage1; private System.Windows.Forms.Label lblPower; private System.Windows.Forms.Label lblVoltageOut; private System.Windows.Forms.Label lblState; private System.Windows.Forms.Label lblVoltageIn; private System.Windows.Forms.Label lblPotentiometer; #endregion private VoltageModel m_voltageModel; private List m_voltages = new List(); private VoltageController m_voltageController; private string m_portName; private int m_baudRate; private bool m_isOpen; public SetVoltageDialog() { InitializeComponent(); InitializeLanguageText(); GetSerialLst(); InitializeData(); } private void InitializeData() { m_voltageModel = Startup.instance.voltageModel; m_portName = m_voltageModel.PortName; m_baudRate = m_voltageModel.BaudRate; cmbPort.SelectedItem = m_portName; txtBaudRate.Text = m_baudRate.ToString(); txtDefaultVoltage.Text = m_voltageModel.DefaultVoltage.ToString(); txtEC11Stepping.Text = m_voltageModel.EC11Stepping.ToString(); txtButtonStepping.Text = m_voltageModel.ButtonStepping.ToString(); switch (m_voltageModel.Controls) { case "USB": rdoUSB.Checked = true; break; case "EC11": rdoEC11.Checked = true; break; default: rdoBoth.Checked = true; break; } m_voltages = m_voltageModel.items; this.SetVoltages(); m_voltageController = VoltageController.GetInstance(); m_voltageController.Close(); m_voltageController.VoltageReceived += new VoltageEvent(CallbackDataReceived); if(cmbPort.Text.Length != 0) { this.Search(); } } private void SetVoltages() { Button voltageButton; Button delButton; pnlVoltage.Controls.Clear(); for (int i=0; i< m_voltages.Count; i++) { voltageButton = new Button(); voltageButton.Size = new Size(60, 25); voltageButton.Name = "voltage" + i.ToString(); voltageButton.Text = m_voltages[i].Voltage.ToString()+"V"; voltageButton.Click += new System.EventHandler(this.Voltage_Click); voltageButton.Location = new Point(i * 100, 0); pnlVoltage.Controls.Add(voltageButton); delButton = new Button(); delButton.Size = new Size(20, 25); delButton.Name = "del" + i.ToString(); delButton.Text = "X"; //delButton.FlatStyle = FlatStyle.Flat; //delButton.FlatAppearance.BorderSize = 0; //delButton.BackgroundImageLayout = ImageLayout.Zoom; //delButton.BackgroundImage = PdnResources.GetImageResource("Images.ButtonUp.png").Reference; delButton.Click += new System.EventHandler(this.VoltageDel_Click); delButton.Location = new Point(i * 100 + 60, 0); pnlVoltage.Controls.Add(delButton); } } private void Voltage_Click(object sender, EventArgs e) { Button button = (Button)sender; int index = int.Parse(button.Name.Trim().Replace("voltage", "")); trbVoltage.Value = (int)(m_voltages[index].Voltage * 100); this.Open(); if (m_isOpen) { string value = "0" + m_voltages[index].Voltage * 100; string commandText = "*309*" + value.Substring(value.Length > 4 ? 1 : 0) + "#"; CommandVoltage command = new CommandVoltage(commandText); m_voltageController.AddCommand(command); } } private void VoltageDel_Click(object sender, EventArgs e) { Button button = (Button)sender; int index = int.Parse(button.Name.Trim().Replace("del", "")); m_voltages.RemoveAt(index); this.SetVoltages(); } private void btnSetVoltage_Click(object sender, EventArgs e) { if (m_voltages.Count >= 5) { MessageBox.Show(PdnResources.GetString("Menu.Upto5presets.Text")); return; } for (int i = 0; i < m_voltages.Count; i++) { if (m_voltages[i].Voltage.ToString().Equals(txtVoltage.Text.Trim())) { return; } } VoltageModel.Item item = new VoltageModel.Item(); item.Voltage = decimal.Parse(txtVoltage.Text.Trim()); m_voltages.Add(item); this.SetVoltages(); } private void CallbackDataReceived(CommandBase command, Result result, string msg) { 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 CommandVoltage) { // *301*0723,1,1500,1100,07000# if (msg.Substring(0, 4).Equals("*301")) { this.Invoke(new Action(() => { lblPotentiometer.Text = msg.Substring(5, 4); switch (msg.Substring(10, 1)) { case "0": lblState.Text = PdnResources.GetString("Menu.Normalhutdow.Text"); break; case "1": lblState.Text = PdnResources.GetString("Menu.Normalboot.Text"); break; case "2": lblState.Text = PdnResources.GetString("Menu.Abnormaloperation.Text"); break; case "3": lblState.Text = PdnResources.GetString("Menu.Abnormalshutdown.Text"); break; } lblVoltageIn.Text = (decimal.Parse( msg.Substring(12, 4)) / 100).ToString() + "V"; lblVoltageOut.Text = (decimal.Parse(msg.Substring(17, 4)) / 100).ToString() + "V"; lblPower.Text = (decimal.Parse(msg.Substring(22, 5)) / 10000).ToString() + "A"; })); } } } 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_voltageController.Open(m_portName, m_baudRate); m_isOpen = true; } catch (Exception) { 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 CheckKeyPress(object sender, KeyPressEventArgs e) { TextBox textBox = (TextBox)sender; if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46) e.Handled = true; //小数点的处理。 if ((int)e.KeyChar == 46) //小数点 { if (textBox.Text.Length <= 0) e.Handled = true; //小数点不能在第一位 else { float f; float oldf; bool b1 = false, b2 = false; b1 = float.TryParse(textBox.Text, out oldf); b2 = float.TryParse(textBox.Text + e.KeyChar.ToString(), out f); if (b2 == false) { if (b1 == true) e.Handled = true; else e.Handled = false; } } } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void trbVoltage_ValueChanged(object sender, EventArgs e) { txtVoltage.Text = (trbVoltage.Value * 0.01).ToString(); } private void btnSearch_Click(object sender, EventArgs e) { this.Search(); } private void Search() { this.Open(); if (m_isOpen) { CommandVoltage command = new CommandVoltage("*301#"); m_voltageController.AddCommand(command); } } private void btnCommit_Click(object sender, EventArgs e) { 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; } decimal defaultVoltage = decimal.Parse(txtDefaultVoltage.Text.Trim()); if (defaultVoltage < (decimal)9.6 || defaultVoltage > (decimal)12.2) { MessageBox.Show(PdnResources.GetString("Menu.Voltagesettingrange.Text")+"9.6V ~ 12.2V"); return; } decimal ec11Stepping = decimal.Parse(txtEC11Stepping.Text.Trim()); decimal buttonStepping = decimal.Parse(txtButtonStepping.Text.Trim()); if (ec11Stepping < (decimal)0.01) { MessageBox.Show(PdnResources.GetString("Menu.EC11rotarysteppingvoltagevalueunitV.Text")); return; } if (buttonStepping < (decimal)0.01) { MessageBox.Show(PdnResources.GetString("Menu.Buttonsteppingvoltagevalueuni.Text")); return; } m_voltageModel.PortName = m_portName; m_voltageModel.BaudRate = m_baudRate; m_voltageModel.DefaultVoltage = decimal.Parse(txtDefaultVoltage.Text.Trim()); string code; if (rdoUSB.Checked) { m_voltageModel.Controls = "USB"; code = "01"; } else if (rdoEC11.Checked) { m_voltageModel.Controls = "EC11"; code = "10"; } else { m_voltageModel.Controls = "BOTH"; code = "11"; } m_voltageModel.EC11Stepping = decimal.Parse(txtEC11Stepping.Text.Trim()); m_voltageModel.ButtonStepping = decimal.Parse(txtButtonStepping.Text.Trim()); string stageModelXml = XmlSerializeHelper.XmlSerialize(m_voltageModel); string filePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\VoltageModel.xml"; if (!FileOperationHelper.WriteStringToFile(stageModelXml, filePath, FileMode.Create)) { MessageBox.Show(PdnResources.GetString("Menu.Savefailed.text")); } else { this.Open(); if (m_isOpen) { // 设置开机默认电压 string defaultValue = ("0" + m_voltageModel.DefaultVoltage * 100); CommandVoltage command = new CommandVoltage("*305*" + defaultValue.Substring(defaultValue.Length > 4 ? 1 : 0) + "#"); m_voltageController.AddCommand(command); // 设置控制源 command = new CommandVoltage("*307*" + code + "#"); m_voltageController.AddCommand(command); // *308*0008,0050# string ecValue = "000" + m_voltageModel.EC11Stepping * 100; string btnValue = "000" + m_voltageModel.ButtonStepping * 100; command = new CommandVoltage("*308*" + ecValue.Substring(ecValue.Length - 4, 4) + "," + btnValue.Substring(btnValue.Length - 4, 4) + "#"); m_voltageController.AddCommand(command); } MessageBox.Show(PdnResources.GetString("Menu.Successfullysaved.text")); } } private void SetVoltageDialog_FormClosing(object sender, FormClosingEventArgs e) { m_voltageController.VoltageReceived -= new VoltageEvent(CallbackDataReceived); m_voltageController.Close(); } private void txtBaudRate_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)) { e.Handled = true; return; } } } }