欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

5分鐘用C#實現(xiàn)串口助手

 更新時間:2022年07月22日 10:37:48   作者:柿子風年  
本文主要介紹了C#實現(xiàn)串口助手,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

嵌入式開發(fā)中,由于產(chǎn)品的綁定、驗證等邏輯限制比較嚴重,需要自己做一個上位機工具,來實現(xiàn)USB/BT通訊工具,實現(xiàn)如串口通訊、OTA升級等功能。

開發(fā)之初,比較了下C#和QT的環(huán)境,還是C#在window環(huán)境下開發(fā)更為簡單,qt往往還需要自己解決Windows環(huán)境配置問題。

第一步,創(chuàng)建新項目,選擇Windows窗體應用

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

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

第二步,點擊工具箱,拖拽控件,搭建一下頁面

最終參考的頁面如下:

5個button,1個comboBox,2個textBox。

第三步,拖入serial port控件,并添加回調函數(shù)

工具箱中搜索serial port,并拖進來。

雙擊“掃描串口”按鈕,出現(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);
 
		//或者再轉成字符串進行打印\處理
		string receive_data = System.Text.Encoding.Default.GetString(bytes);
		textBox1.AppendText(receive_data + "\r\n");
	}
	catch
	{
		MessageBox.Show("數(shù)據(jù)接收出現(xiàn)異常");
	}
}

第四步,實現(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 = "關閉串口";
			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("串口已關閉\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ù)功能

參考如上代碼,只需要調用接口進行寫入。

最終實現(xiàn)效果如下

非常簡單的擴展框架

比如,我要實現(xiàn)一個OTA功能。

完全可以由SerialReceiveCallback來驅動,不需要任何多線程和定時器。

1)在SerialReceiveCallback函數(shù)中處理接收的byte[]字節(jié)數(shù)據(jù),根據(jù)下位機的響應來執(zhí)行下一步操作,直至OTA結束。

2)做一個OTA功能按鈕,只負責發(fā)送第一條命令,用于啟動OTA交互。

3)OTA文件可以做一個listbox,實現(xiàn)文件拖入和選擇功能,去解析和讀取hex或bin文件數(shù)據(jù),組包封包發(fā)送給下位機。

對于如何“解析和讀取hex或bin文件數(shù)據(jù)”,可參考我的上一篇文章。

參考代碼如下:

記得listbox屬性中的“allowDrop”選項為true,否則無法拖入。

private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e) //選擇鼠標雙擊事件
{
	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;
	}
}

到此這篇關于5分鐘用C#實現(xiàn)串口助手的文章就介紹到這了,更多相關C# 串口助手內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論