none
Serial Port IOException

    Question

  •  

    Hi,

     

    I am facing problem in serial port.DataReceive. Sometimes it will trigger the IO Exception.

     

    public SerialPortMgr()

    {

     spComm = new SerialPort();

     spComm.DataReceived += new SerialDataReceivedEventHandler(PortDataReceived);

    }

     

    Unable to read from port COM3. System.IO.IOException: The I/O operation has been aborted because of either a thread exit or an application request.

       at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
       at System.IO.Ports.SerialStream.EndRead(IAsyncResult asyncResult)
       at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
       at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count)
       at System.IO.Ports.SerialPort.InternalRead(Char[] buffer, Int32 offset, Int32 count, Int32 timeout, Boolean countMultiByteCharsAsOne)
       at System.IO.Ports.SerialPort.ReadTo(String value)

     at PortDataReceived(Object sender, SerialDataReceivedEventArgs e)

     

    For your information, i have created another thread in the main program to keep track the status of the serial port.

     

    Thread thread1 = new Thread(new ThreadStart(this.MonitorSerialPortThread));

    thread1.start();

     

    In the MonitorSerialPortThread, it will send the message to the serial port in a predefined time period. If it didn't get any reply from the serial port, it will close the port, reopen it again and send a message to the serialport.

     

    After encountered the exception mentioned above, the thread stopped running. May i know why the thread stop from running? is it because of the serial port crashed due to the exception above? Is there any solution to avoid this? Thank you.

     

    Tuesday, October 23, 2007 6:21 AM

Answers

  • "If it didn't get any reply from the serial port, it will close the port, reopen it again and send a message to the serialport."

     

    This is probably the problem. SerialPort has a background thread, which must be closed down before you are allowed to reopen the port. Add e.g. a Sleep(200) statement between Close() and Open() - perhaps with a Application.DoEvents() statement before the Sleep command, but don't tell ReneeC or Peter Ritchie . There has been a very, very long discussion about using Sleep() on the UI thread, but to my opinion it is the only resonable thing to do in this case.

     

    PS! I don't think it is necessary to close and reopen a port in case of a missing reply. It usually doesn't need any reset.

    Tuesday, October 23, 2007 7:01 AM

All replies

  • You must Open the port before use it.

    Tuesday, October 23, 2007 6:53 AM
  • "If it didn't get any reply from the serial port, it will close the port, reopen it again and send a message to the serialport."

     

    This is probably the problem. SerialPort has a background thread, which must be closed down before you are allowed to reopen the port. Add e.g. a Sleep(200) statement between Close() and Open() - perhaps with a Application.DoEvents() statement before the Sleep command, but don't tell ReneeC or Peter Ritchie . There has been a very, very long discussion about using Sleep() on the UI thread, but to my opinion it is the only resonable thing to do in this case.

     

    PS! I don't think it is necessary to close and reopen a port in case of a missing reply. It usually doesn't need any reset.

    Tuesday, October 23, 2007 7:01 AM