找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5592|回复: 1
收起左侧

不错的C sharp串口工具源码分享

[复制链接]
ID:243092 发表于 2017-10-26 12:30 | 显示全部楼层 |阅读模式
分享一个写得不错的C sharp串口调试工具的源代码!
0.png

C sharp源程序如下:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO.Ports;

  10. namespace project4
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.             System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
  18.         }

  19.         private void button1_Click(object sender, EventArgs e)
  20.         {
  21.             try
  22.             {
  23.                 serialPort1.PortName = comboBox1.Text;//设置串口号
  24.                 serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10);//十进制数据转换,设置波特率
  25.                 serialPort1.Open();//打开串口
  26.                 button1.Enabled = false;//打开串口按钮不可用
  27.                 button2.Enabled = true;//关闭串口按钮可用
  28.             }
  29.             catch
  30.             {
  31.                 MessageBox.Show("端口错误,请检查串口", "错误");
  32.             }
  33.         }

  34.         private void Form1_Load(object sender, EventArgs e)
  35.         {
  36.             for (int i = 1; i < 20; i++)
  37.             {
  38.                 comboBox1.Items.Add("COM" + i.ToString());
  39.             }
  40.             comboBox1.Text = "COM3";//串口号默认值
  41.             comboBox2.Text = "9600";//波特率默认值

  42.             serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);//添加事件处理程序
  43.         }

  44.         private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  45.         {
  46.             if (!radioButton3.Checked)//如果接收模式为字符模式
  47.             {
  48.                 string str = serialPort1.ReadExisting();//字符串方式读
  49.                 textBox1.AppendText(str);//添加内容textBox文本框中依次向后显示
  50.             }
  51.             else //如果接收模式为数值接收
  52.             {
  53.                 //易出现异常:由于线程退出或应用程序请求,已中止 I/O 操作
  54.                 //加入异常处理
  55.                 try
  56.                 {
  57.                     int data;
  58.                     data = serialPort1.ReadByte();
  59.                     string str = Convert.ToString(data, 16).ToUpper();//转换为大写十六进制字符串
  60.                     textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");//空位补“0”                 
  61.                 }
  62.                 catch
  63.                 {
  64.                     this.Close();//关闭当前窗体
  65.                 }

  66.             }
  67.         }

  68.         private void button2_Click(object sender, EventArgs e)
  69.         {
  70.             try
  71.             {
  72.                 serialPort1.Close();//关闭串口
  73.                 button1.Enabled = true;//打开串口按钮可用
  74.                 button2.Enabled = false;//关闭串口按钮不可用
  75.             }
  76.             catch
  77.             {
  78.                 MessageBox.Show("串口关闭错误","错误");
  79.             }
  80.         }

  81.         private void button3_Click(object sender, EventArgs e)
  82.         {
  83.             byte[] Data = new byte[1];
  84.             if (serialPort1.IsOpen)//判断串口是否打开,如果打开执行下一步操作
  85.             {
  86.                 if (textBox2.Text != "")
  87.                 {
  88.                     if (!radioButton1.Checked)//如果发送模式是字符模式
  89.                     {
  90.                         try
  91.                         {
  92.                             serialPort1.WriteLine(textBox2.Text);//写数据
  93.                         }
  94.                         catch
  95.                         {
  96.                             MessageBox.Show("串口数据写入错误", "错误");//出错提示
  97.                             serialPort1.Close();
  98.                             button1.Enabled = true;//打开串口按钮可用
  99.                             button2.Enabled = false;//关闭串口按钮不可用
  100.                         }
  101.                     }
  102.                     else
  103.                     {
  104.                         for (int i = 0; i < (textBox2.Text.Length - textBox2.Text.Length % 2) / 2; i++)//取余3运算作用是防止用户输入的字符为奇数个
  105.                         {
  106.                             Data[0] = Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16);
  107.                             serialPort1.Write(Data, 0, 1);//循环发送(如果输入字符为0A0BB,则只发送0A,0B)
  108.                         }
  109.                         if (textBox2.Text.Length % 2 != 0)//剩下一位单独处理
  110.                         {
  111.                             Data[0] = Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length - 1, 1), 16);//单独发送B(0B)
  112.                             serialPort1.Write(Data, 0, 1);//发送
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.         
  119.         private void textBox2_TextChanged(object sender, EventArgs e)
  120.         {
  121.             if (textBox2.Text != "")
  122.                 button3.Enabled = true;//只有当发送文本框内的有内容时才可以发送
  123.             else
  124.                 button3.Enabled = false;
  125.         }

  126.         private void button4_Click(object sender, EventArgs e)
  127.         {
  128.             textBox1.Text="";//清屏
  129.         }

  130.         private void button5_Click(object sender, EventArgs e)
  131.         {
  132.             this.Close();
  133.         }

  134.     }
  135. }
复制代码

所有资料51hei提供下载:
project4.zip (59.25 KB, 下载次数: 50)
回复

使用道具 举报

ID:447337 发表于 2020-1-4 17:30 | 显示全部楼层
C SHARP 不是VC,VB, 暂时看不了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表