Home All Groups Group Topic Archive Search About

Serial port Error: The I/O operation has been aborted because of either a thread exit or an applicat

Author
31 Aug 2006 3:38 AM
Kite
Hello, everyone!
I write a program with serial port, error happend--The I/O operation has been aborted because of either a thread exit or an application request. I have no idea with it.

This is my source code.
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.IO
Imports System.IO.Ports

Public Class Form1

    Dim WithEvents port As New IO.Ports.SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            port.ReceivedBytesThreshold = 1
            If port.IsOpen = True Then
                port.Close()
            End If
            port.Open()
            lblError.Text = "COM1 port connected!"
            lblError.ForeColor = Color.Green
            TextBox1.Text = "0.000kg"
        Catch ex As Exception
            lblError.Text = ex.Message
        End Try
    End Sub

    Private Sub port_DataReceived1(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
        Try
            TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
        Catch ex As System.IO.IOException
            MsgBox(ex.Message)
        End Try

    End Sub

    '------------------------------------------------------
    ' Delegate and subroutine to update the Textbox control
    '------------------------------------------------------
    Public Delegate Sub myDelegate()
    Public Sub updateTextBox()
        Dim tmpStr As String
        Dim pos As Integer
        tmpStr = Trim(port.ReadLine)
        pos = InStr(tmpStr, "GS")
        If pos > 0 Then
            tmpStr = Trim(Mid(tmpStr, pos + 2))
            If tmpStr <> Trim(TextBox1.Text) Then
                With TextBox1
                    .Text = tmpStr
                    '.ScrollToCaret()
                End With
            End If

        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        port.Close()
        Me.Close()
    End Sub
End Class

Author
31 Aug 2006 4:40 PM
Dick Grier
Hi,

What this means, almost certainly, is that the SerialPort object attempted
to complete the call to ReadLine after the port has been closed.  This can
happen because of the lack of synchronization between UI events which may
cause the port to close, and the background thread in the SerialPort object
that is performing the actual ReadFile operation (this executes as a result
of ReadLine in your delegate).

The problem with ReadLine, and the reason that I DO NOT use it, is that it
blocks until the the line terminating condition occurs -- this may be well
AFTER you have closed the port.  Thus the exception.

I prefer simply buffering my own data in a Static or class-level variable
(all ReadExisting and append new data to the buffer) , and testing that
buffer for the vbCrLf terminating characters.  If the vbCrLf is found (InStr
or Substring, your choice), then call a delegate to process and display the
data in the buffer.  Remember to clear this buffer AFTER you have processed
and displayed its content.  If you do this, the exception should be
resolved.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.

AddThis Social Bookmark Button