none
I/O Exception Serialport.datareceived VB.NET 4 RRS feed

  • Question

  • I get an I/O exception when I run serialport.close() "The I/O operation has been aborted because of either a thread exit or an application request". The exception comes from the datareceived event. I don't want this exception, can anyone tell me how to get rid of it. I have tried Removehandler and sleep before calling close(), but nothing seems to help. In my datareceived procedure I have;

    if SerialPort1.IsOpen then

    Try

             readBuffer = SerialPort1.ReadLine()

             Me.BeginInvoke(New EventHandler(Addressof DoUpDate))

    Catch Ex as Exception

              MsgBox(ex.message)

    End Try

    End If

    I am sure that the exception comes from the ReadLine() statement when I try to close the serialport with serialport1.close. Thank you in advance for your help.

    Monday, June 3, 2013 8:02 AM

Answers

  • Monday, June 3, 2013 1:54 PM
  • What is the purpose of "if SerialPort1.IsOpen then  ..." if your posted code is in the DataReceived event?  Is it in a loop?  Post all of the code in your DataReceived event and all of the code in the method that attempts to close the port.
    Monday, June 3, 2013 2:24 PM
  • The open method sets up the baud rate (and other properties) and gives the appliaction access rights to the hardware.  The DataReceive is just an event handler that isn't really need for your case.  The important part of the code is the Port.BytesToRead().  You only want to read when bytes are availables so you don't get blocked in the Read code waiting for data and the EndOfLine character.  You want to make sure you don't close while you are in any read method.

    A button click event is running independantly from the Read method.  So when you read you want to set a semiphore and then in the Close button wait for the read to complete before you close the port.


    jdweng

    Monday, June 3, 2013 2:48 PM

All replies

  • Readline is a blocking function so you  code is probably spending most of the time inside this function.  You can't close the Port if you are waiting for data inside the ReadLine function.  You either have to close the Port while you are not waiting for data in the ReadLine function, or use the AsynRead function so you can close the the Port when you are not waiting being blocked inside the ReadLine() method..

    jdweng

    Monday, June 3, 2013 9:32 AM
  • Hie,

    Thank you for your response. What does the syntax for AsynRead() look like ? I have searched all over without finding anything.

    Regards

    Mukomana

    Monday, June 3, 2013 1:21 PM
  • Monday, June 3, 2013 1:54 PM
  • What is the purpose of "if SerialPort1.IsOpen then  ..." if your posted code is in the DataReceived event?  Is it in a loop?  Post all of the code in your DataReceived event and all of the code in the method that attempts to close the port.
    Monday, June 3, 2013 2:24 PM
  • The open method sets up the baud rate (and other properties) and gives the appliaction access rights to the hardware.  The DataReceive is just an event handler that isn't really need for your case.  The important part of the code is the Port.BytesToRead().  You only want to read when bytes are availables so you don't get blocked in the Read code waiting for data and the EndOfLine character.  You want to make sure you don't close while you are in any read method.

    A button click event is running independantly from the Read method.  So when you read you want to set a semiphore and then in the Close button wait for the read to complete before you close the port.


    jdweng

    Monday, June 3, 2013 2:48 PM