标签:style c class blog code tar
using System; using System.Windows.Forms; using System.IO.Ports; using System.Drawing; using System.IO; using System.Text; using System.Collections; using System.Threading; using System.Runtime.InteropServices; namespace SerialPortAssistant { public partial class FrmMain : Form { class TextAndValue { private string _RealValue = ""; private string _DisplayText = ""; public string DisplayText { get { return _DisplayText; } } public string RealValue { get { return _RealValue; } } public TextAndValue(string ShowText, string RealVal) { _DisplayText = ShowText; _RealValue = RealVal; } public override string ToString() { return _RealValue.ToString(); } } public FrmMain() { InitializeComponent(); } public string msg = ""; public ArrayList commmand = new ArrayList(); private void FrmMain_Load(object sender, EventArgs e) { #region 加载配置 LoadCansu(); #endregion } #region 加载参数 //加载参数 public void LoadCansu() { //如果有配置 则读取配置文件 if (File.Exists(Application.StartupPath + @"\SerConfig.wjs")) { using (StreamReader sr = new StreamReader(Application.StartupPath + @"\SerConfig.wjs", Encoding.UTF8)) { comboBox1.SelectedItem = sr.ReadLine(); comboBox2.SelectedItem = sr.ReadLine(); comboBox3.SelectedItem = sr.ReadLine(); comboBox4.SelectedItem = sr.ReadLine(); comboBox5.SelectedItem = sr.ReadLine(); } } else { comboBox1.SelectedIndex = 2; comboBox2.SelectedIndex = 6; comboBox3.SelectedIndex = 0; comboBox4.SelectedIndex = 0; comboBox5.SelectedIndex = 0; } FrmMain.CheckForIllegalCrossThreadCalls = false; txt_csdy.Text = iniHelpers.ContentValue("f初始电压"); txt_jsdy.Text = iniHelpers.ContentValue("f结束电压"); txt_jxsj.Text = iniHelpers.ContentValue("f静息时间"); txt_jxdy.Text = iniHelpers.ContentValue("f静息电压"); txt_fjsj.Text = iniHelpers.ContentValue("f富集时间"); txt_fjdy.Text = iniHelpers.ContentValue("f富集电压"); txt_mczq.Text = iniHelpers.ContentValue("f脉冲周期"); txt_cyjg.Text = iniHelpers.ContentValue("f采样间隔"); txt_mckd.Text = iniHelpers.ContentValue("f脉冲宽度"); txt_bjdy.Text = iniHelpers.ContentValue("f步进电压"); txt_mcfd.Text = iniHelpers.ContentValue("f脉冲幅度"); text_csdy.Text = iniHelpers.ContentValue("g初始电压"); text_jsdy.Text = iniHelpers.ContentValue("g结束电压"); text_jxsj.Text = iniHelpers.ContentValue("g静息时间"); text_bjsd.Text = iniHelpers.ContentValue("g步进速度"); text_dgsj.Text = iniHelpers.ContentValue("g镀汞时间"); text_cybjdy.Text = iniHelpers.ContentValue("g采样步进电压"); text_jxsj.Text = iniHelpers.ContentValue("g静息时间"); text_jxdw.Text = iniHelpers.ContentValue("g静息点位"); //连通阀加载 ArrayList al = new ArrayList(); al.Add(new TextAndValue("系统1", iniHelpers.ContentValue("系统1"))); al.Add(new TextAndValue("系统2", iniHelpers.ContentValue("系统2"))); al.Add(new TextAndValue("系统3", iniHelpers.ContentValue("系统3"))); cmb_yq.DataSource = al; cmb_yq.DisplayMember = "DisplayText"; cmb_yq.ValueMember = "RealValue"; } #endregion //打开串口 private void button1_Click(object sender, EventArgs e) { if (button1.Text == "打开串口") { //打开串口 try { serialPort1.PortName = comboBox1.SelectedItem.ToString(); serialPort1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem.ToString()); switch (comboBox3.SelectedIndex) { case 0: serialPort1.Parity = Parity.None; break; case 1: serialPort1.Parity = Parity.Odd; break; case 2: serialPort1.Parity = Parity.Even; break; case 3: serialPort1.Parity = Parity.Mark; break; case 4: serialPort1.Parity = Parity.Space; break; } serialPort1.DataBits = Convert.ToInt32(comboBox4.SelectedItem.ToString()); switch (comboBox5.SelectedIndex) { case 0: serialPort1.StopBits = StopBits.One; break; case 1: serialPort1.StopBits = StopBits.Two; break; case 2: serialPort1.StopBits = StopBits.OnePointFive; break; } serialPort1.Open(); panel1.BackColor = Color.LawnGreen; comboBox1.Enabled = false; comboBox2.Enabled = false; comboBox3.Enabled = false; comboBox4.Enabled = false; comboBox5.Enabled = false; tssl01.Text = "串口" + comboBox1.SelectedItem.ToString() + "已经打开"; button1.Text = "关闭串口"; // timer2.Start(); } catch (Exception) { MessageBox.Show("串口打开失败" + "\n" + "1.请检查配置的参数是否正确" + "\n" + "2.外围设备是否没有连接" + "\n" + "3.串口是否已经打开或被占用" + "\n" + "4.串口驱动是否没有安装"); } } else { serialPort1.Close(); panel1.BackColor = Color.Red; comboBox1.Enabled = true; comboBox2.Enabled = true; comboBox3.Enabled = true; comboBox4.Enabled = true; comboBox5.Enabled = true; tssl01.Text = "串口" + comboBox1.SelectedItem.ToString() + "已经关闭"; button1.Text = "打开串口"; } } #region 清空缓存区 //清空缓存区 private void button4_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { serialPort1.DiscardInBuffer(); textBox1.Text = ""; } } private void button3_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { serialPort1.DiscardOutBuffer(); } } #endregion #region 保存配置 private void button6_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { using (StreamWriter sw = new StreamWriter(Application.StartupPath + @"\SerConfig.wjs", false, Encoding.UTF8)) { sw.WriteLine(comboBox1.SelectedItem.ToString()); sw.WriteLine(comboBox2.SelectedItem.ToString()); sw.WriteLine(comboBox3.SelectedItem.ToString()); sw.WriteLine(comboBox4.SelectedItem.ToString()); sw.WriteLine(comboBox5.SelectedItem.ToString()); sw.Flush(); } MessageBox.Show("配置已成功保存 下次程序启动后会自动读取配置"); } else { MessageBox.Show("只有成功打开了串口 才能保存配置"); } } #endregion private void button2_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "存储有命令的文本文件|*.txt"; openFile.ShowDialog(); if (openFile.FileName.Length > 0) { using (StreamReader sr = new StreamReader(openFile.FileName, Encoding.UTF8)) { while (!sr.EndOfStream) { commmand.Add(sr.ReadLine()); } } } } public int type = 0; //发送命令 private void button5_Click(object sender, EventArgs e) { string msg = "SMS" + cmb_yq.SelectedValue.ToString() + ":" + textBox2.Text; serialPort1.Write(msg); MessageBox.Show("命令发送完成!!!"); //if (serialPort1.IsOpen) //{ // if (button5.Text == "开始发送") // { // if (checkBox1.Checked == true) // { // type = 1; // timer1.Interval = Convert.ToInt32(numericUpDown1.Value) * 1000; // timer1.Enabled = true; // button1.Enabled = false; // } // else // { // TimerAction.timerAction(this,type); // } // } // else if (button5.Text == "停止发送") // { // timer1.Enabled = false; // button1.Enabled = true; // textBox1.Text = ""; // groupBox2.Enabled = true; // button5.Text = "开始发送"; // } //} //else //{ // timer1.Enabled = false; // groupBox2.Enabled = true; // textBox1.Text = ""; // button5.Text = "开始发送"; // MessageBox.Show("请先打开串口 再发送数据"); //} } private void timer1_Tick(object sender, EventArgs e) { TimerAction.timerAction(this,type); } //check改变事件 private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked == true) { numericUpDown1.Enabled = true; } else { numericUpDown1.Enabled = false; } } //串中接收事件 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { //try //{ // string text = string.Empty; // byte[] result = new byte[serialPort1.BytesToRead]; // serialPort1.Read(result, 0, serialPort1.BytesToRead); // foreach (byte b in result) // { // text = text + Convert.ToString(b, 16); // } // textBox1.Text = text; // if (text.Length==10) // { // string[] getSz = GetArrayFromString(text); // txt_zn.Text = getSz[0]; // txt_cd.Text = getSz[1]; // txt_pb.Text = getSz[2]; // txt_cu.Text = getSz[3]; // } //} //catch (Exception err) //{ // MessageBox.Show(err.Message); //} } //保存循环伏安法参数 private void btn_save_xhfaf_Click(object sender, EventArgs e) { iniHelpers.write("f", "----------------f代表循环伏安法参数----------------------"); iniHelpers.write("f初始电压", txt_csdy.Text.Trim()); iniHelpers.write("f结束电压", txt_jsdy.Text.Trim()); iniHelpers.write("f静息时间", txt_jxsj.Text.Trim()); iniHelpers.write("f静息电压", txt_jxdy.Text.Trim()); iniHelpers.write("f富集时间", txt_fjsj.Text.Trim()); iniHelpers.write("f富集电压", txt_fjdy.Text.Trim()); iniHelpers.write("f脉冲周期", txt_mczq.Text.Trim()); iniHelpers.write("f采样间隔", txt_cyjg.Text.Trim()); iniHelpers.write("f脉冲宽度", txt_mckd.Text.Trim()); iniHelpers.write("f步进电压", txt_bjdy.Text.Trim()); iniHelpers.write("f脉冲幅度", txt_mcfd.Text.Trim()); MessageBox.Show("循环伏安法参数保存成功!!!"); } //镀汞参数设置 private void btn_save_dg_Click(object sender, EventArgs e) { iniHelpers.write("g", "-----------------g代表镀汞参数--------------------"); iniHelpers.write("g初始电压", text_csdy.Text.Trim()); iniHelpers.write("g结束电压", text_jsdy.Text.Trim()); iniHelpers.write("g步进速度", text_bjsd.Text.Trim()); iniHelpers.write("g镀汞时间", text_dgsj.Text.Trim()); iniHelpers.write("g采样步进电压", text_cybjdy.Text.Trim()); iniHelpers.write("g静息时间", text_jxsj.Text.Trim()); iniHelpers.write("g静息点位", text_jxdw.Text.Trim()); MessageBox.Show("镀汞参数保存成功!!!"); } //循环伏安法 private void btn_faf_send_Click(object sender, EventArgs e) { //int i = 11; //string s = i.ToString("X").PadLeft(4, ‘0‘); //转16进制 //MessageBox.Show(s); string csdy = Convert.ToInt32(txt_csdy.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string jsdy = Convert.ToInt32(txt_jsdy.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string jxsj = Convert.ToInt32(txt_jxsj.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string fjsj = Convert.ToInt32(txt_fjsj.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string fjdy = Convert.ToInt32(txt_fjdy.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string mczq = Convert.ToInt32(txt_mczq.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string cyjg = Convert.ToInt32(txt_cyjg.Text.Trim()).ToString("X").PadLeft(2, ‘0‘); string mckd = Convert.ToInt32(txt_mckd.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string bjdy = Convert.ToInt32(txt_bjdy.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string mcfd = Convert.ToInt32(txt_mcfd.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string jxdy = Convert.ToInt32(txt_jxdy.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); //循环伏安法(CMD=0x03):CMD(0xff)+富集时间(0xffff)+富集电压(0xffff)+静息时间(0xffff)+静息电压(0xffff)+初始电压(0xffff)+结束电压(0xffff)+步进电压(0xffff)+脉冲幅度(0xffff)+脉冲宽度(0xffff)+脉冲周期(0xffff)+采样间隔(0xff)+结束符(0xfefe) if(serialPort1.IsOpen) { //MessageBox.Show("03" + fjsj + fjdy + jxsj + jsdy + csdy + jsdy + bjdy + mcfd + mckd + mczq + cyjg + "fe"); //serialPort1.Write("03" + fjsj + fjdy + jxsj + jsdy + csdy + jsdy + bjdy + mcfd + mckd + mczq + cyjg + "fe"); string msg = "SMS" + cmb_yq.SelectedValue.ToString() + ":" + "03" + fjsj + fjdy + jxsj + jxdy + csdy + jsdy + bjdy + mcfd + mckd + mczq + cyjg + "fe"; if (MessageBox.Show("指令:03" + fjsj + fjdy + jxsj + jxdy + csdy + jsdy + bjdy + mcfd + mckd + mczq + cyjg + "fefe", "是否向 "+cmb_yq.SelectedValue.ToString()+" 发送指令", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { serialPort1.Write(msg); } } } //镀汞命令发送 private void btn_dg_send_Click(object sender, EventArgs e) { string csdy = Convert.ToInt32(text_csdy.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string jsdy = Convert.ToInt32(text_jsdy.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string jxsj = Convert.ToInt32(text_jxsj.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string bjsd = Convert.ToInt32(text_bjsd.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string dgsj = Convert.ToInt32(text_dgsj.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); string cybjdy = Convert.ToInt32(text_cybjdy.Text.Trim()).ToString("X").PadLeft(2, ‘0‘); string jxdw = Convert.ToInt32(text_jxdw.Text.Trim()).ToString("X").PadLeft(4, ‘0‘); //镀汞(CMD=0x05):CMD(0xff)+初始电压(0xffff)+结束电压(0xffff)+步进速度(0xffff)+镀汞时间(0xffff)+采样步进电压(0xff)+静息时间Time_silent(0xffff)+静息电位Voltage_silent(0xffff)+结束符(0xfefe); if (serialPort1.IsOpen) { string msg = "SMS" + cmb_yq.SelectedValue.ToString() + ":" + "05" + csdy + jsdy + bjsd + dgsj + cybjdy + jxsj + jxdw + "fefe"; if (MessageBox.Show("指令:05" + csdy + jsdy + bjsd + dgsj + cybjdy + jxsj + jxdw + "fe", "是否向 " + cmb_yq.SelectedValue.ToString() + " 发送指令", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { serialPort1.Write(msg); MessageBox.Show("命令发送完成!!!"); } } } #region 连通阀,命令发送 //连通阀,命令发送 private void btn_liangtong_Click(object sender, EventArgs e) { string[] sen = new string[6]; sen[0] = "474F30310D00"; sen[1] = "474F30320D00"; sen[2] = "474F30330D00"; sen[3] = "474F30340D00"; sen[4] = "474F30350D00"; sen[5] = "474F30360D00"; if (serialPort1.IsOpen) { string msg = "SMS" + cmb_yq.SelectedValue.ToString() + ":01" + sen[(cmb_ltz.SelectedIndex)]; serialPort1.Write(msg); MessageBox.Show("命令发送完成!!!"); } else { MessageBox.Show("串口未打开"); } } #endregion #region 系统设置按钮,保存配置 //系统设置按钮,保存配置,并加载新数据 private void btn_xtSeting_Click(object sender, EventArgs e) { SetSystem.SetSystemM(this); MessageBox.Show("系统设置成功!!!"); // cmb_yq.Items.Clear(); ArrayList al = new ArrayList(); al.Add(new TextAndValue("系统1", iniHelpers.ContentValue("系统1"))); al.Add(new TextAndValue("系统2", iniHelpers.ContentValue("系统2"))); al.Add(new TextAndValue("系统3", iniHelpers.ContentValue("系统3"))); cmb_yq.DataSource = al; cmb_yq.DisplayMember = "DisplayText"; cmb_yq.ValueMember = "RealValue"; } #endregion #region 获取结果 //获取结果 private void btn_hqjg_Click(object sender, EventArgs e) { string msg = "SMS" + cmb_yq.SelectedValue.ToString() + ":" + "07fe"; serialPort1.Write(msg); timer_mess.Start(); //string[] getSz = GetArrayFromString("01020304fe"); //txt_zn.Text = getSz[0]; //txt_cd.Text = getSz[1]; //txt_pb.Text = getSz[2]; //txt_cu.Text = getSz[3]; } public string[] GetArrayFromString(string val) { var count = val.Length % 2 == 0 ? val.Length / 2 : (val.Length + 1) / 2; var charVal = val.ToCharArray(); string[] result = new string[count]; for (var i = 1; i < val.Length; i++) { if ((i + 1) % 2 == 0) { result[(i - 1) / 2] = charVal[i - 1] + "" + charVal[i]; } } if (charVal.Length % 2 != 0) { result[count - 1] = "0" + charVal[val.Length - 1]; } return result; } private void timer2_Tick(object sender, EventArgs e) { try { string text = string.Empty; byte[] result = new byte[serialPort1.BytesToRead]; serialPort1.Read(result, 0, serialPort1.BytesToRead); foreach (byte b in result) { text = text + Convert.ToString(b, 16); } textBox1.Text = text; if (text.Length == 10) { string[] getSz = GetArrayFromString(text); txt_zn.Text = getSz[0]; txt_cd.Text = getSz[1]; txt_pb.Text = getSz[2]; txt_cu.Text = getSz[3]; } } catch (Exception err) { MessageBox.Show(err.Message); } } #endregion #region 抽满时间控件间隔发 private void btn_paikong_Click(object sender, EventArgs e) { //抽满:初始化+延时20s+输入阀+延时1s+抽满 //排空:输出阀+延时1s+排空 //初始化 0x2F,0x31,0x5A,0x30,0x52,0x0D,0x00 //输入阀 0x2F,0x31,0x49,0x52,0x0D,0x00 //输出阀 0x2F,0x31,0x4F,0x52,0x0D,0x00 //抽满 0x2F,0x31,0x41,0x33,0x30,0x30,0x30,0x52,0x0D,0x00 //排空 0x2F,0x31,0x41,0x30,0x52,0x0D,0x00 if (serialPort1.IsOpen) { btn_paikong.Enabled = false; timer_pk1.Start(); } else { MessageBox.Show("串口未打开"); } } private void timer_pa1_Tick(object sender, EventArgs e) { msg = "SMS" + cmb_yq.SelectedValue.ToString() + ":" + "2F315A30520D00"; serialPort1.Write(msg); //MessageBox.Show("OK"); //Thread.Sleep(10000); timer_pa2.Start(); timer_pa1.Stop(); } private void timer_pa2_Tick(object sender, EventArgs e) { msg = "SMS" + cmb_yq.SelectedValue.ToString() + ":" + "2F3149520D00"; serialPort1.Write(msg); //Thread.Sleep(10000); // MessageBox.Show("") timer_pa3.Start(); timer_pa2.Stop(); } private void timer_pa3_Tick(object sender, EventArgs e) { msg = "SMS" + cmb_yq.SelectedValue.ToString() + ":" + "2F314133303030520D00"; serialPort1.Write(msg); //Thread.Sleep(10000); //MessageBox.Show("命令发送完成!!!"); btn_cm.Enabled = true; timer_pa3.Stop(); } #endregion #region 排空时间控件间隔发 //抽满 private void btn_cm_Click(object sender, EventArgs e) { //抽满:初始化+延时20s+输入阀+延时1s+抽满 //排空:输出阀+延时1s+排空 //初始化 0x2F,0x31,0x5A,0x30,0x52,0x0D,0x00 //输入阀 0x2F,0x31,0x49,0x52,0x0D,0x00 //输出阀 0x2F,0x31,0x4F,0x52,0x0D,0x00 //抽满 0x2F,0x31,0x41,0x33,0x30,0x30,0x30,0x52,0x0D,0x00 //排空 0x2F,0x31,0x41,0x30,0x52,0x0D,0x00 if (serialPort1.IsOpen) { btn_cm.Enabled = false; timer_pa1.Start(); //timer_pa1.Interval = 20000; //msg = "SMS" + cmb_yq.SelectedValue.ToString() + ":" + "2F315A30520D00"; //serialPort1.Write(msg); //Thread.Sleep(20000); //msg = ""; //msg = "SMS" + cmb_yq.SelectedValue.ToString() + ":" + "2F3149520D00"; //serialPort1.Write(msg); //Thread.Sleep(10000); //msg = ""; //msg = "SMS" + cmb_yq.SelectedValue.ToString() + ":" + "2F314133303030520D00"; //serialPort1.Write(msg); //MessageBox.Show("命令发送完成!!!"); } else { MessageBox.Show("串口未打开"); } } private void timer_pk1_Tick(object sender, EventArgs e) { string msg = "SMS" + cmb_yq.SelectedValue.ToString() + ":" + "2F314F520D00"; serialPort1.Write(msg); //Thread.Sleep(10000); timer_pk2.Start(); timer_pk1.Stop(); } private void timer_pk2_Tick(object sender, EventArgs e) { msg = "SMS" + cmb_yq.SelectedValue.ToString() + ":" + "2F314130520D00"; serialPort1.Write(msg); //Thread.Sleep(10000); btn_paikong.Enabled = true; timer_pk2.Stop(); // MessageBox.Show("命令发送完成!!!"); } #endregion //提示消息 private void timer_mess_Tick(object sender, EventArgs e) { // Thread.Sleep(10000); MessageBox.Show("未收到数据!!!"); timer_mess.Stop(); } } }
标签:style c class blog code tar
原文地址:http://www.cnblogs.com/vienna/p/3753085.html