找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4686|回復(fù): 2
收起左側(cè)

串口助手上位機(C#源碼)

[復(fù)制鏈接]
ID:610116 發(fā)表于 2019-9-10 17:22 | 顯示全部樓層 |閱讀模式
c#上位機源碼
0.png
  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.Threading.Tasks;
  9. using System.Windows.Forms;

  10. using System.IO.Ports;
  11. using System.Text.RegularExpressions;

  12. namespace COM
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         //聲明了一個delegate 類型
  17.         public delegate void Displaydelegate(byte[] InputBuf);
  18.         //聲明了一個delegate 對象
  19.         public Displaydelegate disp_delegate;
  20.         //將串口接收到的數(shù)據(jù)顯示到textBox1上
  21.         public void DispUI(byte[] InputBuf)
  22.         {

  23.             ASCIIEncoding encoding = new ASCIIEncoding();
  24.             if (radioButton3.Checked)
  25.             {
  26.                 foreach (byte b in InputBuf)
  27.                 {
  28.                     //將數(shù)值轉(zhuǎn)換成16進制數(shù)并追加一個空格并顯示到textBox1上
  29.                     textBox1.AppendText(b.ToString("X2") + " ");
  30.                 }
  31.             }
  32.             if (radioButton4.Checked)
  33.             {
  34.                 //直接將數(shù)值轉(zhuǎn)換成字符串并顯示并顯示到textBox1上
  35.                 textBox1.AppendText(encoding.GetString(InputBuf));
  36.             }

  37.         }


  38.         public Form1()
  39.         {
  40.             //創(chuàng)建一個delegate 對象
  41.             disp_delegate = new Displaydelegate(DispUI);
  42.             InitializeComponent();
  43.         }

  44.         private void Form1_Load(object sender, EventArgs e)
  45.         {
  46.             //獲取端口名字 使用前需要添加 using System.IO.Ports;
  47.             string[] PortName = SerialPort.GetPortNames();
  48.             Array.Sort(PortName);//給端口名稱排序
  49.             for (int i = 0; i < PortName.Length; i++)
  50.             {
  51.                 comboBox1.Items.Add(PortName[i]);//給comboBox1添加選項
  52.             }
  53.         }

  54.         private void button1_Click(object sender, EventArgs e)
  55.         {
  56.             try
  57.             {
  58.                 serialPort1.PortName = comboBox1.Text;//更改端口名稱
  59.                 //因為存儲在comboBox2中的數(shù)值都為字符串,所以需要將端口號轉(zhuǎn)換為10進制數(shù)值
  60.                 serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10);
  61.                 serialPort1.Open();//打開串口
  62.                 button1.Enabled = false;//"打開串口"按鍵失效
  63.                 button2.Enabled = true;//"關(guān)閉串口"按鍵使能
  64.             }
  65.             catch
  66.             {
  67.                 MessageBox.Show("端口錯誤,請檢查端口", "錯誤");
  68.             }
  69.         }
  70.         private void button2_Click(object sender, EventArgs e)
  71.         {
  72.             try
  73.             {
  74.                 serialPort1.Close();//關(guān)閉串口
  75.                 button1.Enabled = true;//"打開串口"按鍵使能
  76.                 button2.Enabled = false;//"關(guān)閉串口"按鍵失效
  77.             }
  78.             catch
  79.             {

  80.             }
  81.         }

  82.         private void button3_Click(object sender, EventArgs e)
  83.         {
  84.             if (!serialPort1.IsOpen)//如果沒有打開串口就報錯并返回
  85.             {
  86.                 MessageBox.Show("串口寫入失敗", "錯誤");
  87.                 serialPort1.Close();
  88.                 button1.Enabled = true;
  89.                 button2.Enabled = false;
  90.                 return;
  91.             }
  92.             if (radioButton1.Checked)//如果選擇數(shù)值發(fā)送模式
  93.             {
  94.                 List<byte> buf = new List<byte>();//填充到這個臨時列表中
  95.                 //使用正則表達式獲取textBox2中的有效數(shù)據(jù)
  96.                 MatchCollection mc = Regex.Matches(textBox2.Text, @"(?i)[\da-f]{2}");
  97.                 //將mc轉(zhuǎn)換為16進制數(shù)據(jù)并添加到buf列表中
  98.                 foreach (Match m in mc)
  99.                 {
  100.                     byte data = Convert.ToByte(m.Value, 16);
  101.                     buf.Add(data);
  102.                 }
  103.                 //將buf列表轉(zhuǎn)換為數(shù)組并通過串口發(fā)送出去
  104.                 serialPort1.Write(buf.ToArray(), 0, buf.Count);
  105.             }
  106.             else//如果選擇字符發(fā)送模式
  107.             {
  108.                 serialPort1.WriteLine(textBox2.Text);
  109.             }
  110.         }

  111.         private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  112.         {
  113.             int n = serialPort1.BytesToRead;//串口緩存中數(shù)據(jù)的個數(shù)
  114.             Byte[] InputBuf = new Byte[n];
  115.             try
  116.             {
  117.                 //讀取串口緩存中的數(shù)據(jù)并存放到InputBuf數(shù)組中
  118.                 serialPort1.Read(InputBuf, 0, serialPort1.BytesToRead);
  119.                 //將當(dāng)前線程掛起50ms
  120.                 System.Threading.Thread.Sleep(50);
  121.                 //執(zhí)行委托
  122.                 this.Invoke(disp_delegate, InputBuf);
  123.             }
  124.             catch (TimeoutException ex)
  125.             {
  126.                 MessageBox.Show(ex.ToString());
  127.             }
  128.         }

  129.     }
  130. }
復(fù)制代碼


全部資料51hei下載地址:
串口助手上位機(C#).zip (255.13 KB, 下載次數(shù): 107)

評分

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

查看全部評分

回復(fù)

使用道具 舉報

ID:544352 發(fā)表于 2019-9-17 16:53 | 顯示全部樓層
很有幫助,多謝分享!
回復(fù)

使用道具 舉報

ID:664669 發(fā)表于 2019-12-15 22:37 | 顯示全部樓層
感謝分享,謝謝
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

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