串行端口在捕获到异常后关闭后需要花费大量时间才能重新打开自身 [英] serial port taking a huge amount of time to reopen itself after closing by catching an exception

查看:190
本文介绍了串行端口在捕获到异常后关闭后需要花费大量时间才能重新打开自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我试图通过这样的串行端口发送数据来捕获超时异常,

 ReadTimeout = 1000'//1000毫秒= 1秒
        WriteTimeout = 1000'//1000毫秒= 1秒

        尝试
            如果COMPort.IsOpen然后
                COMPort.Write(dataToSend)
            万一

        异常捕获
                COMPort.DiscardInBuffer()
                COMPort.DiscardOutBuffer()

                调暗为新线程(AddressOf ClosePort)
                线程睡眠(1000)
                t.Start()
        结束尝试

  私人子ClosePort()
        如果COMPort.IsOpen然后
            COMPort.DiscardInBuffer()
            COMPort.DiscardOutBuffer()

            尝试
                COMPort.Close()

            异常捕获
                MsgBox(例如消息)
                MsgBox(例如StackTrace)
            结束尝试
        万一

    结束子

通过捕获超时异常,我能够立即关闭端口,但是当我再次重新打开serialPort时,我必须等待大约1-2分钟,然后端口才会重新打开.因为我是新手,所以我阅读了文档并找出了该serialPort 它是不受管理的资源,因此它不受垃圾收集器的直接控制

我找到了一个名为  ReleaseCOMObject(o作为对象)作为整数.我试图像这样使用此API,

将WithEvents COMPort调暗为新的SerialPort

    私有子版本ReleaseCOMObject(obj作为对象)
        Dim countDown为Int32
        countDown = 1

        虽然(倒数> 0)
            countDown = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        结束时间

    结束子

  私人子ClosePort()
        如果COMPort.IsOpen然后
            COMPort.DiscardInBuffer()
            COMPort.DiscardOutBuffer()

            尝试
                ReleaseCOMObject(COMPort)
                COMPort.Close()

            异常捕获
                MsgBox(例如消息)
                MsgBox(例如StackTrace)
            结束尝试
        万一

    结束

API接受此COMPort作为SerialPort对象,但是当我发布并运行此代码时,它给了我ArgumentException.这是下面的例外情况,

 mscorlib.dll中发生了'System.ArgumentException'类型的未处理异常

附加信息:对象的类型必须是__ComObject或从__ComObject派生.

我发现还有另一种方法来实现IDisposable接口,但是如何在我的Form类中实现此接口,该类已经由Visual Studio在Form1.Designer.vb文件中实现. Visual Studio提供的代码是 在下面给出,

'表单替代物用于清理组件列表.
    < System.Diagnostics.DebuggerNonUserCode()> _
    受保护的重写子Dispose(ByVal以布尔方式进行处置)
        尝试
            如果处置AndAlso组件不算什么,那么
                components.Dispose()
            万一
        最后
            MyBase.Dispose(处理)
        结束尝试
    结束子

在这方面的任何帮助将不胜感激.

解决方案

通过捕获超时异常,我能够立即关闭端口,但是当我再次重新打开serialPort时,我必须等待大约1-2分钟才能重新打开端口.

关闭并重新打开COM端口与管理资源和垃圾收集无关.除非您期望的错误可能以某种方式破坏了实例的实例,否则无需考虑处理它. 零件.在这种情况下,请使用该类中的Dispose方法.

写入错误通常不需要关闭并重新打开端口-丢弃缓冲区并重试写入应该足够了.您的错误陷阱应具体说明您是哪种可能的错误.处理- 只有TimeoutEception需要特殊处理-在调用Write方法之前,应先捕获其他可能的错误.

如果需要关闭端口然后重新打开,则重新打开端口的延迟可能是由于另一端的设备未能正确设置控制信号(这也可能是原始超时的原因) ).


Hi everyone,

I am trying to catch a time-out exception by sending data through serial port like this,

        ReadTimeout = 1000     '// 1000 milliseconds = 1 seconds
        WriteTimeout = 1000    '// 1000 milliseconds = 1 seconds

        Try
            If COMPort.IsOpen Then
                COMPort.Write(dataToSend)
            End If

        Catch ex As Exception
                COMPort.DiscardInBuffer()
                COMPort.DiscardOutBuffer()

                Dim t As New Thread(AddressOf ClosePort)
                Thread.Sleep(1000)
                t.Start()
        End Try

  Private Sub ClosePort()
        If COMPort.IsOpen Then
            COMPort.DiscardInBuffer()
            COMPort.DiscardOutBuffer()

            Try
                COMPort.Close()

            Catch ex As Exception
                MsgBox(ex.Message)
                MsgBox(ex.StackTrace)
            End Try
        End If

    End Sub

By catching the time out exception I am able to close the port immediately but when I reopen the serialPort again I have to wait around 1-2 minutes before the port reopens itself. Because, I am new newbie I read the documents and find out that serialPort it an unmanaged resource therefore it is not directly under the control of garbage collector.

I found an API called ReleaseCOMObject(o as Object) as Integer. I tried to use this API like that,

    Dim WithEvents COMPort As New SerialPort

    Private Sub ReleaseCOMObject(obj As Object)
        Dim countDown As Int32
        countDown = 1

        While (countDown > 0)
            countDown = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        End While

    End Sub

  Private Sub ClosePort()
        If COMPort.IsOpen Then
            COMPort.DiscardInBuffer()
            COMPort.DiscardOutBuffer()

            Try
                ReleaseCOMObject(COMPort)
                COMPort.Close()

            Catch ex As Exception
                MsgBox(ex.Message)
                MsgBox(ex.StackTrace)
            End Try
        End If

    End Sub

The API accepts this COMPort as SerialPort object but when I published and run this code it is giving me an ArgumentException. This is the below exception,

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: The object's type must be __ComObject or derived from __ComObject.

I found out that there is another way is to implement an IDisposable interface but how can I implement this interface in my form class which is already implemented by visual studio in the Form1.Designer.vb file. The code give by visual studio is given below,

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

Any help in this regard would be truly appreciated.

解决方案

By catching the time out exception I am able to close the port immediately but when I reopen the serialPort again I have to wait around 1-2 minutes before the port reopens itself.

Closing and reopening the COM port has nothing to do with managing resources and garbage collection.   There should be no need to consider disposing of it unless the error that you are expecting might have somehow corrupted the instance of the component. In that case, use the Dispose method that is part of the class.

A Write error would not usually require that the port is closed and re-opened - discarding the buffers and retrying the Write ought to be sufficient.  Your error trapping should be specific about which of the possible errors you are handling - only the TimeoutEception would require special handling - the other possible errors should be trapped before the Write method is called.  

If you need to close the port and re-open it then the delay in reopening it is likely due to the device at the other end failing to correctly set the control signals (which would also likely be the cause of the original timeout). 


这篇关于串行端口在捕获到异常后关闭后需要花费大量时间才能重新打开自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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