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

QQ登錄

只需一步,快速開始

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

c#串口助手源碼 SerialCommunicate

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
分享學(xué)習(xí)一下


單片機(jī)源程序如下:
  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 SerialCommunicate
  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);//十進(jìn)制數(shù)據(jù)轉(zhuǎn)換
  25.                 serialPort1.Open();
  26.                 button1.Enabled = false;//打開串口按鈕不可用
  27.                 button2.Enabled = true;//關(guān)閉串口
  28.             }
  29.             catch {
  30.                 MessageBox.Show("端口錯(cuò)誤,請(qǐng)檢查串口", "錯(cuò)誤");
  31.             }
  32.         }

  33.         private void Form1_Load(object sender, EventArgs e)
  34.         {
  35.             for (int i = 1; i < 20; i++)
  36.             {
  37.                 comboBox1.Items.Add("COM" + i.ToString());
  38.             }
  39.             comboBox1.Text = "COM1";//串口號(hào)多額默認(rèn)值
  40.             comboBox2.Text = "9600";//波特率默認(rèn)值
  41.             
  42.             /*****************非常重要************************/
  43.             
  44.             serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);//必須手動(dòng)添加事件處理程序

  45.         }

  46.         private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口數(shù)據(jù)接收事件
  47.         {
  48.             if (!radioButton3.Checked)//如果接收模式為字符模式
  49.             {
  50.                 string str = serialPort1.ReadExisting();//字符串方式讀
  51.                 textBox1.AppendText(str);//添加內(nèi)容
  52.             }
  53.             else { //如果接收模式為數(shù)值接收
  54.                 byte data;
  55.                 data = (byte)serialPort1.ReadByte();//此處需要強(qiáng)制類型轉(zhuǎn)換,將(int)類型數(shù)據(jù)轉(zhuǎn)換為(byte類型數(shù)據(jù),不必考慮是否會(huì)丟失數(shù)據(jù)
  56.                 string str = Convert.ToString(data, 16).ToUpper();//轉(zhuǎn)換為大寫十六進(jìn)制字符串
  57.                 textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");//空位補(bǔ)“0”   
  58.                 //上一句等同為:if(str.Length == 1)
  59.                 //                  str = "0" + str;
  60.                 //              else
  61.                 //                  str = str;
  62.                 //              textBox1.AppendText("0x" + str);
  63.             }
  64.         }

  65.         private void radioButton2_CheckedChanged(object sender, EventArgs e)
  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 (Exception err)//一般情況下關(guān)閉串口不會(huì)出錯(cuò),所以不需要加處理程序
  77.             {

  78.             }
  79.         }

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

  117.     }
  118. }
復(fù)制代碼

所有資料51hei提供下載:
SerialCommunicate.zip (238.24 KB, 下載次數(shù): 49)


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

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

使用道具 舉報(bào)

沙發(fā)
ID:642557 發(fā)表于 2019-11-15 13:00 | 只看該作者
感謝分享,學(xué)習(xí)了
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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