简体   繁体   中英

I/O operation has been aborted because of either a thread exit or an application request in serial port

I am working a window application to display data continuously from serial port on a textbox. the following is my code. I get this error when i run the program:

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

How can I solve this issue?

    private void mainform_Load(object sender, EventArgs e)
    {
        this.WindowState = FormWindowState.Maximized;   
        getWGT();           
    }
    SerialPort port = new SerialPort();
    public void getWGT()
    {            
        try
        {

            port.PortName = "COM1";
            port.BaudRate = 9600;
            port.StopBits = System.IO.Ports.StopBits.One;
            port.DataBits = 8;
            port.Parity = System.IO.Ports.Parity.None;

            port.Open();

            port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }
   public delegate void myDelegate();
    private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {          
        tWGT.BeginInvoke(new myDelegate(updateTextBox));
    }

    public void updateTextBox()
    {
        try
        {
            tWGT.AppendText(port.ReadExisting());
            tWGT.ScrollToCaret();
        }
        catch {
            port.Close();
        }
    }

The SerialPort is using it's own Eventloop on a thread. As you are subscribing to the DataReceived event you better not do the reading of the data of the port on the UI thread. You better keep that on the background thread and switch context once you have all data.

One other addition I would do is calling port.Close when the form gets closed. After the SerialPort imstance can be disposed.

The following methods have been changed to include the changes I described above:

// the delegate now takes a string argument, holding the received data
public delegate void UpdateText(string text);

// checks for the correct EventType and calls updateText 
// after all available data is read
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    try
    {
        if (e.EventType == SerialData.Chars)
        {
            // read all data and then handle
            updateTextBox(port.ReadExisting());
        }
        else
        {
            port.Close();
        }
    }
    catch
    {
        port.Close();
    }     
}

// called twice, once from the background thread
// and by BeginInvoke
public void updateTextBox(string text)
{
    // do we need to be switch to the UI thread?
    if (tWGT.InvokeRequired)
    {
        // yes, so setup the delegate and pass on the parameter
        // notice how BeginInvoke will call this same method again
        tWGT.BeginInvoke(new UpdateText(updateTextBox), text);
    }
    else
    {
        // show the received data
        tWGT.AppendText(text);
        tWGT.ScrollToCaret();
    } 
}

// nicely close
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    // if omitted you'll run into problems if this forms gets reopened
    if (port.IsOpen) port.Close();
    port.Dispose();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM