none
IO operation aborted error thrown while reading serial port using System.IO.Ports.SerialPort class RRS feed

  • Question

  • I am trying to read data written by an external device (weighing scale in this case) connected to serial port using .Net 4.0 - SerialPort class.

    First we initialize the serial port as below:

    InitializeSerialPort()
    {
       if ((serialPort != null) && (serialPort.IsOpen))
            {
                serialPort.Close();
                serialPort.Dispose();
                serialPort = null;
            }
    
            serialPort = new SerialPort("COM2", 9600, Parity.None, 8,
                             StopBits.One) { Handshake = Handshake.None };
            serialPort.DataReceived += serialPort_DataReceived;
            serialPort.NewLine = "\r";
    }
    

    We are using background worker thread to poll the device on continuous interval by sending a command (understood by the weighing scale) on the serial port. As soon as we send the command the device connected to serial port reacts with a response output. We call ReadLine API of SerialPort class to get the data present on the serial port written by the device in the DataReceived event as shown in the code snippet below :

    private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { data = serialPort.ReadLine(); // throws IO error } catch(System.IO.IOException ex) { //since serial port reading threw an error so

    //there is no value to be parsed hence exit the function. return; } //if no error then parse the data received }

    I'm using System.IO.Ports.SerialPort class of .Net framework 4.0. I can see a number of people posting this issue on other forums but with no specific resolution. Some of them terming .Net Serial port class as buggy which has not been fixed by Microsoft till date.

    I also tried the solution posted on msdn forums but no success. I need some input if any one else has come across this issue or its resolution.

    Thanks in advance.


    • Edited by DeepakGarg83 Thursday, January 23, 2014 10:38 AM
    Thursday, January 23, 2014 10:37 AM

Answers

  • You're calling ReadLine which is a blocking call until a newline is found.  But DataReceived will be raised as data is received from the port.  There is no guarantee that newline has been sent when you receive the event so you are likely running into an issue there.  ReadLine might work with DataReceived but every example you'll see will tell you to use ReadExisting which will read all the data available to this point.  You should be building up a buffer of received data until you get the delimiter you're looking for (I assume newline), then process the data and flush the buffer.

    Alternatively don't bother with DataReceived and just call ReadLine at the point in your code where you want to wait.  You mentioned you were using BWC so if you are doing this in the DoWork method then the event doesn't gain you much.  Instead send your data to the device and call ReadLine to block until a response is received.  Then continue with your processing.  Since the DoWork handler is on a background thread you won't freeze the UI. 

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, January 23, 2014 4:57 PM
    Moderator