|
c#上位機源碼
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO.Ports;
- using System.Text.RegularExpressions;
- namespace COM
- {
- public partial class Form1 : Form
- {
- //聲明了一個delegate 類型
- public delegate void Displaydelegate(byte[] InputBuf);
- //聲明了一個delegate 對象
- public Displaydelegate disp_delegate;
- //將串口接收到的數(shù)據(jù)顯示到textBox1上
- public void DispUI(byte[] InputBuf)
- {
- ASCIIEncoding encoding = new ASCIIEncoding();
- if (radioButton3.Checked)
- {
- foreach (byte b in InputBuf)
- {
- //將數(shù)值轉(zhuǎn)換成16進制數(shù)并追加一個空格并顯示到textBox1上
- textBox1.AppendText(b.ToString("X2") + " ");
- }
- }
- if (radioButton4.Checked)
- {
- //直接將數(shù)值轉(zhuǎn)換成字符串并顯示并顯示到textBox1上
- textBox1.AppendText(encoding.GetString(InputBuf));
- }
- }
- public Form1()
- {
- //創(chuàng)建一個delegate 對象
- disp_delegate = new Displaydelegate(DispUI);
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //獲取端口名字 使用前需要添加 using System.IO.Ports;
- string[] PortName = SerialPort.GetPortNames();
- Array.Sort(PortName);//給端口名稱排序
- for (int i = 0; i < PortName.Length; i++)
- {
- comboBox1.Items.Add(PortName[i]);//給comboBox1添加選項
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.PortName = comboBox1.Text;//更改端口名稱
- //因為存儲在comboBox2中的數(shù)值都為字符串,所以需要將端口號轉(zhuǎn)換為10進制數(shù)值
- serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10);
- serialPort1.Open();//打開串口
- button1.Enabled = false;//"打開串口"按鍵失效
- button2.Enabled = true;//"關(guān)閉串口"按鍵使能
- }
- catch
- {
- MessageBox.Show("端口錯誤,請檢查端口", "錯誤");
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.Close();//關(guān)閉串口
- button1.Enabled = true;//"打開串口"按鍵使能
- button2.Enabled = false;//"關(guān)閉串口"按鍵失效
- }
- catch
- {
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- if (!serialPort1.IsOpen)//如果沒有打開串口就報錯并返回
- {
- MessageBox.Show("串口寫入失敗", "錯誤");
- serialPort1.Close();
- button1.Enabled = true;
- button2.Enabled = false;
- return;
- }
- if (radioButton1.Checked)//如果選擇數(shù)值發(fā)送模式
- {
- List<byte> buf = new List<byte>();//填充到這個臨時列表中
- //使用正則表達式獲取textBox2中的有效數(shù)據(jù)
- MatchCollection mc = Regex.Matches(textBox2.Text, @"(?i)[\da-f]{2}");
- //將mc轉(zhuǎn)換為16進制數(shù)據(jù)并添加到buf列表中
- foreach (Match m in mc)
- {
- byte data = Convert.ToByte(m.Value, 16);
- buf.Add(data);
- }
- //將buf列表轉(zhuǎn)換為數(shù)組并通過串口發(fā)送出去
- serialPort1.Write(buf.ToArray(), 0, buf.Count);
- }
- else//如果選擇字符發(fā)送模式
- {
- serialPort1.WriteLine(textBox2.Text);
- }
- }
- private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- int n = serialPort1.BytesToRead;//串口緩存中數(shù)據(jù)的個數(shù)
- Byte[] InputBuf = new Byte[n];
- try
- {
- //讀取串口緩存中的數(shù)據(jù)并存放到InputBuf數(shù)組中
- serialPort1.Read(InputBuf, 0, serialPort1.BytesToRead);
- //將當(dāng)前線程掛起50ms
- System.Threading.Thread.Sleep(50);
- //執(zhí)行委托
- this.Invoke(disp_delegate, InputBuf);
- }
- catch (TimeoutException ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- }
- }
復(fù)制代碼
全部資料51hei下載地址:
串口助手上位機(C#).zip
(255.13 KB, 下載次數(shù): 107)
2019-9-10 17:21 上傳
點擊文件名下載附件
串口助手上位機(C#)
|
評分
-
查看全部評分
|