分享一個寫得不錯的C sharp串口調試工具的源代碼!
0.png (66.73 KB, 下載次數: 57)
下載附件
2017-10-26 17:24 上傳
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;//設置串口號
- 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++)
- {
- 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);//添加內容textBox文本框中依次向后顯示
- }
- else //如果接收模式為數值接收
- {
- //易出現異常:由于線程退出或應用程序請求,已中止 I/O 操作
- //加入異常處理
- try
- {
- int data;
- data = serialPort1.ReadByte();
- string str = Convert.ToString(data, 16).ToUpper();//轉換為大寫十六進制字符串
- textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");//空位補“0”
- }
- catch
- {
- this.Close();//關閉當前窗體
- }
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.Close();//關閉串口
- button1.Enabled = true;//打開串口按鈕可用
- button2.Enabled = false;//關閉串口按鈕不可用
- }
- catch
- {
- MessageBox.Show("串口關閉錯誤","錯誤");
- }
- }
- 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
- {
- 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 - 1, 1), 16);//單獨發送B(0B)
- serialPort1.Write(Data, 0, 1);//發送
- }
- }
- }
- }
- }
-
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
- if (textBox2.Text != "")
- button3.Enabled = true;//只有當發送文本框內的有內容時才可以發送
- else
- button3.Enabled = false;
- }
- private void button4_Click(object sender, EventArgs e)
- {
- textBox1.Text="";//清屏
- }
- private void button5_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- }
- }
復制代碼
所有資料51hei提供下載:
project4.zip
(59.25 KB, 下載次數: 50)
2017-10-26 12:29 上傳
點擊文件名下載附件
c sharp串口 下載積分: 黑幣 -5
|