• 大小: 51KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-05-09
  • 语言: C#
  • 标签: SerialPort  

资源简介

支持串口大量数据读写操作,能一次性读取数据,通过设置超时大小控制一次读取串口所有数据。支持事件触发,异步发送接收数据。附件有demo,可下载Virtual Serial Port Driver模拟串口测试。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YS.IO.Ports;

namespace SerialPortTest
{
    public partial class Form1 : Form
    {
        SerialPortListener m_SerialPort = null;

        SerialPort m_s = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender EventArgs e)
        {
            string[] portlist = SerialPort.GetPortNames();
        }

        private void btnListen_Click(object sender EventArgs e)
        {
            if (m_SerialPort == null)
            {
                m_SerialPort = new SerialPortListener(tbPort.Text Convert.ToInt32(tbFrequency.Text));
                m_SerialPort.Parity = Parity.None;
                m_SerialPort.StopBits = StopBits.One;
                m_SerialPort.Handshake = Handshake.None;
                m_SerialPort.DataBits = 8;
                m_SerialPort.ReadBufferSize = 100;
                m_SerialPort.ReceivedBytesThreshold = 1;
                m_SerialPort.BufferSize = 4;
                m_SerialPort.ReceiveTimeout = Convert.ToInt32(tbTimeout.Text);
                m_SerialPort.WriteBufferSize = 100;
                m_SerialPort.SendInterval = 100;
                m_SerialPort.SerialPortResult += new HandResult(SerialPort_Result);
                m_SerialPort.OnSerialPortReceived += new OnReceivedData(SerialPort_Received);
                m_SerialPort.OnSeriaPortSend += new OnSendData(SerialPort_Send);
            }

            if (m_SerialPort.IsOpen)
            {
                m_SerialPort.Stop();
                btnListen.Text = “启动监听“;
            }
            else
            {
                m_SerialPort.Start();
                btnListen.Text = “关闭监听“;
            }
        }

        void SerialPort_Result(object senderSerialPortEvents e)
        {
            this.Invoke(new MethodInvoker(() => {
                //处理结果
                rtbResult.Text += Encoding.GetEncoding(“GB2312“).GetString(e.BufferData);
            }));
        }

        void SerialPort_Received(object sender SerialPortEvents e)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                long receivedCount = Convert.ToInt64(lbReceivedCount.Text);
                if (e.BufferData != null)
                {
                    receivedCount += e.BufferData.Length;
                }
                lbReceivedCount.Text = receivedCount.ToString();
            }));
        }

        void SerialPort_Send(object sender SerialPortEvents e)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                long sendCount = Convert.ToInt64(lbSendCount.Text);
                if (e.BufferData != null)
        

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-10-16 12:06  SerialPortTest\
     目录           0  2016-10-16 21:58  SerialPortTest\SerialPortTest\
     文件        1480  2016-10-16 12:06  SerialPortTest\SerialPortTest.sln
     文件       73216  2016-10-16 21:59  SerialPortTest\SerialPortTest.v12.suo
     文件         187  2016-10-16 00:00  SerialPortTest\SerialPortTest\App.config
     目录           0  2016-10-16 21:58  SerialPortTest\SerialPortTest\bin\
     目录           0  2016-10-16 21:58  SerialPortTest\SerialPortTest\bin\Debug\
     文件         187  2016-10-16 00:00  SerialPortTest\SerialPortTest\bin\Debug\SerialPortTest.exe.config
     文件       24224  2016-10-16 21:58  SerialPortTest\SerialPortTest\bin\Debug\SerialPortTest.vshost.exe
     文件         187  2016-10-16 00:00  SerialPortTest\SerialPortTest\bin\Debug\SerialPortTest.vshost.exe.config
     文件         490  2010-03-17 22:39  SerialPortTest\SerialPortTest\bin\Debug\SerialPortTest.vshost.exe.manifest
     文件        3633  2016-10-16 21:48  SerialPortTest\SerialPortTest\Form1.cs
     文件        9733  2016-10-16 21:26  SerialPortTest\SerialPortTest\Form1.Designer.cs
     文件        5817  2016-10-16 21:26  SerialPortTest\SerialPortTest\Form1.resx
     目录           0  2016-10-16 00:00  SerialPortTest\SerialPortTest\obj\
     目录           0  2016-10-16 21:53  SerialPortTest\SerialPortTest\obj\Debug\
     文件         865  2016-10-16 00:00  SerialPortTest\SerialPortTest\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7064  2016-10-16 12:07  SerialPortTest\SerialPortTest\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         934  2016-10-16 21:58  SerialPortTest\SerialPortTest\obj\Debug\SerialPortTest.csproj.FileListAbsolute.txt
     文件           0  2016-10-16 00:00  SerialPortTest\SerialPortTest\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2016-10-16 00:00  SerialPortTest\SerialPortTest\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
     文件           0  2016-10-16 00:00  SerialPortTest\SerialPortTest\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
     目录           0  2016-10-16 00:00  SerialPortTest\SerialPortTest\obj\Debug\TempPE\
     文件         526  2016-10-16 00:00  SerialPortTest\SerialPortTest\Program.cs
     目录           0  2016-10-16 00:00  SerialPortTest\SerialPortTest\Properties\
     文件        1370  2016-10-16 00:00  SerialPortTest\SerialPortTest\Properties\AssemblyInfo.cs
     文件        2880  2016-10-16 00:00  SerialPortTest\SerialPortTest\Properties\Resources.Designer.cs
     文件        5612  2016-10-16 00:00  SerialPortTest\SerialPortTest\Properties\Resources.resx
     文件        1101  2016-10-16 00:00  SerialPortTest\SerialPortTest\Properties\Settings.Designer.cs
     文件         249  2016-10-16 00:00  SerialPortTest\SerialPortTest\Properties\Settings.settings
     文件        4011  2016-10-16 12:07  SerialPortTest\SerialPortTest\SerialPortTest.csproj
............此处省略18个文件信息

评论

共有 条评论