Error closing serial port in c#

Asked

Viewed 727 times

4

I am new in C#. I made a program, which keeps continuously reading the data from the serial port and showing in a textbox.

Until then, everything works well. The problem occurs when I try to close the door, it gives the following error

The I/O operation was cancelled due to an outflow of thread or an application request;

I’ve read several threads on the same subject, but I still can’t solve.

Below is part of the code.

public FormRelatorio()
{
    InitializeComponent();
    FormPrincipal.SerialPortCommunicator.SerialPort.DataReceived += 
          new SerialDataReceivedEventHandler(SerialPort_DataReceived_RL);
}
private void SerialPort_DataReceived_RL(object sender, SerialDataReceivedEventArgs e)
{
    rxSerialRL = FormPrincipal.SerialPortCommunicator.SerialPort.ReadLine();
    BeginInvoke(new Fdelegate(recebeSerial), new object[] { rxSerialRL });    
}
public void recebeSerial(string a)
{
    textBoxTesteMSG.Text = a; 
}
  • if you just change BeginInvoke(new Fdelegate(recebeSerial), new object[] { rxSerialRL }); for recebeSerial(rxSerialRL); ?

1 answer

1

Try that and instead of CommPort enter the name of your class that addresses the Serial Port.

void CloseComPort()
{

CommPort com=CommPort.Instance;
com.Close();
}

I hope it is the beginning of the solution of your need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.