Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kite - 31 Aug 2006 04:38 GMT
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
Dick Grier - 31 Aug 2006 17:40 GMT
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

Signature

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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.