找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 6355|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

不錯(cuò)的C sharp串口工具源碼分享

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:243092 發(fā)表于 2017-10-26 12:30 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
分享一個(gè)寫得不錯(cuò)的C sharp串口調(diào)試工具的源代碼!


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;//設(shè)置串口號(hào)
  24.                 serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10);//十進(jìn)制數(shù)據(jù)轉(zhuǎn)換,設(shè)置波特率
  25.                 serialPort1.Open();//打開串口
  26.                 button1.Enabled = false;//打開串口按鈕不可用
  27.                 button2.Enabled = true;//關(guān)閉串口按鈕可用
  28.             }
  29.             catch
  30.             {
  31.                 MessageBox.Show("端口錯(cuò)誤,請(qǐng)檢查串口", "錯(cuò)誤");
  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";//串口號(hào)默認(rèn)值
  41.             comboBox2.Text = "9600";//波特率默認(rèn)值

  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);//添加內(nèi)容textBox文本框中依次向后顯示
  50.             }
  51.             else //如果接收模式為數(shù)值接收
  52.             {
  53.                 //易出現(xiàn)異常:由于線程退出或應(yīng)用程序請(qǐng)求,已中止 I/O 操作
  54.                 //加入異常處理
  55.                 try
  56.                 {
  57.                     int data;
  58.                     data = serialPort1.ReadByte();
  59.                     string str = Convert.ToString(data, 16).ToUpper();//轉(zhuǎn)換為大寫十六進(jìn)制字符串
  60.                     textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");//空位補(bǔ)“0”                 
  61.                 }
  62.                 catch
  63.                 {
  64.                     this.Close();//關(guān)閉當(dāng)前窗體
  65.                 }

  66.             }
  67.         }

  68.         private void button2_Click(object sender, EventArgs e)
  69.         {
  70.             try
  71.             {
  72.                 serialPort1.Close();//關(guān)閉串口
  73.                 button1.Enabled = true;//打開串口按鈕可用
  74.                 button2.Enabled = false;//關(guān)閉串口按鈕不可用
  75.             }
  76.             catch
  77.             {
  78.                 MessageBox.Show("串口關(guān)閉錯(cuò)誤","錯(cuò)誤");
  79.             }
  80.         }

  81.         private void button3_Click(object sender, EventArgs e)
  82.         {
  83.             byte[] Data = new byte[1];
  84.             if (serialPort1.IsOpen)//判斷串口是否打開,如果打開執(zhí)行下一步操作
  85.             {
  86.                 if (textBox2.Text != "")
  87.                 {
  88.                     if (!radioButton1.Checked)//如果發(fā)送模式是字符模式
  89.                     {
  90.                         try
  91.                         {
  92.                             serialPort1.WriteLine(textBox2.Text);//寫數(shù)據(jù)
  93.                         }
  94.                         catch
  95.                         {
  96.                             MessageBox.Show("串口數(shù)據(jù)寫入錯(cuò)誤", "錯(cuò)誤");//出錯(cuò)提示
  97.                             serialPort1.Close();
  98.                             button1.Enabled = true;//打開串口按鈕可用
  99.                             button2.Enabled = false;//關(guān)閉串口按鈕不可用
  100.                         }
  101.                     }
  102.                     else
  103.                     {
  104.                         for (int i = 0; i < (textBox2.Text.Length - textBox2.Text.Length % 2) / 2; i++)//取余3運(yùn)算作用是防止用戶輸入的字符為奇數(shù)個(gè)
  105.                         {
  106.                             Data[0] = Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16);
  107.                             serialPort1.Write(Data, 0, 1);//循環(huán)發(fā)送(如果輸入字符為0A0BB,則只發(fā)送0A,0B)
  108.                         }
  109.                         if (textBox2.Text.Length % 2 != 0)//剩下一位單獨(dú)處理
  110.                         {
  111.                             Data[0] = Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length - 1, 1), 16);//單獨(dú)發(fā)送B(0B)
  112.                             serialPort1.Write(Data, 0, 1);//發(fā)送
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.         
  119.         private void textBox2_TextChanged(object sender, EventArgs e)
  120.         {
  121.             if (textBox2.Text != "")
  122.                 button3.Enabled = true;//只有當(dāng)發(fā)送文本框內(nèi)的有內(nèi)容時(shí)才可以發(fā)送
  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. }
復(fù)制代碼

所有資料51hei提供下載:
project4.zip (59.25 KB, 下載次數(shù): 50)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:447337 發(fā)表于 2020-1-4 17:30 | 只看該作者
C SHARP 不是VC,VB, 暫時(shí)看不了
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表