• 大小: 31KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-19
  • 语言: C#
  • 标签: 串口  C#  

资源简介

C#实现打开串口,关闭串口,利用串口接收和发送数据,并且能把数据实时显示在屏幕上,实现世博功能!

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;

namespace RFCOMM
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public SerialPort Port=new SerialPort();
        //public delegate void HandleInterfaceUpdataDelegate(string text);
        //private HandleInterfaceUpdataDelegate interfaceUpdataHandle;
         
        private void Form1_Load(object sender EventArgs e)
        {
           // this.CreatFile();//创建文件
            //this.WriteData();
        }

///////////////////////////////////////////////////////////////////////////////////////
        //没用到
        private void portnum_comboBox_SelectedIndexChanged(object sender EventArgs e)
        {

        }

        private void data_textBox_TextChanged(object sender EventArgs e)
        {

        }

        private void baud_comboBox_SelectedIndexChanged(object sender EventArgs e)
        {

        }

///////////////////////////////////////////////////////////////////////////////////
/// 
/// 启动串口
/// 

/// 
/// 
        private void listen_button_Click(object sender EventArgs e)
        {
            Port.PortName = portnum_comboBox.SelectedItem as string;
            Port.Parity = Parity.None;
            Port.BaudRate = Convert.ToInt32(baud_comboBox.SelectedItem);
            Port.StopBits = StopBits.One;
            Port.DataBits = 8;
            Port.ReadBufferSize = 4096;
            Port.DataReceived +=new SerialDataReceivedEventHandler(Port_DataReceived);
            Port.ReceivedBytesThreshold = 1;

            //listen_button.Text = “停止串口“;

            if (Port.PortName.Trim().Equals(string.Empty) || Port.BaudRate<=0)
            {
                information.Text = “请选择正确的端口号和波特率!“;
            }
            else
            {
                try
                {
                    Port.Open();
                    information.Text = “端口打开成功!“;
                }
                catch
                {
                    information.Text = “端口打开失败!“;
                }

            }

        }

        private int result;
        public void Port_DataReceived(object sender System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            byte[] ReadDataBuffer = new byte[100];//new byte[Port.ReadBufferSize];

            result = Port.ReadByte();
            data_textBox.Text = data_textBox.Text+“ “+Convert.ToString(result);// Convert.ToString(ReadDataBuffer);
            
  //写文件
           /* try
            {
                //StreamWriter m_SW = new StreamWriter(@“e:\data.txt“ false);
                m_SW.WriteLine(result);
                m_SW.Close();
      

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       9728  2008-08-02 20:08  RFCOMM1\RFCOMM\bin\Debug\RFCOMM.exe

     文件      24064  2008-08-02 20:08  RFCOMM1\RFCOMM\bin\Debug\RFCOMM.pdb

     目录          0  2008-08-01 10:36  RFCOMM1\RFCOMM\bin\Debug

     目录          0  2008-08-01 10:36  RFCOMM1\RFCOMM\bin

     文件       4650  2008-08-03 11:21  RFCOMM1\RFCOMM\Form1.cs

     文件       8433  2008-08-02 17:14  RFCOMM1\RFCOMM\Form1.Designer.cs

     文件       6300  2008-08-02 17:14  RFCOMM1\RFCOMM\Form1.resx

     目录          0  2004-08-02 09:27  RFCOMM1\RFCOMM\obj\Debug\Refactor

     文件       5053  2008-08-02 18:13  RFCOMM1\RFCOMM\obj\Debug\ResolveAssemblyReference.cache

     文件        842  2008-08-02 17:14  RFCOMM1\RFCOMM\obj\Debug\RFCOMM.csproj.GenerateResource.Cache

     文件       9728  2008-08-02 20:08  RFCOMM1\RFCOMM\obj\Debug\RFCOMM.exe

     文件        180  2008-08-02 17:14  RFCOMM1\RFCOMM\obj\Debug\RFCOMM.Form1.resources

     文件      24064  2008-08-02 20:08  RFCOMM1\RFCOMM\obj\Debug\RFCOMM.pdb

     文件        180  2008-08-02 16:15  RFCOMM1\RFCOMM\obj\Debug\RFCOMM.Properties.Resources.resources

     目录          0  2008-08-01 10:36  RFCOMM1\RFCOMM\obj\Debug\TempPE

     目录          0  2008-08-01 10:36  RFCOMM1\RFCOMM\obj\Debug

     文件        261  2008-08-02 20:08  RFCOMM1\RFCOMM\obj\RFCOMM.csproj.FileList.txt

     目录          0  2008-08-01 10:36  RFCOMM1\RFCOMM\obj

     文件        351  2008-08-01 10:36  RFCOMM1\RFCOMM\Program.cs

     文件       1141  2008-08-01 10:36  RFCOMM1\RFCOMM\Properties\AssemblyInfo.cs

     文件       2347  2008-08-01 10:36  RFCOMM1\RFCOMM\Properties\Resources.Designer.cs

     文件       5618  2008-08-01 10:36  RFCOMM1\RFCOMM\Properties\Resources.resx

     目录          0  2008-08-01 10:36  RFCOMM1\RFCOMM\Properties

     文件       4063  2004-08-02 09:14  RFCOMM1\RFCOMM\RFCOMM.csproj

     文件        234  2008-08-02 20:08  RFCOMM1\RFCOMM\RFCOMM.csproj.user

     目录          0  2008-08-01 10:36  RFCOMM1\RFCOMM

     文件       1073  2008-08-01 10:37  RFCOMM1\RFCOMM.sln

    ..A..H.     13824  2008-08-27 17:16  RFCOMM1\RFCOMM.suo

     目录          0  2008-08-01 10:36  RFCOMM1

----------- ---------  ---------- -----  ----

............此处省略2个文件信息

评论

共有 条评论