欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
串口助手上位機(C#源碼)
[打印本頁]
作者:
flyfengyuzhou
時間:
2019-9-10 17:22
標題:
串口助手上位機(C#源碼)
c#上位機源碼
0.png
(4.48 KB, 下載次數: 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
{
//聲明了一個delegate 類型
public delegate void Displaydelegate(byte[] InputBuf);
//聲明了一個delegate 對象
public Displaydelegate disp_delegate;
//將串口接收到的數據顯示到textBox1上
public void DispUI(byte[] InputBuf)
{
ASCIIEncoding encoding = new ASCIIEncoding();
if (radioButton3.Checked)
{
foreach (byte b in InputBuf)
{
//將數值轉換成16進制數并追加一個空格并顯示到textBox1上
textBox1.AppendText(b.ToString("X2") + " ");
}
}
if (radioButton4.Checked)
{
//直接將數值轉換成字符串并顯示并顯示到textBox1上
textBox1.AppendText(encoding.GetString(InputBuf));
}
}
public Form1()
{
//創建一個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中的數值都為字符串,所以需要將端口號轉換為10進制數值
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10);
serialPort1.Open();//打開串口
button1.Enabled = false;//"打開串口"按鍵失效
button2.Enabled = true;//"關閉串口"按鍵使能
}
catch
{
MessageBox.Show("端口錯誤,請檢查端口", "錯誤");
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
serialPort1.Close();//關閉串口
button1.Enabled = true;//"打開串口"按鍵使能
button2.Enabled = false;//"關閉串口"按鍵失效
}
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)//如果選擇數值發送模式
{
List<byte> buf = new List<byte>();//填充到這個臨時列表中
//使用正則表達式獲取textBox2中的有效數據
MatchCollection mc = Regex.Matches(textBox2.Text, @"(?i)[\da-f]{2}");
//將mc轉換為16進制數據并添加到buf列表中
foreach (Match m in mc)
{
byte data = Convert.ToByte(m.Value, 16);
buf.Add(data);
}
//將buf列表轉換為數組并通過串口發送出去
serialPort1.Write(buf.ToArray(), 0, buf.Count);
}
else//如果選擇字符發送模式
{
serialPort1.WriteLine(textBox2.Text);
}
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int n = serialPort1.BytesToRead;//串口緩存中數據的個數
Byte[] InputBuf = new Byte[n];
try
{
//讀取串口緩存中的數據并存放到InputBuf數組中
serialPort1.Read(InputBuf, 0, serialPort1.BytesToRead);
//將當前線程掛起50ms
System.Threading.Thread.Sleep(50);
//執行委托
this.Invoke(disp_delegate, InputBuf);
}
catch (TimeoutException ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
復制代碼
全部資料51hei下載地址:
串口助手上位機(C#).zip
(255.13 KB, 下載次數: 107)
2019-9-10 17:21 上傳
點擊文件名下載附件
串口助手上位機(C#)
下載積分: 黑幣 -5
作者:
jzdcff
時間:
2019-9-17 16:53
很有幫助,多謝分享!
作者:
xfxz0514
時間:
2019-12-15 22:37
感謝分享,謝謝
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1