Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Serial Port Strange Error!

Serial Port Strange Error!

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
7 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zafersavas
    wrote on last edited by
    #1

    Hello everyone, I am trying to send an array of bytes using the serial port and I get a strange I/O exception. In my opinion there is no reason for the error. Here is the code;

    SerialPort comport = new SerialPort("COM1", 57600, Parity.None, 8, StopBits.One);
    comport.Open();

    byte[] data = new byte[16];
    comport.Write(data, 0, 16); // This func gives the Exception

    The details for the exception is "The I/O operation has been aborted because of either a thread exit or an application request". How and why a write function can give an exception? Any ideas will be appreciated :) zafer

    L L 2 Replies Last reply
    0
    • Z zafersavas

      Hello everyone, I am trying to send an array of bytes using the serial port and I get a strange I/O exception. In my opinion there is no reason for the error. Here is the code;

      SerialPort comport = new SerialPort("COM1", 57600, Parity.None, 8, StopBits.One);
      comport.Open();

      byte[] data = new byte[16];
      comport.Write(data, 0, 16); // This func gives the Exception

      The details for the exception is "The I/O operation has been aborted because of either a thread exit or an application request". How and why a write function can give an exception? Any ideas will be appreciated :) zafer

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Can you post the Exception details. And Is the COM1 port , begin opened by another Application ? Or did it Exist ?

      I know nothing , I know nothing ...

      1 Reply Last reply
      0
      • Z zafersavas

        Hello everyone, I am trying to send an array of bytes using the serial port and I get a strange I/O exception. In my opinion there is no reason for the error. Here is the code;

        SerialPort comport = new SerialPort("COM1", 57600, Parity.None, 8, StopBits.One);
        comport.Open();

        byte[] data = new byte[16];
        comport.Write(data, 0, 16); // This func gives the Exception

        The details for the exception is "The I/O operation has been aborted because of either a thread exit or an application request". How and why a write function can give an exception? Any ideas will be appreciated :) zafer

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, is the code shown all part of a single method? are multiple threads involved? (including ThreadPool, BackgroundWorker, ...) did you use CheckForIllegalCrossThreadCalls anywhere? are you using one of the asynchronous events, such as DataReceived? where in the code is comport.Close()? you probably simplified your code before showing to such an extent that we cannot help you, unless you tell more and show more code. :)

        Luc Pattyn


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        Local announcement (Antwerp region): Lange Wapper? Neen!


        C 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, is the code shown all part of a single method? are multiple threads involved? (including ThreadPool, BackgroundWorker, ...) did you use CheckForIllegalCrossThreadCalls anywhere? are you using one of the asynchronous events, such as DataReceived? where in the code is comport.Close()? you probably simplified your code before showing to such an extent that we cannot help you, unless you tell more and show more code. :)

          Luc Pattyn


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          Local announcement (Antwerp region): Lange Wapper? Neen!


          C Offline
          C Offline
          charlie3636
          wrote on last edited by
          #4

          Hi, I am getting a similar error. I am just trying to read from a serial COM port. It works fine. Then if I open and close the COM port a few times I get the following error when trying to open again:

          The I/O operation has been aborted because of either a thread exit or an application request.

          Starnagely enough the error is still tehre if I stop debugging (VisualStuio 2008, vb.net) and start again. The only way I can get it to work again is by opening and closing the port with hyperterminal! Here is my wrapper class:

          Public Class clComPort
          Private WithEvents comPort As System.IO.Ports.SerialPort
          Private write As Boolean = True
          Private bAllowSndRcv As Boolean = False
          Private thRec As System.Threading.Thread

          Public Event WroteData(ByVal msg As String)
          Public Event DataRecieved(ByVal msg As String)
          Public Event COMError(ByVal msg As String)

          Public Function OpenPort(ByVal ComNum As Integer, ByVal baudRate As Integer, ByVal dataBits As Integer, _
          ByVal parity As IO.Ports.Parity, ByVal stpBits As IO.Ports.StopBits) As Boolean
          bAllowSndRcv = False
          Try
          'first check if the port is already open
          'if its open then close it
          If Not IsNothing(comPort) Then
          Me.ClosePort()
          End If
          comPort = New System.IO.Ports.SerialPort()
          'set the properties of our SerialPort Object
          comPort.BaudRate = Integer.Parse(baudRate)
          comPort.DataBits = Integer.Parse(dataBits)
          comPort.StopBits = stpBits
          comPort.Parity = parity
          comPort.PortName = "COM" & ComNum
          comPort.ReadTimeout = 500
          comPort.WriteTimeout = 500

            'now open the port
            Dim b As Boolean = comPort.IsOpen
            comPort.Open()
            bAllowSndRcv = True
            'return true
            Return True
          Catch ex As Exception
            bAllowSndRcv = False
            RaiseEvent COMError(ex.Message)
            Return False
          End Try
          

          End Function
          Public Sub ClosePort()
          bAllowSndRcv = False
          If IsNothing(comPort) Then Exit Sub
          If comPort.IsOpen Then
          RemoveHandler comPort.DataReceived, AddressOf comPort_DataReceived
          comPort.Close()
          End If
          comPort.Dispose()
          End Sub
          Private Sub comPort_DataReceived(ByVal sender As Object, _
          ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles comPort.DataReceived
          'This event will Receive the data from the selected COM port..
          If Not bAllowSndRcv Then Exit Sub
          If e.Eve

          L 1 Reply Last reply
          0
          • C charlie3636

            Hi, I am getting a similar error. I am just trying to read from a serial COM port. It works fine. Then if I open and close the COM port a few times I get the following error when trying to open again:

            The I/O operation has been aborted because of either a thread exit or an application request.

            Starnagely enough the error is still tehre if I stop debugging (VisualStuio 2008, vb.net) and start again. The only way I can get it to work again is by opening and closing the port with hyperterminal! Here is my wrapper class:

            Public Class clComPort
            Private WithEvents comPort As System.IO.Ports.SerialPort
            Private write As Boolean = True
            Private bAllowSndRcv As Boolean = False
            Private thRec As System.Threading.Thread

            Public Event WroteData(ByVal msg As String)
            Public Event DataRecieved(ByVal msg As String)
            Public Event COMError(ByVal msg As String)

            Public Function OpenPort(ByVal ComNum As Integer, ByVal baudRate As Integer, ByVal dataBits As Integer, _
            ByVal parity As IO.Ports.Parity, ByVal stpBits As IO.Ports.StopBits) As Boolean
            bAllowSndRcv = False
            Try
            'first check if the port is already open
            'if its open then close it
            If Not IsNothing(comPort) Then
            Me.ClosePort()
            End If
            comPort = New System.IO.Ports.SerialPort()
            'set the properties of our SerialPort Object
            comPort.BaudRate = Integer.Parse(baudRate)
            comPort.DataBits = Integer.Parse(dataBits)
            comPort.StopBits = stpBits
            comPort.Parity = parity
            comPort.PortName = "COM" & ComNum
            comPort.ReadTimeout = 500
            comPort.WriteTimeout = 500

              'now open the port
              Dim b As Boolean = comPort.IsOpen
              comPort.Open()
              bAllowSndRcv = True
              'return true
              Return True
            Catch ex As Exception
              bAllowSndRcv = False
              RaiseEvent COMError(ex.Message)
              Return False
            End Try
            

            End Function
            Public Sub ClosePort()
            bAllowSndRcv = False
            If IsNothing(comPort) Then Exit Sub
            If comPort.IsOpen Then
            RemoveHandler comPort.DataReceived, AddressOf comPort_DataReceived
            comPort.Close()
            End If
            comPort.Dispose()
            End Sub
            Private Sub comPort_DataReceived(ByVal sender As Object, _
            ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles comPort.DataReceived
            'This event will Receive the data from the selected COM port..
            If Not bAllowSndRcv Then Exit Sub
            If e.Eve

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, this[^] says you shouldn't close and immediately reopen a serial port. suggestion: check the port settings; if no change needed simply clear in- and out-buffer and go on; otherwise include a sleep. BTW: why are you parsing integer parameters such as baudrate? :)

            Luc Pattyn


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


            C 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, this[^] says you shouldn't close and immediately reopen a serial port. suggestion: check the port settings; if no change needed simply clear in- and out-buffer and go on; otherwise include a sleep. BTW: why are you parsing integer parameters such as baudrate? :)

              Luc Pattyn


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


              C Offline
              C Offline
              charlie3636
              wrote on last edited by
              #6

              Hi, Thanks for your answer. Ok, I will try the sleep. Interesting link you sent. What I am worried about is that once it's locked into the:

              ERROR The I/O operation has been aborted because of either a thread exit or an application request.
              Port Open Failed.

              it seems to stay there even if I start and stop my application again (both in dev environment in debug config, and using a released exe). The only way to unlock it is by opening and closing the port with hyperterminal. I wonder how I might be able to do this programatically? Is there any ways to run a batch file to open and close a hyperterminal? It's not the best solution but will have to do for a quick-fix... Thanks, Charlie

              C 1 Reply Last reply
              0
              • C charlie3636

                Hi, Thanks for your answer. Ok, I will try the sleep. Interesting link you sent. What I am worried about is that once it's locked into the:

                ERROR The I/O operation has been aborted because of either a thread exit or an application request.
                Port Open Failed.

                it seems to stay there even if I start and stop my application again (both in dev environment in debug config, and using a released exe). The only way to unlock it is by opening and closing the port with hyperterminal. I wonder how I might be able to do this programatically? Is there any ways to run a batch file to open and close a hyperterminal? It's not the best solution but will have to do for a quick-fix... Thanks, Charlie

                C Offline
                C Offline
                charlie3636
                wrote on last edited by
                #7

                I seem to have solved the issue by modifying my ClosePort sub to:

                Public Sub ClosePort()
                bAllowSndRcv = False
                If IsNothing(comPort) Then Exit Sub
                If comPort.IsOpen Then
                comPort.DiscardOutBuffer()
                RemoveHandler comPort.DataReceived, AddressOf comPort_DataReceived
                Threading.Thread.Sleep(200)
                comPort.Close()
                End If
                End Sub

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups