接上一节,这节来编写一个完整的串口调试助手!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;using System.IO.Ports;
namespace SerialCommunicate
{public partial class Form1 : Form{public Form1(){InitializeComponent();System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;button3.Enabled = false;}private void button1_Click(object sender, EventArgs e)//打开串口{try{serialPort1.PortName = comboBox1.Text;//把选择的串口名赋给串口serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text,10);//波特率一开始读出的是字符串形式,所以要把字符串转换成十进制数据。serialPort1.Open();//打开串口button1.Enabled = false;//打开串口按钮不可用button2.Enabled = true;//关闭串口}catch {MessageBox.Show("端口错误,请检查串口", "错误");}}private void Form1_Load(object sender, EventArgs e){for (int i = 1; i < 20; i++)//添加19个串口号。也可以在,属性-》items集合 中添加。{comboBox1.Items.Add("COM" + i.ToString());}comboBox1.Text = "COM1";//串口号多额默认值comboBox2.Text = "4800";//波特率默认值/*****************非常重要************************/serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);//必须手动添加事件处理程序}/******************接收部分**********************************/private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口数据接收事件{if (!radioButton3.Checked)//如果接收模式为字符模式{string str = serialPort1.ReadExisting();//字符串方式读textBox1.AppendText(str);//添加内容。AppendText函数等同:textBox1.Text += str;即在尾部添加,不清空前面接收的字符}else { //如果接收模式为数值接收byte data;data = (byte)serialPort1.ReadByte();//此处需要强制类型转换,将(int)类型数据转换为(byte类型数据,不必考虑是否会丢失数据string str = Convert.ToString(data, 16).ToUpper();//转换为大写十六进制字符串textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");//空位补“0”   //上一句等同为:if(str.Length == 1)//                  str = "0" + str;//              else //                  str = str;//              textBox1.AppendText("0x" + str);}}private void radioButton2_CheckedChanged(object sender, EventArgs e){}private void button2_Click(object sender, EventArgs e)//关闭串口{try{serialPort1.Close();//关闭串口button1.Enabled = true;//打开串口按钮可用button2.Enabled = false;//关闭串口按钮不可用}catch//一般情况下关闭串口不会出错,所以不需要加处理程序{}}/******************发送部分**********************************/private void button3_Click(object sender, EventArgs e){byte[] Data = new byte[1];//单字节发送if (serialPort1.IsOpen)//判断串口是否打开,如果打开执行下一步操作{if (textBox2.Text != "")//如果发送内容为空,不做反应{button3.Enabled = false;}else{button3.Enabled = true;if (!radioButton1.Checked)//如果发送模式是字符模式{try{serialPort1.WriteLine(textBox2.Text);//写数据}catch{MessageBox.Show("串口数据写入错误", "错误");//出错提示serialPort1.Close();button1.Enabled = true;//打开串口按钮可用button2.Enabled = false;//关闭串口按钮不可用}}else//如果发送模式是数值模式{/****处理偶数个数****/for (int i = 0; i < (textBox2.Text.Length - textBox2.Text.Length % 2) / 2; i++)//取余3运算作用是防止用户输入的字符为奇数个{Data[0] = Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16);serialPort1.Write(Data, 0, 1);//循环发送(如果输入字符为0A0BB,则只发送0A,0B)}/****处理奇数个数****/if (textBox2.Text.Length % 2 != 0)//剩下一位单独处理{//Data[0] = Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length - TextBox2.Text.Length % 2, TextBox2.Text.Length % 2), 16);Data[0] = Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length - 1, 1), 16);//单独发送B(0B)serialPort1.Write(Data, 0, 1);//发送}}                  }}}private void textBox1_TextChanged(object sender, EventArgs e)//文本控件内容改变事件{//textBox1.ScrollToCaret();//将滚动条调至最下。但此版本默认此功能。}}
}

www.DoYoung.net(部分代码来至杜洋工作室)

C#之windows桌面软件第三课:完整的串口调试助手相关推荐

  1. C#之windows桌面软件第七课:(下集)串口工具实现数据校验、用灯反应设备状态

    C#之windows桌面软件第七课:(下集)串口工具实现数据校验.用灯反应设备状态 using System; using System.Collections.Generic; using Syst ...

  2. C#之windows桌面软件第六课:(上集)串口工具实现数据校验、用灯反应设备状态

    C#之windows桌面软件第六课:(上集)串口工具实现数据校验.用灯反应设备状态 using System; using System.Collections.Generic; using Syst ...

  3. C#之windows桌面软件第十三课:C#中常用的类有哪些?构造函数怎么用?

    C#之windows桌面软件第十三课:C#中常用的类有哪些?构造函数怎么用? using System; using System.Collections.Generic; using System. ...

  4. C#之windows桌面软件第十一课:电脑ADC值显示(上位机)(多通道显示)

    C#之windows桌面软件第十一课:电脑ADC值显示(上位机)(多通道显示) using System; using System.Collections.Generic; using System ...

  5. C#之windows桌面软件第十课:电脑ADC值显示(上位机)(单通道显示)

    C#之windows桌面软件第十课:电脑ADC值显示(上位机) (单通道显示) using System; using System.Collections.Generic; using System ...

  6. C#之windows桌面软件第八课:汉字(GB2312)与编码(UTF-8)之间的相互转换

    C#之windows桌面软件第八课:汉字(GB2312)与编码(UTF-8)之间的相互转换 using System; using System.Collections.Generic; using ...

  7. C#之windows桌面软件第五课:串口助手实现定时关闭设备、鼠标移动使按钮颜色变化功能

    本节在串口助手上实现: 1.定时关闭设备 2.移动鼠标使按钮颜色变换 Form1.cs代码如下: using System; using System.Collections.Generic; usi ...

  8. C#之windows桌面软件第四课:串口助手控制设备的开关

    串口助手控制设备的开关 using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...

  9. C#之windows桌面软件第十二课:电脑ADC值显示(上位机),记忆上次串口号,并用TrackBar控件显示ADC值

    C#之windows桌面软件第十二课:电脑ADC值显示(上位机),记忆上次串口号,并用TrackBar控件显示ADC值 using System; using System.Collections.G ...

最新文章

  1. 浏览器--如何让登陆页面的表单不默认显示账号和密码
  2. 《转载》Java异常处理的10个最佳实践
  3. 湖南科技大学c语言程序设计b,2017年湖南科技大学计算机科学与工程学院826C语言程序设计与数据结构综合之数据结构考研题库...
  4. 2020年AI领域有哪些让人惊艳的研究?
  5. 开源的数据库,PostgreSQL 基础入门实战
  6. Dapper源码学习和源码修改
  7. hadoop生态系统学习之路(十)MR将结果输出到hbase
  8. mysql不带加密模式jar包_Spring boot jar包加密(防止放在客户端反编译)
  9. plc编程入门视频教程
  10. osgb转json_cesuim加载倾斜摄影OSGB三维数据
  11. 营收增速环比放缓 Okta高歌猛进的那股劲去哪了?
  12. arcgis制作分幅图层,并对分幅图进行编号
  13. 会议安排(经典贪心算法例题)
  14. linux驱动开发篇Linaro(arm交叉编译器的下载与安装)
  15. openwrt安装ipk报错“incompatible with the architectures configured”
  16. 2021年危险化学品经营单位安全管理人员作业考试题库及危险化学品经营单位安全管理人员操作证考试
  17. bin文件夹是个什么东西?
  18. 个人简历模板html5
  19. Excel-VBA应用(5):设计问卷及数据回收统计系统
  20. OpenThreads库介绍——Block

热门文章

  1. 利用ConcurrentHashMap来实现一个ConcurrentHashSet
  2. 反射(操作MetaData)
  3. java 中文及特殊字符校验
  4. 嵌入式软件设计第8次实验报告-140201235-陈宇
  5. mount windows目录
  6. 用tableView实现的一种加载数据的布局
  7. C++中多态与虚函数的学习例子
  8. 【转载】windows mobile 上隐藏和关闭X以及OK的处理
  9. 汇编中的扫描码、ASCII码(也称字符码)、扩展码、虚拟键码 的区别
  10. 线程的切入和切出(切入: 一个线程被系统选中占用处理器开始或继续运行)