博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#串口调试助手代码
阅读量:4166 次
发布时间:2019-05-26

本文共 4364 字,大约阅读时间需要 14 分钟。

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 串口调试助手{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;        }        private void Form1_Load(object sender, EventArgs e)        {            for (int i = 0; i < 10; i++)            {                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);//添加内容            }            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 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 button2_Click(object sender, EventArgs e) { try { serialPort1.Close(); button1.Enabled = true;//打开串口按钮可用 button2.Enabled = false;//关闭串口按钮不可用 } catch(Exception err)//一般情况下关闭串口不会出错,所以不需要加处理程序 { } } private void button3_Click(object sender, EventArgs e) { byte[] Data = new byte[1]; if (serialPort1.IsOpen)//判断串口是否打开,如果打开执行下一步操作 { if (textBox2.Text != "") { if(!radioButton1.Checked)//如果发送模式是字符模式 { try { serialPort1.WriteLine(textBox2.Text); } catch (Exception err) { 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); } } } } } }}

转载地址:http://srrxi.baihongyu.com/

你可能感兴趣的文章
嵌入式100题(037):Http1.1和Http1.0的区别
查看>>
嵌入式100题(038):HTTPS与HTTP的一些区别
查看>>
嵌入式100题(042):为什么服务端易受到SYN攻击?
查看>>
嵌入式100题(043):什么是四次挥手
查看>>
嵌入式100题(044):为什么客户端最后还要等待2MSL?
查看>>
嵌入式100题(045):为什么建立连接是三次握手,关闭连接确是四次挥手呢?...
查看>>
嵌入式100题(028):static的用法(定义和用途)
查看>>
嵌入式100题(027):char和int之间的转换
查看>>
嵌入式100题(029):const常量和#define的区别(编译阶段、安全性、内存占用等)...
查看>>
嵌入式100题(030):volatile作用和用法
查看>>
嵌入式100题(033):TCP、UDP的优缺点
查看>>
嵌入式100题(035):TCP为什么是可靠连接
查看>>
嵌入式100题(034):TCP UDP适用场景
查看>>
嵌入式100题(70):一个程序从开始运行到结束的完整过程(四个过程)
查看>>
嵌入式100题(71):什么是堆,栈,内存泄漏和内存溢出?
查看>>
嵌入式100题(73):死锁的原因、条件 创建一个死锁,以及如何预防
查看>>
嵌入式100题(60):系统调用的作用
查看>>
C语言基本概念归纳
查看>>
初识单片机
查看>>
在单片机上点亮LED
查看>>