如何更可靠地使用 SerialPort 类 [英] How to use SerialPort class more reliably

查看:55
本文介绍了如何更可靠地使用 SerialPort 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在与我设计的某些外部硬件进行通信的应用程序中,我一直在使用 SerialPort 类一段时间.在调试上述硬件的过程中,我发现了一些不可靠的东西,最近偶然发现了this 这似乎是在做某事.

I've been using the SerialPort class for a little while in an application that communicates with some external hardware that I've designed. During the debugging process of said hardware, I found a few things to be unreliable, and I recently stumbled across this which seems to be onto something.

我正在尝试将其实现到我的应用程序中,但是我有两个关于接收数据的问题...

I'm trying to implement this into my application, however I have two questions about receiving data...

因此,在我链接的文章中,作者提到 DataReceived 事件有问题,并展示了如何使用它的典型代码示例...

So, in the article I linked, the author mentions that the DataReceived event is problematic, and shows a typical code sample of how it could be used...

port.DataReceived += port_DataReceived;

// (later, in DataReceived event)
try {
    byte [] buffer = new byte[port.BytesToRead];
    port.Read(buffer, 0, buffer.Length);
    raiseAppSerialDataEvent(buffer);
}
catch (IOException exc) {
    handleAppSerialError(exc);
}

然后展示了作者认为正确的方法...

And then what the author considers to be the correct method is shown...

byte[] buffer = new byte[blockLimit];
Action kickoffRead = null;

kickoffRead = delegate {
port.BaseStream.BeginRead(buffer, 0, buffer.Length, 
        delegate (IAsyncResult ar) {
            try {
                int actualLength = port.BaseStream.EndRead(ar);
                byte[] received = new byte[actualLength];
                Buffer.BlockCopy(buffer, 0, received, 0, actualLength);
                raiseAppSerialDataEvent(received);
            }
            catch (IOException exc) {
                handleAppSerialError(exc);
            }
            kickoffRead();
        }, null);
    };
    kickoffRead();

我的问题围绕着 BaseStream.BeginRead 的使用;应该把这个放在我班级的什么地方来读取数据?我的第一个想法是在 DataReceived 事件中,正如在第一个示例中显示如何不使用 SerialPort 类,作者提到代码在 DataReceived 事件在评论中,但对于显示更好方式的示例,作者没有提到代码应该在哪里,所以我认为他仍然指的是 DataReceived 事件,但是作者提到 DataReceived 事件本身有问题,所以...?这里的任何指导都很棒,如果很明显,请道歉!

My questions revolve around the use of BaseStream.BeginRead; where abouts in my class that would be reading the data should this be put? My first thought was in the DataReceived event, as in the first example showing how NOT to use the SerialPort class the author mentions that the code is in the DataReceived event in the comment, but for the sample showing a better way the author makes no mention of where the code should be so I thought he was still referring to the DataReceived event, but then the author mentioned that the DataReceived event itself has problems, so...? Any guidance here would be great, and appologies if its obvious!

如果我没有提到对任何试图回答这个问题的人有益的事情,那么请务必让我知道.提前感谢您的任何指导和/或反馈!

If I've not mentioned something that would benefit anyone trying to answer this then by all means let me know. Thanks in advance for any guidance and/ or feedback!

推荐答案

这是一个递归函数,所以你在打开端口后只调用一次这段代码,它会继续重复自己而不阻塞执行(或需要一个 DataReceived 事件处理程序).

It's a recursive function, so you call this code only ONCE after opening the port, and it will continue to repeat itself without blocking execution (or requiring a DataReceived event handler).

这篇关于如何更可靠地使用 SerialPort 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆