检测 SerialPort 何时断开连接 [英] Detecting when a SerialPort gets disconnected

查看:57
本文介绍了检测 SerialPort 何时断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个打开的 SerialPort 并通过 DataReceived 事件接收数据.

I have an open SerialPort and receive data through the DataReceived event.

有没有办法检测SerialPort 是否断开连接?

Is there any way to detect if the SerialPort gets disconnected?

我尝试了 ErrorReceivedPinChanged 事件,但没有成功.

I tried the ErrorReceived and PinChanged events but no luck.

除此之外,SerialPort.IsOpen 在物理断开连接时返回 true.

In addition to that, SerialPort.IsOpen returns true when physically disconnected.

推荐答案

USB 串行端口是一个巨大的痛苦.例如,请参阅这个问题.我不确定它是否真的是用 .NET 4.0 修复的,但在那天,我试图用这样的东西来处理断开连接使整个程序崩溃的问题:

USB-serial ports are a huge pain. See, for example, this question. I'm not sure whether it really was fixed with .NET 4.0, but back in the day I tried to deal with the problem of disconnections crashing the whole program with something like this:

public class SafeSerialPort : SerialPort
{
    private Stream theBaseStream;

    public SafeSerialPort(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
        : base(portName, baudRate, parity, dataBits, stopBits)
    {

    }

    public new void Open()
    {
        try
        {
            base.Open();
            theBaseStream = BaseStream;
            GC.SuppressFinalize(BaseStream);
        }
        catch
        {

        }
    }

    public new void Dispose()
    {
        Dispose(true);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (base.Container != null))
        {
            base.Container.Dispose();               
        }
        try
        {
            if (theBaseStream.CanRead)
            {
                theBaseStream.Close();
                GC.ReRegisterForFinalize(theBaseStream);
            }
        }
        catch
        {
            // ignore exception - bug with USB - serial adapters.
        }
        base.Dispose(disposing);
    }
}

向我从谁那里改编的道歉,似乎我没有在我的代码中记下它.问题显然源于.NET 如何在串行端口消失的情况下处理底层流.串口断开后好像无法关闭流.

Apologies to whoever I adapted this from, it seems I failed to make a note of it in my code. The problem apparently stemmed from how .NET handled the underlying stream in the case of the serial port disappearing. It seemed you couldn't close the stream after the serial port is disconnected.

我使用的另一个策略是创建一个小程序,它只执行串行通信部分并公开一个 WCF 服务供我的主程序连接.这样,当 USB 串行适配器脱落并导致通信程序崩溃时,我可以从主程序中自动重新启动它.

Another strategy I used was to create a small program that did just the serial communication part and exposed a WCF service for my main program to connect to. That way, when the USB-serial adapter flakes out and crashes the communication program, I can just automatically restart it from my main program.

最后,我不知道为什么没有人销售锁定 USB 端口来避免整个意外断开连接问题,尤其是使用 USB 串行适配器时!

Finally, I don't know why nobody ever marketed a locking USB port to avoid the whole accidental disconnection problem, especially with USB-serial adapters!

这篇关于检测 SerialPort 何时断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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