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

基于C#實(shí)現(xiàn)串口通信

 更新時(shí)間:2022年02月17日 12:50:35   作者:Zed_H  
這篇文章主要為大家詳細(xì)介紹了基于C#實(shí)現(xiàn)串口通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#實(shí)現(xiàn)串口通信的具體代碼,供大家參考,具體內(nèi)容如下

1.基本概念

2.前端winForm布局如下(僅僅為了實(shí)現(xiàn)功能,布局略丑)

3.代碼實(shí)現(xiàn)如下

namespace SerialPortTest
? {
? ? ? public partial class Form1 : Form
? ? ? {
? ? ? ? ? SerialPort sp1 = new SerialPort();
? ? ? ? ? public Form1()
? ? ? ? ? {
? ? ? ? ? ? ? InitializeComponent();
? ? ? ? ? }
??
? ? ? ? ? private void Form1_Load(object sender, EventArgs e)
? ? ? ? ? {
       ?//分別對(duì)應(yīng)前端的波特率、數(shù)字位、校驗(yàn)位、停止位
? ? ? ? ? ? ? cbBaudRate.SelectedIndex = 0;
? ? ? ? ? ? ? cbDataBits.SelectedIndex = 0;
? ? ? ? ? ? ? cbCheck.SelectedIndex = 0;
? ? ? ? ? ? ? cbStop.SelectedIndex = 0;
??
? ? ? ? ? ? ? string[] strCom = SerialPort.GetPortNames();
? ? ? ? ? ? ? if (strCom == null)
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? MessageBox.Show("本機(jī)沒有串口!", "Error");
? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? }
? ? ? ? ? ? ? //GetPortNames()方法:獲取當(dāng)前計(jì)算機(jī)的串行端口名的數(shù)組
? ? ? ? ? ? ? foreach (string com in System.IO.Ports.SerialPort.GetPortNames())
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? cbCom.Items.Add(com);
? ? ? ? ? ? ? }
??
? ? ? ? ? ? ? cbCom.SelectedIndex = 0;
? ? ? ? ? ? ? sp1.BaudRate = 9600;
? ? ? ? ? ? ? Control.CheckForIllegalCrossThreadCalls = false;
? ? ? ? ? ? ? sp1.DataReceived += Sp1_DataReceived;
? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? sp1.DtrEnable = true;//獲取或設(shè)置一個(gè)值,該值在串行通信過程中啟用數(shù)據(jù)終端就緒 (DTR) 信號(hào)。
? ? ? ? ? ? ? sp1.RtsEnable = true;//獲取或設(shè)置一個(gè)值,該值指示在串行通信中是否啟用請(qǐng)求發(fā)送 (RTS) 信號(hào)
? ? ? ? ? ? ? //設(shè)置數(shù)據(jù)讀取超時(shí)為1秒
? ? ? ? ? ? ? sp1.ReadTimeout = 1000;
??
? ? ? ? ? ? ? sp1.Close();
? ? ? ? ? }
??
? ? ? ? ? private void Sp1_DataReceived(object sender, SerialDataReceivedEventArgs e)
? ? ? ? ? {
? ? ? ? ? ? ? if (sp1.IsOpen) ? ? //判斷是否打開串口
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? //輸出當(dāng)前時(shí)間
? ? ? ? ? ? ? ? ? DateTime dt = DateTime.Now;
? ? ? ? ? ? ? ? ? txtReceived.Text += dt.GetDateTimeFormats('f')[0].ToString() + "\r\n";
? ? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? Byte[] receivedData = new Byte[sp1.BytesToRead]; ? ? ? ?//創(chuàng)建接收字節(jié)數(shù)組
? ? ? ? ? ? ? ? ? ? ? sp1.Read(receivedData, 0, receivedData.Length); ? ? ? ? //讀取數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? AddContent(new UTF8Encoding().GetString(receivedData));//用萬能的UTF8可以傳輸中文不會(huì)亂碼
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ?catch (System.Exception ex)
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? MessageBox.Show(ex.Message, "出錯(cuò)提示!!!!!");
? ? ? ? ? ? ? ? ? ? ? txtSendStr.Text = "";
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
? ? ? ? ? ? ? else
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? MessageBox.Show("請(qǐng)打開某個(gè)串口", "錯(cuò)誤提示");
? ? ? ? ? ? ? }
? ? ? ? ? }
??
? ? ? ? ? //將接受到的內(nèi)容顯示出來
? ? ? ? ? private void AddContent(string content)
? ? ? ? ? {
? ? ? ? ? ? ? this.BeginInvoke(new MethodInvoker(delegate
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? txtReceived.AppendText(content);
? ? ? ? ? ? ? ? ? txtReceived.AppendText("\r\n");
? ? ? ? ? ? ? ? ? //記錄收到的字符個(gè)數(shù)
? ? ? ? ? ? ? ? ? lblRevCount.Text = (int.Parse(lblRevCount.Text) + content.Length).ToString();
? ? ? ? ? ? ? }));
? ? ? ? ? }
??
? ? ? ? ? private void btnOpen_Click(object sender, EventArgs e)
? ? ? ? ? {
? ? ? ? ? ? ? //serialPort1.IsOpen
? ? ? ? ? ? ? if (!sp1.IsOpen)
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? //設(shè)置串口號(hào)
? ? ? ? ? ? ? ? ? ? ? string serialName = cbCom.SelectedItem.ToString();
? ? ? ? ? ? ? ? ? ? ? sp1.PortName = serialName;
??
? ? ? ? ? ? ? ? ? ? ? //設(shè)置各“串口設(shè)置”
? ? ? ? ? ? ? ? ? ? ? string strBaudRate = cbBaudRate.Text;
? ? ? ? ? ? ? ? ? ? ? string strDateBits = cbDataBits.Text;
? ? ? ? ? ? ? ? ? ? ? string strStopBits = cbStop.Text;
? ? ? ? ? ? ? ? ? ? ? Int32 iBaudRate = Convert.ToInt32(strBaudRate);
? ? ? ? ? ? ? ? ? ? ? Int32 iDateBits = Convert.ToInt32(strDateBits);
?
? ? ? ? ? ? ? ? ? ? ?sp1.BaudRate = iBaudRate; ? ? ? //波特率
? ? ? ? ? ? ? ? ? ? ?sp1.DataBits = iDateBits; ? ? ? //數(shù)據(jù)位
? ? ? ? ? ? ? ? ? ? ?switch (cbStop.Text) ? ? ? ? ? ?//停止位
? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ?case "1":
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sp1.StopBits = StopBits.One;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ? ?case "1.5":
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sp1.StopBits = StopBits.OnePointFive;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ? ?case "2":
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sp1.StopBits = StopBits.Two;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ? ?default:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?MessageBox.Show("Error:參數(shù)不正確!", "Error");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ?switch (cbCheck.Text) ? ? ? ? ? ? //校驗(yàn)位
? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ?case "無":
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sp1.Parity = Parity.None;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ? ?case "奇校驗(yàn)":
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sp1.Parity = Parity.Odd;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ? ?case "偶校驗(yàn)":
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sp1.Parity = Parity.Even;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ? ?default:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?MessageBox.Show("Error:參數(shù)不正確!", "Error");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ?}
?
? ? ? ? ? ? ? ? ? ? ?if (sp1.IsOpen == true)//如果打開狀態(tài),則先關(guān)閉一下
? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ?sp1.Close();
? ? ? ? ? ? ? ? ? ? ?}
?
? ? ? ? ? ? ? ? ? ? ?//設(shè)置必要控件不可用
? ? ? ? ? ? ? ? ? ? ?cbCom.Enabled = false;
? ? ? ? ? ? ? ? ? ? ?cbBaudRate.Enabled = false;
? ? ? ? ? ? ? ? ? ? ?cbDataBits.Enabled = false;
? ? ? ? ? ? ? ? ? ? ?cbStop.Enabled = false;
? ? ? ? ? ? ? ? ? ? ?cbCheck.Enabled = false;
? ? ? ? ? ? ? ? ? ? ?sp1.Open(); ? ? //打開串口
? ? ? ? ? ? ? ? ? ? ?btnOpen.Text = "關(guān)閉串口";
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ?catch (System.Exception ex)
? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ?MessageBox.Show("Error:" + ex.Message, "Error");
? ? ? ? ? ? ? ? ? ? ?return;
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ?}
? ? ? ? ? ? ?else
? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ?//恢復(fù)控件功能
? ? ? ? ? ? ? ? ?//設(shè)置必要控件不可用
? ? ? ? ? ? ? ? ?cbCom.Enabled = true;
? ? ? ? ? ? ? ? ?cbBaudRate.Enabled = true;
? ? ? ? ? ? ? ? ?cbDataBits.Enabled = true;
? ? ? ? ? ? ? ? ?cbStop.Enabled = true;
? ? ? ? ? ? ? ? ?cbCheck.Enabled = true;
? ? ? ? ? ? ? ? ?sp1.Close(); ? ? ? ? ? ? ? ? ? ?//關(guān)閉串口
? ? ? ? ? ? ? ? ?btnOpen.Text = "打開串口";
? ? ? ? ? ? ?}
? ? ? ? ?}
?
? ? ? ? ?private void btnSend_Click(object sender, EventArgs e)
? ? ? ? ?{
? ? ? ? ? ? ?byte[] sendData = null;
? ? ? ? ? ? ?if (!sp1.IsOpen) //如果沒打開
? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ?MessageBox.Show("請(qǐng)先打開串口!", "Error");
? ? ? ? ? ? ? ? ?return;
? ? ? ? ? ? ?}
? ? ? ? ? ? ?String strSend = txtSendStr.Text;
? ? ? ? ? ? ?try
? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ?sendData = Encoding.UTF8.GetBytes(txtSendStr.Text.Trim());
? ? ? ? ? ? ? ? //sp1.WriteLine(txtSendStr.Text); ? ?//寫入數(shù)據(jù)
? ? ? ? ? ? ? ? ?sp1.Write(sendData, 0, sendData.Length);
? ? ? ? ? ? ? }
? ? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? MessageBox.Show("Error:" + ex.Message, "Error");
}
?
}
?
}
}

4.測(cè)試運(yùn)行結(jié)果如下

在自己同一臺(tái)電腦上測(cè)試,需要先用Configure Virtual Serial Port Driver建立兩個(gè)虛擬串口,如下

串口運(yùn)行結(jié)果如下:

上述兩窗體通信時(shí)要選擇同一波特率,不然收發(fā)數(shù)據(jù)會(huì)失敗

關(guān)于C# serialport的一些說明:

SerialPort() :如果未指定,則此構(gòu)造函數(shù)使用默認(rèn)屬性值。 例如, DataBits 屬性默認(rèn)值為 8, Parity 屬性默認(rèn)為 None 枚舉值,
StopBits 屬性默認(rèn)值為 1,默認(rèn)端口名為 COM1。

public static string[] GetPortNames() :獲取當(dāng)前計(jì)算機(jī)的串行端口名的數(shù)組

SerialPort.Read 方法 (Byte[], Int32, Int32) :從 SerialPort 輸入緩沖區(qū)讀取一些字節(jié)并將那些字節(jié)寫入字節(jié)數(shù)組中指定的偏移量處

SerialPort.ReadLine 方法 () :一直讀取到輸入緩沖區(qū)中的 NewLine 值

SerialPort.Write 方法 (Byte[], Int32, Int32) : 使用緩沖區(qū)中的數(shù)據(jù)將指定數(shù)量的字節(jié)寫入串行端口

SerialPort.WriteLine 方法 (String) : 將指定的字符串和 NewLine 值寫入輸出緩沖區(qū)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#操作注冊(cè)表的方法

    C#操作注冊(cè)表的方法

    這篇文章介紹了C#操作注冊(cè)表的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • c#刪除代碼中的單行注釋行示例

    c#刪除代碼中的單行注釋行示例

    本文提供了c#刪除代碼中的單行注釋行的示例,還可以看到文件流的使用方法,大家參考使用吧
    2014-01-01
  • 使用C#實(shí)現(xiàn)在word中插入頁眉頁腳的方法

    使用C#實(shí)現(xiàn)在word中插入頁眉頁腳的方法

    這篇文章主要介紹了使用C#實(shí)現(xiàn)在word中插入頁眉頁腳的方法,是操作Word的常見方法,有一定的學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下
    2014-08-08
  • C# 中使用正則表達(dá)式匹配字符的含義

    C# 中使用正則表達(dá)式匹配字符的含義

    正則表達(dá)式的作用用來描述字符串的特征。本文重點(diǎn)給大家介紹C# 中使用正則表達(dá)式匹配字符的含義,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-10-10
  • 詳解c#索引(Index)和范圍(Range)

    詳解c#索引(Index)和范圍(Range)

    這篇文章主要介紹了c#索引(Index)和范圍(Range)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下
    2020-10-10
  • C#隱式運(yùn)行CMD命令(隱藏命令窗口)

    C#隱式運(yùn)行CMD命令(隱藏命令窗口)

    這篇文章主要介紹了C#隱式運(yùn)行CMD命令(隱藏命令窗口),本文實(shí)現(xiàn)在winform窗口中運(yùn)行CMD命令,需要的朋友可以參考下
    2015-06-06
  • C#實(shí)現(xiàn)IP代理池調(diào)度的示例代碼

    C#實(shí)現(xiàn)IP代理池調(diào)度的示例代碼

    這篇文章主要為大家介紹了C#實(shí)現(xiàn)IP代理池調(diào)度的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的參考與學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下
    2023-07-07
  • C#實(shí)現(xiàn)獲取電腦硬件顯卡核心代號(hào)信息

    C#實(shí)現(xiàn)獲取電腦硬件顯卡核心代號(hào)信息

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)獲取電腦硬件顯卡核心代號(hào)信息,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • C#定時(shí)每天00點(diǎn)00分00秒自動(dòng)重啟軟件

    C#定時(shí)每天00點(diǎn)00分00秒自動(dòng)重啟軟件

    這篇文章主要為大家詳細(xì)介紹了C#定時(shí)每天00點(diǎn)00分00秒自動(dòng)重啟軟件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • C#根據(jù)反射和特性實(shí)現(xiàn)ORM映射實(shí)例分析

    C#根據(jù)反射和特性實(shí)現(xiàn)ORM映射實(shí)例分析

    這篇文章主要介紹了C#根據(jù)反射和特性實(shí)現(xiàn)ORM映射的方法,實(shí)例分析了反射的原理、特性與ORM的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04

最新評(píng)論