分享一個寫得不錯的C sharp串口調(diào)試工具的源代碼!
C sharp源程序如下:
- 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 project4
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.PortName = comboBox1.Text;//設(shè)置串口號
- serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10);//十進制數(shù)據(jù)轉(zhuǎn)換,設(shè)置波特率
- serialPort1.Open();//打開串口
- button1.Enabled = false;//打開串口按鈕不可用
- button2.Enabled = true;//關(guān)閉串口按鈕可用
- }
- catch
- {
- MessageBox.Show("端口錯誤,請檢查串口", "錯誤");
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- for (int i = 1; i < 20; i++)
- {
- comboBox1.Items.Add("COM" + i.ToString());
- }
- comboBox1.Text = "COM3";//串口號默認值
- comboBox2.Text = "9600";//波特率默認值
- serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);//添加事件處理程序
- }
- private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- if (!radioButton3.Checked)//如果接收模式為字符模式
- {
- string str = serialPort1.ReadExisting();//字符串方式讀
- textBox1.AppendText(str);//添加內(nèi)容textBox文本框中依次向后顯示
- }
- else //如果接收模式為數(shù)值接收
- {
- //易出現(xiàn)異常:由于線程退出或應(yīng)用程序請求,已中止 I/O 操作
- //加入異常處理
- try
- {
- int data;
- data = serialPort1.ReadByte();
- string str = Convert.ToString(data, 16).ToUpper();//轉(zhuǎn)換為大寫十六進制字符串
- textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");//空位補“0”
- }
- catch
- {
- this.Close();//關(guān)閉當前窗體
- }
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.Close();//關(guān)閉串口
- button1.Enabled = true;//打開串口按鈕可用
- button2.Enabled = false;//關(guān)閉串口按鈕不可用
- }
- catch
- {
- MessageBox.Show("串口關(guān)閉錯誤","錯誤");
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- byte[] Data = new byte[1];
- if (serialPort1.IsOpen)//判斷串口是否打開,如果打開執(zhí)行下一步操作
- {
- if (textBox2.Text != "")
- {
- if (!radioButton1.Checked)//如果發(fā)送模式是字符模式
- {
- try
- {
- serialPort1.WriteLine(textBox2.Text);//寫數(shù)據(jù)
- }
- catch
- {
- MessageBox.Show("串口數(shù)據(jù)寫入錯誤", "錯誤");//出錯提示
- serialPort1.Close();
- button1.Enabled = true;//打開串口按鈕可用
- button2.Enabled = false;//關(guān)閉串口按鈕不可用
- }
- }
- else
- {
- for (int i = 0; i < (textBox2.Text.Length - textBox2.Text.Length % 2) / 2; i++)//取余3運算作用是防止用戶輸入的字符為奇數(shù)個
- {
- Data[0] = Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16);
- serialPort1.Write(Data, 0, 1);//循環(huán)發(fā)送(如果輸入字符為0A0BB,則只發(fā)送0A,0B)
- }
- if (textBox2.Text.Length % 2 != 0)//剩下一位單獨處理
- {
- Data[0] = Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length - 1, 1), 16);//單獨發(fā)送B(0B)
- serialPort1.Write(Data, 0, 1);//發(fā)送
- }
- }
- }
- }
- }
-
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
- if (textBox2.Text != "")
- button3.Enabled = true;//只有當發(fā)送文本框內(nèi)的有內(nèi)容時才可以發(fā)送
- else
- button3.Enabled = false;
- }
- private void button4_Click(object sender, EventArgs e)
- {
- textBox1.Text="";//清屏
- }
- private void button5_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
project4.zip
(59.25 KB, 下載次數(shù): 50)
2017-10-26 12:29 上傳
點擊文件名下載附件
c sharp串口
|