5分鐘用C#實(shí)現(xiàn)串口助手
嵌入式開發(fā)中,由于產(chǎn)品的綁定、驗證等邏輯限制比較嚴(yán)重,需要自己做一個上位機(jī)工具,來實(shí)現(xiàn)USB/BT通訊工具,實(shí)現(xiàn)如串口通訊、OTA升級等功能。
開發(fā)之初,比較了下C#和QT的環(huán)境,還是C#在window環(huán)境下開發(fā)更為簡單,qt往往還需要自己解決Windows環(huán)境配置問題。
第一步,創(chuàng)建新項目,選擇Windows窗體應(yīng)用

如果你沒有這個選項,說明你沒有安裝.net框架,打開visual studio install,選擇修改,安裝一下,具體見下圖。

按照下圖,選擇最新的.NET框架,點(diǎn)擊創(chuàng)建。

第二步,點(diǎn)擊工具箱,拖拽控件,搭建一下頁面

最終參考的頁面如下:
5個button,1個comboBox,2個textBox。

第三步,拖入serial port控件,并添加回調(diào)函數(shù)
工具箱中搜索serial port,并拖進(jìn)來。
雙擊“掃描串口”按鈕,出現(xiàn)如下頁面。

copy如下代碼:
public Form1()
{
InitializeComponent();
serialPort1.DataReceived += new SerialDataReceivedEventHandler(SerialReceiveCallback);
serialPort1.Encoding = Encoding.UTF8;
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
public void SerialReceiveCallback(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (serialPort1.BytesToRead == 0)
return;
//接收字節(jié)序列直接處理
byte[] bytes = new byte[serialPort1.BytesToRead];
serialPort1.Read(bytes, 0, bytes.Length);
//或者再轉(zhuǎn)成字符串進(jìn)行打印\處理
string receive_data = System.Text.Encoding.Default.GetString(bytes);
textBox1.AppendText(receive_data + "\r\n");
}
catch
{
MessageBox.Show("數(shù)據(jù)接收出現(xiàn)異常");
}
}第四步,實(shí)現(xiàn)按鈕功能
1、掃描功能
需要引用系統(tǒng)管理模塊,如下圖。
using System.IO.Ports; using System.Management;

copy如下代碼:
private void button3_Click(object sender, EventArgs e)//打開串口按鈕
{
try
{
if (button3.Text == "打開串口")
{
button3.Text = "關(guān)閉串口";
serialPort1.Close();
string[] sArray = comboBox1.Text.Split(new string[] { " " }, 2, StringSplitOptions.RemoveEmptyEntries);
serialPort1.PortName = sArray[0];
serialPort1.BaudRate = 115200;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.DataBits = 8;
serialPort1.Open();
textBox1.AppendText("打開串口成功\r\n");
}
else
{
button3.Text = "打開串口";
textBox1.AppendText("串口已關(guān)閉\r\n");
if (serialPort1.IsOpen == false)
return;
else
serialPort1.Close();
}
}
catch {
button3.Text = "打開串口";
MessageBox.Show("打開串口失敗");
}
}
private void button2_Click(object sender, EventArgs e)//發(fā)送數(shù)據(jù)
{
if (textBox2.Text.Length == 0)
{
MessageBox.Show("請在文本框中輸入數(shù)據(jù)");
}
else
{
serialPort1.Write(textBox2.Text);
}
}
private void button4_Click(object sender, EventArgs e)//清空接收區(qū)按鈕
{
textBox1.Clear();
}
private void button5_Click(object sender, EventArgs e)//清空發(fā)送區(qū)按鈕
{
textBox2.Clear();
}2、發(fā)送數(shù)據(jù)功能
參考如上代碼,只需要調(diào)用接口進(jìn)行寫入。
最終實(shí)現(xiàn)效果如下

非常簡單的擴(kuò)展框架
比如,我要實(shí)現(xiàn)一個OTA功能。
完全可以由SerialReceiveCallback來驅(qū)動,不需要任何多線程和定時器。
1)在SerialReceiveCallback函數(shù)中處理接收的byte[]字節(jié)數(shù)據(jù),根據(jù)下位機(jī)的響應(yīng)來執(zhí)行下一步操作,直至OTA結(jié)束。
2)做一個OTA功能按鈕,只負(fù)責(zé)發(fā)送第一條命令,用于啟動OTA交互。
3)OTA文件可以做一個listbox,實(shí)現(xiàn)文件拖入和選擇功能,去解析和讀取hex或bin文件數(shù)據(jù),組包封包發(fā)送給下位機(jī)。
對于如何“解析和讀取hex或bin文件數(shù)據(jù)”,可參考我的上一篇文章。
參考代碼如下:
記得listbox屬性中的“allowDrop”選項為true,否則無法拖入。
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e) //選擇鼠標(biāo)雙擊事件
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = false;
dialog.Title = "請選擇升級文件";
dialog.Filter = "hex格式(*.hex)|*.hex";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string strPath = dialog.FileName;
listBox1.Items.Clear();
listBox1.Items.Add(strPath);
}
}
private void listBox1_DragDrop(object sender, DragEventArgs e)//選擇文件拖入完成事件
{
string strPath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
if (strPath.Contains(".hex"))
{
listBox1.Items.Clear();
listBox1.Items.Add(strPath);
}
else
{
MessageBox.Show("文件格式錯誤");
}
}
private void listBox1_DragEnter(object sender, DragEventArgs e)//選擇文件拖入事件
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.All;
}
else
{
e.Effect = DragDropEffects.None;
}
}到此這篇關(guān)于5分鐘用C#實(shí)現(xiàn)串口助手的文章就介紹到這了,更多相關(guān)C# 串口助手內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c#將字節(jié)數(shù)組轉(zhuǎn)成易讀的字符串的實(shí)現(xiàn)
這篇文章主要介紹了c#將字節(jié)數(shù)組轉(zhuǎn)成易讀的字符串的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
在.NET框架使用C#實(shí)現(xiàn)PDF文件轉(zhuǎn)為HTML格式的步驟
HTML作為一種開放標(biāo)準(zhǔn)的網(wǎng)頁標(biāo)記語言,具有跨平臺、易于瀏覽和搜索引擎友好的特性,通過將PDF文件轉(zhuǎn)換為HTML格式,我們可以更方便地在瀏覽器中展示PDF文檔內(nèi)容,本文將介紹如何在.NET框架使用C#將PDF文件轉(zhuǎn)換為HTML格式,需要的朋友可以參考下2025-01-01
C#實(shí)現(xiàn)帶引導(dǎo)窗體的窗體設(shè)計操作流程
很多時候,我們的窗體設(shè)計需要一個引導(dǎo)窗體,當(dāng)打開一個項目的窗體時,默認(rèn)的是先打開一個歡迎或介紹項目信息的引導(dǎo)窗體,幾秒鐘后再打開項目的主窗體,本文給大家介紹了C#實(shí)現(xiàn)帶引導(dǎo)窗體的窗體設(shè)計操作流程,感興趣的朋友可以參考下2024-04-04

