diff --git a/.vs/SerialAssistant/v15/.suo b/.vs/SerialAssistant/v15/.suo index 1a0830a..e3781f1 100644 Binary files a/.vs/SerialAssistant/v15/.suo and b/.vs/SerialAssistant/v15/.suo differ diff --git a/.vs/SerialAssistant/v15/Server/sqlite3/storage.ide b/.vs/SerialAssistant/v15/Server/sqlite3/storage.ide index f378904..c4393ff 100644 Binary files a/.vs/SerialAssistant/v15/Server/sqlite3/storage.ide and b/.vs/SerialAssistant/v15/Server/sqlite3/storage.ide differ diff --git a/README.md b/README.md index 60e733e..436bcc3 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,13 @@ -# SerialAssistant +# SerialAssistant 串口助手 第一次更新 2018/6/3 1、完成串口属性设置,打开与关闭异常处理; 2、字符串发送功能; 3、字符串接收功能; 第二次更新 2018/6/4 - 1、完善串口扩展功能界面部分 \ No newline at end of file + 1、完善串口扩展功能界面部分 +第三次更新 2018/6/6 + 1、完善串口扩展功能后台搭建 +---------------------------------------------------- +SerialAssistantV1.1.0标准版已完成 +---------------------------------------------------- \ No newline at end of file diff --git a/SerialAssistant/Form1.Designer.cs b/SerialAssistant/Form1.Designer.cs index 72368fc..aabc556 100644 --- a/SerialAssistant/Form1.Designer.cs +++ b/SerialAssistant/Form1.Designer.cs @@ -47,6 +47,7 @@ private void InitializeComponent() this.radioButton2 = new System.Windows.Forms.RadioButton(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.panel3 = new System.Windows.Forms.Panel(); + this.numericUpDown1 = new System.Windows.Forms.NumericUpDown(); this.checkBox3 = new System.Windows.Forms.CheckBox(); this.checkBox2 = new System.Windows.Forms.CheckBox(); this.radioButton4 = new System.Windows.Forms.RadioButton(); @@ -62,15 +63,14 @@ private void InitializeComponent() this.label8 = new System.Windows.Forms.Label(); this.panel6 = new System.Windows.Forms.Panel(); this.label9 = new System.Windows.Forms.Label(); - this.numericUpDown1 = new System.Windows.Forms.NumericUpDown(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.panel1.SuspendLayout(); this.panel2.SuspendLayout(); this.panel3.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit(); this.panel4.SuspendLayout(); this.panel5.SuspendLayout(); this.panel6.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit(); this.SuspendLayout(); // // panel1 @@ -255,6 +255,7 @@ private void InitializeComponent() this.button3.TabIndex = 11; this.button3.Text = "清空接收"; this.button3.UseVisualStyleBackColor = false; + this.button3.Click += new System.EventHandler(this.button3_Click); // // radioButton2 // @@ -264,13 +265,13 @@ private void InitializeComponent() this.radioButton2.Name = "radioButton2"; this.radioButton2.Size = new System.Drawing.Size(59, 25); this.radioButton2.TabIndex = 1; - this.radioButton2.TabStop = true; this.radioButton2.Text = "HEX"; this.radioButton2.UseVisualStyleBackColor = true; // // radioButton1 // this.radioButton1.AutoSize = true; + this.radioButton1.Checked = true; this.radioButton1.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.radioButton1.Location = new System.Drawing.Point(13, 5); this.radioButton1.Name = "radioButton1"; @@ -295,6 +296,29 @@ private void InitializeComponent() this.panel3.Size = new System.Drawing.Size(179, 98); this.panel3.TabIndex = 2; // + // numericUpDown1 + // + this.numericUpDown1.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.numericUpDown1.Increment = new decimal(new int[] { + 10, + 0, + 0, + 0}); + this.numericUpDown1.Location = new System.Drawing.Point(104, 70); + this.numericUpDown1.Maximum = new decimal(new int[] { + 1000000, + 0, + 0, + 0}); + this.numericUpDown1.Name = "numericUpDown1"; + this.numericUpDown1.Size = new System.Drawing.Size(65, 23); + this.numericUpDown1.TabIndex = 16; + this.numericUpDown1.Value = new decimal(new int[] { + 1000, + 0, + 0, + 0}); + // // checkBox3 // this.checkBox3.AutoSize = true; @@ -316,6 +340,7 @@ private void InitializeComponent() this.checkBox2.TabIndex = 14; this.checkBox2.Text = "自动发送"; this.checkBox2.UseVisualStyleBackColor = true; + this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged); // // radioButton4 // @@ -325,13 +350,13 @@ private void InitializeComponent() this.radioButton4.Name = "radioButton4"; this.radioButton4.Size = new System.Drawing.Size(59, 25); this.radioButton4.TabIndex = 13; - this.radioButton4.TabStop = true; this.radioButton4.Text = "HEX"; this.radioButton4.UseVisualStyleBackColor = true; // // radioButton3 // this.radioButton3.AutoSize = true; + this.radioButton3.Checked = true; this.radioButton3.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.radioButton3.Location = new System.Drawing.Point(13, 3); this.radioButton3.Name = "radioButton3"; @@ -361,7 +386,6 @@ private void InitializeComponent() this.textBox_receive.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox_receive.Size = new System.Drawing.Size(432, 337); this.textBox_receive.TabIndex = 0; - this.textBox_receive.WordWrap = false; this.textBox_receive.TextChanged += new System.EventHandler(this.textBox1_TextChanged); // // panel5 @@ -377,6 +401,7 @@ private void InitializeComponent() // button2 // this.button2.BackColor = System.Drawing.SystemColors.ButtonFace; + this.button2.Enabled = false; this.button2.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.button2.Location = new System.Drawing.Point(375, 30); this.button2.Name = "button2"; @@ -454,28 +479,9 @@ private void InitializeComponent() this.label9.TabIndex = 3; this.label9.Text = "@V1.1.0"; // - // numericUpDown1 + // timer1 // - this.numericUpDown1.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.numericUpDown1.Increment = new decimal(new int[] { - 10, - 0, - 0, - 0}); - this.numericUpDown1.Location = new System.Drawing.Point(104, 70); - this.numericUpDown1.Maximum = new decimal(new int[] { - 1000000, - 0, - 0, - 0}); - this.numericUpDown1.Name = "numericUpDown1"; - this.numericUpDown1.Size = new System.Drawing.Size(65, 23); - this.numericUpDown1.TabIndex = 16; - this.numericUpDown1.Value = new decimal(new int[] { - 1000, - 0, - 0, - 0}); + this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // Form1 // @@ -498,13 +504,13 @@ private void InitializeComponent() this.panel2.PerformLayout(); this.panel3.ResumeLayout(false); this.panel3.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit(); this.panel4.ResumeLayout(false); this.panel4.PerformLayout(); this.panel5.ResumeLayout(false); this.panel5.PerformLayout(); this.panel6.ResumeLayout(false); this.panel6.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit(); this.ResumeLayout(false); } diff --git a/SerialAssistant/Form1.cs b/SerialAssistant/Form1.cs index b3e08f0..087b5ab 100644 --- a/SerialAssistant/Form1.cs +++ b/SerialAssistant/Form1.cs @@ -5,12 +5,18 @@ using System.Drawing; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Windows.Forms; namespace SerialAssistant { public partial class Form1 : Form { + private long receive_count = 0; //接收字节计数 + private long send_count = 0; //发送字节计数 + private StringBuilder sb = new StringBuilder(); //为了避免在接收处理函数中反复调用,依然声明为一个全局变量 + private DateTime current_time = new DateTime(); //为了避免在接收处理函数中反复调用,依然声明为一个全局变量 + private StringBuilder builder = new StringBuilder(); //避免在事件处理方法中反复创建,定义为全局 public Form1() { @@ -38,6 +44,7 @@ private void Form1_Load(object sender, EventArgs e) comboBox4.Text = "None"; comboBox5.Text = "1"; + } private void textBox1_TextChanged(object sender, EventArgs e) { @@ -61,8 +68,9 @@ private void button1_Click(object sender, EventArgs e) comboBox3.Enabled = true; comboBox4.Enabled = true; comboBox5.Enabled = true; - textBox_receive.Text = ""; //清空接收区 - textBox_send.Text = ""; //清空发送区 + label6.Text = "串口已关闭"; + label6.ForeColor = Color.Red; + button2.Enabled = false; //失能发送按钮 } else { @@ -97,7 +105,10 @@ private void button1_Click(object sender, EventArgs e) serialPort1.Open(); //打开串口 button1.Text = "关闭串口"; button1.BackColor = Color.Firebrick; - + label6.Text = "串口已打开"; + label6.ForeColor = Color.Green; + button2.Enabled = true; //使能发送按钮 + } } catch (Exception ex) @@ -124,17 +135,72 @@ private void button1_Click(object sender, EventArgs e) private void button2_Click(object sender, EventArgs e) { + byte[] temp = new byte[1]; try { //首先判断串口是否开启 if (serialPort1.IsOpen) { + int num = 0; //获取本次发送字节数 //串口处于开启状态,将发送区文本发送 - serialPort1.Write(textBox_send.Text); + + //判断发送模式 + if (radioButton4.Checked) + { + //以HEX模式发送 + //首先需要用正则表达式将用户输入字符中的十六进制字符匹配出来 + string buf = textBox_send.Text; + string pattern = @"\s"; + string replacement = ""; + Regex rgx = new Regex(pattern); + string send_data = rgx.Replace(buf, replacement); + + //不发送新行 + num = (send_data.Length - send_data.Length % 2) / 2; + for (int i = 0; i < num; i++) + { + temp[0] = Convert.ToByte(send_data.Substring(i * 2, 2), 16); + serialPort1.Write(temp, 0, 1); //循环发送 + } + //如果用户输入的字符是奇数,则单独处理 + if (send_data.Length % 2 != 0) + { + temp[0] = Convert.ToByte(send_data.Substring(textBox_send.Text.Length-1,1), 16); + serialPort1.Write(temp, 0, 1); + num++; + } + //判断是否需要发送新行 + if (checkBox3.Checked) + { + //自动发送新行 + serialPort1.WriteLine(""); + } + } + else + { + //以ASCII模式发送 + //判断是否需要发送新行 + if (checkBox3.Checked) + { + //自动发送新行 + serialPort1.WriteLine(textBox_send.Text); + num = textBox_send.Text.Length + 2; //回车占两个字节 + } + else + { + //不发送新行 + serialPort1.Write(textBox_send.Text); + num = textBox_send.Text.Length; + } + } + + send_count += num; //计数变量累加 + label8.Text = "Tx:" + send_count.ToString() + "Bytes"; //刷新界面 } } catch (Exception ex) { + serialPort1.Close(); //捕获到异常,创建一个新的对象,之前的不可以再用 serialPort1 = new System.IO.Ports.SerialPort(); //刷新COM口选项 @@ -156,16 +222,50 @@ private void button2_Click(object sender, EventArgs e) //串口接收事件处理 private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { + int num = serialPort1.BytesToRead; //获取接收缓冲区中的字节数 + byte[] received_buf = new byte[num]; //声明一个大小为num的字节数据用于存放读出的byte型数据 + + + receive_count += num; //接收字节计数变量增加nun + serialPort1.Read(received_buf,0,num); //读取接收缓冲区中num个字节到byte数组中 + + sb.Clear(); //防止出错,首先清空字符串构造器 + + if (radioButton2.Checked) + { + //选中HEX模式显示 + foreach (byte b in received_buf) + { + sb.Append(b.ToString("X2") + ' '); //将byte型数据转化为2位16进制文本显示,并用空格隔开 + } + + } + else + { + //选中ASCII模式显示 + sb.Append(Encoding.ASCII.GetString(received_buf)); //将整个数组解码为ASCII数组 + } try { //因为要访问UI资源,所以需要使用invoke方式同步ui Invoke((EventHandler)(delegate { - textBox_receive.AppendText(serialPort1.ReadExisting()); + if (checkBox1.Checked) + { + //显示时间 + current_time = System.DateTime.Now; //获取当前时间 + textBox_receive.AppendText(current_time.ToString("HH:mm:ss") + " " + sb.ToString()); + + } + else + { + //不显示时间 + textBox_receive.AppendText(sb.ToString()); + } + label7.Text = "Rx:" + receive_count.ToString() + "Bytes"; } ) ); - } catch (Exception ex) { @@ -180,5 +280,41 @@ private void radioButton1_CheckedChanged(object sender, EventArgs e) { } + + private void button3_Click(object sender, EventArgs e) + { + textBox_receive.Text = ""; //清空接收文本框 + textBox_send.Text = ""; //清空发送文本框 + receive_count = 0; //接收计数清零 + send_count = 0; //发送计数 + label7.Text = "Rx:" + receive_count.ToString() + "Bytes"; //刷新界面 + label8.Text = "Tx:" + send_count.ToString() + "Bytes"; //刷新界面 + } + + private void checkBox2_CheckedChanged(object sender, EventArgs e) + { + if (checkBox2.Checked) + { + //自动发送功能选中,开始自动发送 + numericUpDown1.Enabled = false; //失能时间选择 + timer1.Interval = (int)numericUpDown1.Value; //定时器赋初值 + timer1.Start(); //启动定时器 + label6.Text = "串口已打开" + " 自动发送中..."; + } + else + { + //自动发送功能未选中,停止自动发送 + numericUpDown1.Enabled = true; //使能时间选择 + timer1.Stop(); //停止定时器 + label6.Text = "串口已打开"; + + } + } + + private void timer1_Tick(object sender, EventArgs e) + { + //定时时间到 + button2_Click(button2, new EventArgs()); //调用发送按钮回调函数 + } } } diff --git a/SerialAssistant/bin/Debug/SerialAssistant.exe b/SerialAssistant/bin/Debug/SerialAssistant.exe index 4e9a321..0373751 100644 Binary files a/SerialAssistant/bin/Debug/SerialAssistant.exe and b/SerialAssistant/bin/Debug/SerialAssistant.exe differ diff --git a/SerialAssistant/bin/Debug/SerialAssistant.pdb b/SerialAssistant/bin/Debug/SerialAssistant.pdb index a8fd84c..16a60d0 100644 Binary files a/SerialAssistant/bin/Debug/SerialAssistant.pdb and b/SerialAssistant/bin/Debug/SerialAssistant.pdb differ diff --git a/SerialAssistant/obj/Debug/SerialAssistant.csproj.GenerateResource.cache b/SerialAssistant/obj/Debug/SerialAssistant.csproj.GenerateResource.cache index b705ae3..5fcb5ac 100644 Binary files a/SerialAssistant/obj/Debug/SerialAssistant.csproj.GenerateResource.cache and b/SerialAssistant/obj/Debug/SerialAssistant.csproj.GenerateResource.cache differ diff --git a/SerialAssistant/obj/Debug/SerialAssistant.exe b/SerialAssistant/obj/Debug/SerialAssistant.exe index 4e9a321..0373751 100644 Binary files a/SerialAssistant/obj/Debug/SerialAssistant.exe and b/SerialAssistant/obj/Debug/SerialAssistant.exe differ diff --git a/SerialAssistant/obj/Debug/SerialAssistant.pdb b/SerialAssistant/obj/Debug/SerialAssistant.pdb index a8fd84c..16a60d0 100644 Binary files a/SerialAssistant/obj/Debug/SerialAssistant.pdb and b/SerialAssistant/obj/Debug/SerialAssistant.pdb differ