debugging SerialPort.BaseStream.BeginRead的正确用法是什么?

zzlelutf  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(152)

我有一个程序的问题,它从串行端口获取不可靠的数据,我相信www.example.com
这篇文章有一些答案-bytestorread值似乎不稳定,回调并不总是在数据存在时调用,所有默认的串行端口函数都不可靠,包括datareceived事件。这正是我在这个程序中遇到的。事实上,在微软的串口文档中到处都有声明和失败功能的注解。它没有的是这些问题的解决方案当它们出现在你的应用程序中时。
然而,当我尝试使用本文中提供的解决方案时,它似乎总是输出相同的第一个字节:"pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp我不知道。据我所知,这是完全相同的解决方案中提出的文章。这里是代码:

private void Form1_Shown(object sender, EventArgs e)
    {
        try
        {
            sp.Open();
            SPDataHelper();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

private void SPDataHelper()
    {
        byte[] buffer = new byte[8000];
        Action kickoffRead = null;

        kickoffRead = delegate
        {
            sp.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate (IAsyncResult ar)
            {
                try
                {
                    int actualLength = sp.BaseStream.EndRead(ar);
                    byte[] received = new byte[actualLength];
                    Buffer.BlockCopy(buffer, 0, received, 0, actualLength);
                    sp_DataReceived(System.Text.Encoding.UTF8.GetString(received));//not called by the actual serialport anymore
                }
                catch (IOException exc)
                {
                    WriteUUTWindow("Exception: "+exc.ToString());
                }
                kickoffRead();
            }, null);
        };
        kickoffRead();
    }

字符串
当我在"actuallength"赋值后设置断点时,它显示为1,这表明它在第一个字节之后永远不会读取。此后,赋值和回调的断点都不会再次到达,但应用程序继续发送"p"垃圾邮件。你知道这是怎么回事吗?

iyr7buue

iyr7buue1#

如果您对使用BeginRead感兴趣,请尝试以下操作-它已通过条形码扫描仪测试:
创建一个类(名称:HelperSerialPort.cs)

选项1
HelperSerialPort.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Ports;
using System.Diagnostics;

namespace SerialPortTest
{
    public enum PortBaudRate : int
    {
        Baud1200 = 1200,
        Baud2400 = 2400,
        Baud4800 = 4800,
        Baud9600 = 9600,
        Baud14400 = 14400,
        Baud19200 = 19200,
        Baud28800 = 28800,
        Baud38400 = 38400,
        Baud56000 = 56000,
        Baud57600 = 57600,
        Baud76800 = 76800,
        Baud115200 = 115200,
        Baud128000 = 128000,
        Baud230400 = 230400,
        Baud250000 = 250000,
        Baud256000 = 256000
    };

    public class HelperSerialPort
    {
        public SerialPort Port { get; private set; } = null;

        private string _dataReceived = string.Empty;

        public HelperSerialPort(string portName, PortBaudRate baudRate = PortBaudRate.Baud9600)
        {
            Initialize(portName, baudRate);
        }

        private void Initialize(string portName, PortBaudRate baudRate = PortBaudRate.Baud9600)
        {
            //create new instance
            this.Port = new SerialPort();

            //set properties
            Port.BaudRate = (int)baudRate;
            Port.DataBits = 8;
            Port.Parity = Parity.None; //use 'None' when DataBits = 8; if DataBits = 7, use 'Even' or 'Odd'
            Port.DtrEnable = true; //enable Data Terminal Ready
            Port.Handshake = Handshake.None;
            Port.PortName = portName;
            Port.ReadTimeout = 200; //used when using ReadLine
            Port.RtsEnable = true; //enable Request to send
            Port.StopBits = StopBits.One;
            Port.WriteTimeout = 50;

            //open
            Port.Open();

            Listen();
        }

        private void Listen()
        {
            byte[] buffer = new byte[65536];

            IAsyncResult result = Port.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate (IAsyncResult ar)
            {
                try
                {
                    if (Port.IsOpen)
                    {
                        int bytesRead = Port.BaseStream.EndRead(ar);

                        byte[] received = new byte[bytesRead];
                        Buffer.BlockCopy(buffer, 0, received, 0, bytesRead);

                        _dataReceived = System.Text.Encoding.UTF8.GetString(received);
                        Debug.WriteLine("Info: " + DateTime.Now.ToString("HH:mm:ss:fff") + " - _dataReceived: " + _dataReceived);

                        Listen();
                    }
                    
                }
                catch (IOException ex)
                {
                    Debug.WriteLine("Error (Listen) - " + ex.Message);
                }
            }, null);
        }

        public void Dispose()
        {
            if (Port != null)
            {
                Port.Close();
                Port.Dispose();

                Port = null;
            }
        }
    }
}

字符串

更新

这里有另一个版本,它似乎也工作,它使用了稍微修改过的代码版本。

选项2
HelperSerialPort.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Ports;
using System.Diagnostics;

namespace SerialPortTest
{
    public enum PortBaudRate : int
    {
        Baud1200 = 1200,
        Baud2400 = 2400,
        Baud4800 = 4800,
        Baud9600 = 9600,
        Baud14400 = 14400,
        Baud19200 = 19200,
        Baud28800 = 28800,
        Baud38400 = 38400,
        Baud56000 = 56000,
        Baud57600 = 57600,
        Baud76800 = 76800,
        Baud115200 = 115200,
        Baud128000 = 128000,
        Baud230400 = 230400,
        Baud250000 = 250000,
        Baud256000 = 256000
    };

    public class HelperSerialPort
    {
        public SerialPort Port { get; private set; } = null;

        private string _dataReceived = string.Empty;

        public HelperSerialPort(string portName, PortBaudRate baudRate = PortBaudRate.Baud9600)
        {
            Initialize(portName, baudRate);
        }

        private void Initialize(string portName, PortBaudRate baudRate = PortBaudRate.Baud9600)
        {
            //create new instance
            this.Port = new SerialPort();

            //set properties
            Port.BaudRate = (int)baudRate;
            Port.DataBits = 8;
            Port.Parity = Parity.None; //use 'None' when DataBits = 8; if DataBits = 7, use 'Even' or 'Odd'
            Port.DtrEnable = true; //enable Data Terminal Ready
            Port.Handshake = Handshake.None;
            Port.PortName = portName;
            Port.ReadTimeout = 200; //used when using ReadLine
            Port.RtsEnable = true; //enable Request to send
            Port.StopBits = StopBits.One;
            Port.WriteTimeout = 50;

            //open
            Port.Open();

            SPDataHelper();
        }

        private void SPDataHelper()
        {
            byte[] buffer = new byte[65536];
            Action kickoffRead = null;

            kickoffRead = delegate
            {
                IAsyncResult result = Port.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate (IAsyncResult ar)
                {
                    try
                    {
                        if (Port.IsOpen)
                        {
                            int bytesRead = Port.BaseStream.EndRead(ar);

                            byte[] received = new byte[bytesRead];
                            Buffer.BlockCopy(buffer, 0, received, 0, bytesRead);

                            _dataReceived = System.Text.Encoding.UTF8.GetString(received);
                            Debug.WriteLine("Info: " + DateTime.Now.ToString("HH:mm:ss:fff") + " - _dataReceived: " + _dataReceived);

                            kickoffRead();
                        }

                    }
                    catch (IOException ex)
                    {
                        Debug.WriteLine("Error (SPDataHelper) - " + ex.Message);
                    }
                }, null);
            };

            kickoffRead();
        }

        public void Dispose()
        {
            if (Port != null)
            {
                Port.Close();
                Port.Dispose();

                Port = null;
            }
        }
    }
}

备选方案3

下面是一个使用SerialPort DataReceived的选项。

HelperSerialPort.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Ports;
using System.Diagnostics;

namespace SerialPortTest
{
    public enum PortBaudRate : int
    {
        Baud1200 = 1200,
        Baud2400 = 2400,
        Baud4800 = 4800,
        Baud9600 = 9600,
        Baud14400 = 14400,
        Baud19200 = 19200,
        Baud28800 = 28800,
        Baud38400 = 38400,
        Baud56000 = 56000,
        Baud57600 = 57600,
        Baud76800 = 76800,
        Baud115200 = 115200,
        Baud128000 = 128000,
        Baud230400 = 230400,
        Baud250000 = 250000,
        Baud256000 = 256000
    };

    public class HelperSerialPort : IDisposable 
    {
        public SerialPort Port { get; private set; } = null;

        private string _dataReceived = string.Empty;

        public HelperSerialPort(string portName, PortBaudRate baudRate = PortBaudRate.Baud9600)
        {
            Initialize(portName, baudRate);  
        }

        private void Initialize(string portName, PortBaudRate baudRate = PortBaudRate.Baud9600)
        {
            //create new instance
            this.Port = new SerialPort();

            //set properties
            Port.BaudRate = (int)baudRate;
            Port.DataBits = 8;
            Port.Parity = Parity.None; //use 'None' when DataBits = 8; if DataBits = 7, use 'Even' or 'Odd'
            Port.DtrEnable = true; //enable Data Terminal Ready
            Port.Handshake = Handshake.None;
            Port.PortName = portName;
            Port.ReadTimeout = 200; //used when using ReadLine
            Port.RtsEnable = true; //enable Request to send
            Port.StopBits = StopBits.One;
            Port.WriteTimeout = 50;

            //subscribe to events
            Port.DataReceived += Port_DataReceived;
            Port.ErrorReceived += Port_ErrorReceived;

            //open
            Port.Open();

        }

        private void Port_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
        {
            Debug.WriteLine("Error: (sp_ErrorReceived) - " + e.EventType);
        }

        private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            _dataReceived = Port.ReadExisting();
            Debug.WriteLine("Info: " + DateTime.Now.ToString("HH:mm:ss:fff") + " - _dataReceived: " + _dataReceived);
        }

        public void Dispose()
        {
            if (Port != null)
            {
                //unsubscribe from events
                Port.DataReceived -= Port_DataReceived;
                Port.ErrorReceived -= Port_ErrorReceived;

                Port.Close();
                Port.Dispose();

                Port = null;
            }
        }
    }
}

用法

HelperSerialPort helper = new HelperSerialPort("COM1");

注意:确保完成SerialPort后调用Dispose

相关问题