最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c# 實(shí)現(xiàn)簡(jiǎn)單的串口通訊

 更新時(shí)間:2021年03月12日 09:03:55   作者:月井石  
這篇文章主要介紹了c# 如何實(shí)現(xiàn)簡(jiǎn)單的串口通訊,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

本文提供一個(gè)用C#實(shí)現(xiàn)串口通訊實(shí)例,親自編寫(xiě),親測(cè)可用!

開(kāi)發(fā)環(huán)境:

VS2008+.net FrameWork3.5(實(shí)際上2.0應(yīng)該也可以)

第一步

創(chuàng)建一個(gè)WinForm窗體,拉入一些界面元素

重點(diǎn)就是,圖中用紅框標(biāo)出的,工具箱——組件——SerialPort,做.net串口通訊,這是必備控件

第二步

設(shè)置SerialPort控件屬性

用C#向串口發(fā)送數(shù)據(jù)沒(méi)什么特別的,就是調(diào)用SerialPort的Write方法往串口寫(xiě)數(shù)據(jù)就行

但是從串口那里接收數(shù)據(jù)的方式就比較特別了

首先,需要在代碼里聲明一個(gè)特別的事件函數(shù)

private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  {
   this.Invoke(new EventHandler(UpdateUIText));
  }

此函數(shù)是用來(lái)綁定到SerialPort控件的DataReceived事件

顧名思義,這個(gè)事件就是在接收到串口返回的數(shù)據(jù)時(shí)觸發(fā),里面就一句代碼

對(duì)這句代碼有興趣的可以私下再去研究,這里就不贅述了

總之,這句代碼的用途就是用來(lái)調(diào)動(dòng)另一個(gè)函數(shù),對(duì)界面UI元素的值進(jìn)行更新(當(dāng)然你也可以在里面執(zhí)行其他操作)

private void UpdateUIText(object s, EventArgs e)
  {
   try
   {
    //必須要阻塞線(xiàn)程一段時(shí)間,以免在交易超時(shí)的情況下,由于read太快導(dǎo)致讀取不完整
    System.Threading.Thread.Sleep(500);
    string txt = serialPort.ReadExisting();
    txt_Received.Text = txt;
   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message.ToString());
   }   
  }

第三步

開(kāi)始寫(xiě)邏輯代碼,廢話(huà)不多說(shuō),直接貼上來(lái)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WinForm串口通訊
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
             
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            if (ports.Length == 0)
            {
                MessageBox.Show("本機(jī)沒(méi)有串口!");
            }
            Array.Sort(ports);
            serialPort.PortName = ports[0];//串口號(hào)COM3
            serialPort.BaudRate = 115200;//波特率
            serialPort.DataBits = 8;//數(shù)據(jù)位
            serialPort.StopBits = System.IO.Ports.StopBits.One;//停止位
            serialPort.Encoding = System.Text.Encoding.GetEncoding("GB2312");//此行非常重要,解決接收中文亂碼的問(wèn)題
 
            // 打開(kāi)串口
            try
            {
                serialPort.Open();
            }
            catch (Exception ex)
            {
                //捕獲到異常信息,創(chuàng)建一個(gè)新的comm對(duì)象,之前的不能用了。  
                serialPort = new System.IO.Ports.SerialPort();
                //將異常信息傳遞給用戶(hù)。  
                MessageBox.Show(ex.Message);
                return;
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {          
            string msgOrder = txt_Msg.Text;
            //MessageBox.Show(msgOrder);
            serialPort.Write(msgOrder);           
        }
 
 
        private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            this.Invoke(new EventHandler(UpdateUIText));
        }
 
        private void UpdateUIText(object s, EventArgs e)
        {
            try
            {
                //必須要阻塞線(xiàn)程一段時(shí)間,以免在交易超時(shí)的情況下,由于read太快導(dǎo)致讀取不完整
                System.Threading.Thread.Sleep(500);
                string txt = serialPort.ReadExisting();
                txt_Received.Text = txt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
             
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (serialPort != null && serialPort.IsOpen)
                {
                    serialPort.Close();
                    serialPort.Dispose();
                }
            }
            catch (Exception ex)
            {
                //將異常信息傳遞給用戶(hù)。  
                MessageBox.Show(ex.Message);
                return;
            }
        }
 
         
    }
}

至此,一個(gè)簡(jiǎn)單完整的串口通訊就完成了,希望對(duì)你們有所幫助

以上就是c# 實(shí)現(xiàn)簡(jiǎn)單的串口通訊的詳細(xì)內(nèi)容,更多關(guān)于c# 串口通訊的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

洛浦县| 剑川县| 年辖:市辖区| 西峡县| 仙居县| 新津县| 阜康市| 房产| 高碑店市| 应用必备| 临泉县| 贵阳市| 石阡县| 神农架林区| 江华| 溧阳市| 铁岭市| 林周县| 呼伦贝尔市| 海门市| 盐津县| 河西区| 花垣县| 富顺县| 三江| 天全县| 榆社县| 安塞县| 平果县| 闽侯县| 利辛县| 平原县| 巴南区| 临清市| 林口县| 四子王旗| 余庆县| 墨竹工卡县| 招远市| 金川县| 兰溪市|