• 大小: 38KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-08
  • 语言: C#
  • 标签: C#  COM端口  serialPort  

资源简介

C# 通过COM端口读取数据 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

资源截图

代码片段和文件信息

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;
using System.Threading;

namespace COM
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender EventArgs e)
        {
            initCom();
        }
        /// 
        /// 初始化COM端口
        /// 

        private void initCom()
        {
            ////////////初始化端口
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
            }
            //设置波特率为9600
            serialPort1.BaudRate = 9600;
            //设置端口号,这里使用COM1端口
            serialPort1.PortName = “COM1“;
            //设置 DataReceived 事件发生前内部输入缓冲区中的字节数为8
            serialPort1.ReceivedBytesThreshold = 8;
            //将事件处理方法添加到事件中去
            textChanged += new UpdateTextEventHandler(XXX);
            try
            {
                serialPort1.Open();
            }
            catch (Exception ex)
            {

                MessageBox.Show(“未能打开端口,请检查是否已经连接串口.\n“ + ex.Message);
            }
            
        }
        /// 
        /// 业务、DB等
        /// 

        private void XXX()
        {

        }
        //定义委托
        private delegate void UpdateTextEventHandler();
        //定义事件
        private event UpdateTextEventHandler textChanged;
        /// 
        /// 接收数据
        /// 

        /// 
        /// 
        private void serialPort1_DataReceived(object sender System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            bool b = serialPort1.IsOpen;
            if (!b)
            {
                return;
            }
            int bytes = serialPort1.BytesToRead;
            byte[] receivedData = new byte[bytes];
            Thread.Sleep(300);
            serialPort1.Read(receivedData 0 bytes);

            this.textBox1.Text = byteToHexStr(receivedData);
 
 
            serialPort1.DiscardInBuffer();
            this.Invoke(textChanged);
        }
        /// 
        /// 十六进制转字符串
        /// 

        /// 
        /// 
        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = ““;
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString(“X2“);
                }
            }
            return returnStr;
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2012-09-18 10:03  COM\
     目录           0  2012-09-18 09:56  COM\bin\
     目录           0  2012-09-18 10:04  COM\bin\Debug\
     文件        9728  2012-09-18 10:04  COM\bin\Debug\COM.exe
     文件       22016  2012-09-18 10:04  COM\bin\Debug\COM.pdb
     文件       14328  2012-09-18 10:02  COM\bin\Debug\COM.vshost.exe
     文件         490  2007-07-21 01:33  COM\bin\Debug\COM.vshost.exe.manifest
     文件        3717  2012-09-18 10:01  COM\COM.csproj
     文件         895  2012-09-18 09:56  COM\COM.sln
     文件        7168  2012-09-18 09:56  COM\COM.suo
     文件        3036  2012-09-18 10:03  COM\Form1.cs
     文件        2885  2012-09-18 09:59  COM\Form1.Designer.cs
     文件        6013  2012-09-18 09:59  COM\Form1.resx
     目录           0  2012-09-18 09:56  COM\obj\
     目录           0  2012-09-18 10:04  COM\obj\Debug\
     文件         376  2012-09-18 10:04  COM\obj\Debug\COM.csproj.FileListAbsolute.txt
     文件         847  2012-09-18 10:04  COM\obj\Debug\COM.csproj.GenerateResource.Cache
     文件        9728  2012-09-18 10:04  COM\obj\Debug\COM.exe
     文件         180  2012-09-18 10:04  COM\obj\Debug\COM.Form1.resources
     文件       22016  2012-09-18 10:04  COM\obj\Debug\COM.pdb
     文件         180  2012-09-18 10:04  COM\obj\Debug\COM.Properties.Resources.resources
     目录           0  2012-09-18 09:56  COM\obj\Debug\TempPE\
     文件         484  2012-09-18 09:56  COM\Program.cs
     目录           0  2012-09-18 09:56  COM\Properties\
     文件        1362  2012-09-18 09:56  COM\Properties\AssemblyInfo.cs
     文件        2856  2012-09-18 09:56  COM\Properties\Resources.Designer.cs
     文件        5612  2012-09-18 09:56  COM\Properties\Resources.resx
     文件        1088  2012-09-18 09:56  COM\Properties\Settings.Designer.cs
     文件         249  2012-09-18 09:56  COM\Properties\Settings.settings

评论

共有 条评论