资源简介

在做项目时,发现C#串口经常会发生丢帧的情况,在发送/接收大量数据时,这种状况尤为明显。因此,在对比了各种缓存处理办法的基础上,做了这个小程序。经过实测,完全可以应对48byte/5ms的数据量。这个小程序只做了接收部分,仅供参考~

资源截图

代码片段和文件信息

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

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

       /*************
         设定完整数据帧长度为48字节,帧头为0xAA0x55        
        *********************/

        const int Flen = 48;//完整数据帧长度为48字节根据具体需求更改

        SerialPort sp = new SerialPort();
        byte head_rx = 0 tail_rx = 0 ptr_rx = 0;
        byte[] BufRx = new byte[256];

      
        private void Form1_Load(object sender EventArgs e)
        {
            sp.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
            //准备就绪              
            sp.DtrEnable = true;
            sp.RtsEnable = true;
            //设置数据读取超时为1秒
            sp.ReadTimeout = 1000;

            sp.Close();
        }

        private void btn_ComOK_Click(object sender EventArgs e)
        {
            if (!sp.IsOpen)//如果串口没有打开
            {
                try
                {
                    //设置串口号
                    string serialName = cb_Port.Text;
                    sp.PortName = serialName;

                    string strBaudRate = cb_Baud.Text;

                    string strDateBits = cb_Data.Text;

                    string strStopBits = cb_Stop.Text;

                    Int32 iBaudRate = Convert.ToInt32(strBaudRate);
                    Int32 iDateBits = Convert.ToInt32(strDateBits);

                    sp.BaudRate = iBaudRate;       //波特率
                    sp.DataBits = iDateBits;       //数据位

                    switch (strStopBits)            //停止位
                    {
                        case “1“:
                            sp.StopBits = StopBits.One;
                            break;
                        case “1.5“:
                            sp.StopBits = StopBits.OnePointFive;
                            break;
                        case “2“:
                            sp.StopBits = StopBits.Two;
                            break;
                        default:
                            MessageBox.Show(“Error1:停止位参数不正确!“ “Error“);
                            break;
                    }
                    switch (cb_Parity.Text)             //校验位
                    {
                        case “无校验“:
                            sp.Parity = Parity.None;
                            break;
                        case “奇校验“:
                            sp.Parity = Parity.Odd;
                            break;
                        case “偶校验“:
                            sp.Parity = Parity.Even;
                            break;
                        default:
                            MessageBox

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-08-09 10:56  SerialMassData\
     目录           0  2018-08-09 10:04  SerialMassData\SerialMassData\
     文件         187  2018-08-09 10:04  SerialMassData\SerialMassData\App.config
     文件       14842  2018-08-09 10:12  SerialMassData\SerialMassData\Form1.Designer.cs
     文件        8159  2018-08-09 10:55  SerialMassData\SerialMassData\Form1.cs
     文件        6217  2018-08-09 10:12  SerialMassData\SerialMassData\Form1.resx
     文件         526  2018-08-09 10:04  SerialMassData\SerialMassData\Program.cs
     目录           0  2018-08-09 10:04  SerialMassData\SerialMassData\Properties\
     文件        1366  2018-08-09 10:04  SerialMassData\SerialMassData\Properties\AssemblyInfo.cs
     文件        2880  2018-08-09 10:04  SerialMassData\SerialMassData\Properties\Resources.Designer.cs
     文件        5612  2018-08-09 10:04  SerialMassData\SerialMassData\Properties\Resources.resx
     文件        1101  2018-08-09 10:04  SerialMassData\SerialMassData\Properties\Settings.Designer.cs
     文件         249  2018-08-09 10:04  SerialMassData\SerialMassData\Properties\Settings.settings
     文件        3807  2018-08-09 10:06  SerialMassData\SerialMassData\SerialMassData.csproj
     目录           0  2018-08-09 10:04  SerialMassData\SerialMassData\bin\
     目录           0  2018-08-09 10:29  SerialMassData\SerialMassData\bin\Debug\
     文件       16384  2018-08-09 10:54  SerialMassData\SerialMassData\bin\Debug\SerialMassData.exe
     文件         187  2018-08-09 10:04  SerialMassData\SerialMassData\bin\Debug\SerialMassData.exe.config
     文件       30208  2018-08-09 10:54  SerialMassData\SerialMassData\bin\Debug\SerialMassData.pdb
     文件       22984  2018-08-09 10:57  SerialMassData\SerialMassData\bin\Debug\SerialMassData.vshost.exe
     文件         187  2018-08-09 10:04  SerialMassData\SerialMassData\bin\Debug\SerialMassData.vshost.exe.config
     文件         490  2010-03-17 22:39  SerialMassData\SerialMassData\bin\Debug\SerialMassData.vshost.exe.manifest
     目录           0  2018-08-09 10:04  SerialMassData\SerialMassData\obj\
     目录           0  2018-08-09 10:56  SerialMassData\SerialMassData\obj\Debug\
     文件        1308  2018-08-09 10:56  SerialMassData\SerialMassData\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7039  2018-08-09 10:04  SerialMassData\SerialMassData\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         180  2018-08-09 10:29  SerialMassData\SerialMassData\obj\Debug\SerialMassData.Form1.resources
     文件         180  2018-08-09 10:29  SerialMassData\SerialMassData\obj\Debug\SerialMassData.Properties.Resources.resources
     文件         906  2018-08-09 10:57  SerialMassData\SerialMassData\obj\Debug\SerialMassData.csproj.FileListAbsolute.txt
     文件         975  2018-08-09 10:29  SerialMassData\SerialMassData\obj\Debug\SerialMassData.csproj.GenerateResource.Cache
     文件        2209  2018-08-09 10:48  SerialMassData\SerialMassData\obj\Debug\SerialMassData.csprojResolveAssemblyReference.cache
............此处省略9个文件信息

评论

共有 条评论