標(biāo)題:
串口助手上位機(jī)(C#源碼)
[打印本頁]
作者:
flyfengyuzhou
時(shí)間:
2019-9-10 17:22
標(biāo)題:
串口助手上位機(jī)(C#源碼)
c#上位機(jī)源碼
0.png
(4.48 KB, 下載次數(shù): 75)
下載附件
2019-9-10 21:20 上傳
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
{
//聲明了一個(gè)delegate 類型
public delegate void Displaydelegate(byte[] InputBuf);
//聲明了一個(gè)delegate 對(duì)象
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進(jìn)制數(shù)并追加一個(gè)空格并顯示到textBox1上
textBox1.AppendText(b.ToString("X2") + " ");
}
}
if (radioButton4.Checked)
{
//直接將數(shù)值轉(zhuǎn)換成字符串并顯示并顯示到textBox1上
textBox1.AppendText(encoding.GetString(InputBuf));
}
}
public Form1()
{
//創(chuàng)建一個(gè)delegate 對(duì)象
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添加選項(xiàng)
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
serialPort1.PortName = comboBox1.Text;//更改端口名稱
//因?yàn)榇鎯?chǔ)在comboBox2中的數(shù)值都為字符串,所以需要將端口號(hào)轉(zhuǎn)換為10進(jìn)制數(shù)值
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10);
serialPort1.Open();//打開串口
button1.Enabled = false;//"打開串口"按鍵失效
button2.Enabled = true;//"關(guān)閉串口"按鍵使能
}
catch
{
MessageBox.Show("端口錯(cuò)誤,請(qǐng)檢查端口", "錯(cuò)誤");
}
}
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)//如果沒有打開串口就報(bào)錯(cuò)并返回
{
MessageBox.Show("串口寫入失敗", "錯(cuò)誤");
serialPort1.Close();
button1.Enabled = true;
button2.Enabled = false;
return;
}
if (radioButton1.Checked)//如果選擇數(shù)值發(fā)送模式
{
List<byte> buf = new List<byte>();//填充到這個(gè)臨時(shí)列表中
//使用正則表達(dá)式獲取textBox2中的有效數(shù)據(jù)
MatchCollection mc = Regex.Matches(textBox2.Text, @"(?i)[\da-f]{2}");
//將mc轉(zhuǎn)換為16進(jìn)制數(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ù)的個(gè)數(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下載地址:
串口助手上位機(jī)(C#).zip
(255.13 KB, 下載次數(shù): 107)
2019-9-10 17:21 上傳
點(diǎn)擊文件名下載附件
串口助手上位機(jī)(C#)
下載積分: 黑幣 -5
作者:
jzdcff
時(shí)間:
2019-9-17 16:53
很有幫助,多謝分享!
作者:
xfxz0514
時(shí)間:
2019-12-15 22:37
感謝分享,謝謝
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1