简体   繁体   中英

serial port myPort.Datareceived C#

Hello friends have a form in C # that reads data from a serial device connected, my problem is that I even changing form of the method myPort.DataReceived still running and receiving data. There's no way I close the connection with the serial port because the method does not stop excutar. I've tried a command to zip it when I change my form but it crashes when you try to run the myPort.Close, I believe that is why the myPort.DataReceived still running, so I removed the code and it continues myPort.Close open in another form. I think my solution would be to stop the myPort.DataReceived to then close connection, but can not find way to do this.Below is an excerpt from my code:

 namespace EntradaFinalCliente { public partial class ConsultaSerial : Form { string SerialString; private SerialPort myport; public ConsultaSerial() { InitializeComponent(); abrirSerial(); lerDados(); } public void abrirSerial() { myport = new SerialPort(); myport.BaudRate = 9600; myport.PortName = SerialPort1; myport.DataReceived += myport_DataReceived; } private void lerDados() { if (myport.IsOpen == false) { try { myport.Open(); } catch { return; } } } private void myport_DataReceived(object sender, SerialDataReceivedEventArgs e) { Thread.Sleep(100); SerialString = myport.ReadExisting(); this.Invoke(new EventHandler(Analisa)); } private void Analisa(object sender, EventArgs e) { checarSerial(); } 

And this is my closing the form button:

 private void button1_Click (object sender, EventArgs e) { myPort.Close (); this.Hide (); var form1 = new Form1 (); form1.Closed + = (s, args) => this.Close (); Form1.Show (); } 

The issue you have it that once the event has been triggered, your application would have entered the function myport_DataReceived . The function will continue to execute regardless of whether the port has been closed. If the port has been closed, the function would execute for the last time. Waiting for 100ms makes it worse. So my advice is to remove the wait and put a try catch statement around the code to make the thread terminate cleanly.

Furthermore, it is better if you use the sender to read the incoming data than using the member myPort because the sender is the one that fires the event. It also helps to remove confusion when you open two or more ports.

It is also advised that the body of DataReceived event handler function should be kept to minimum. Only do what you need to get the data out. You can then store the data in memory and do more complicated handling somewhere else using the stored data.

  private void myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
  {
      Thread.Sleep(100); // Why do you need to wait for 100 ms? If the event reaches here, it will have the data to read. Can remove?
      try
      {
          SerialPort sp = (SerialPort)sender;
          SerialString = sp.ReadExisting();
          this.Invoke(new EventHandler(Analisa));
      }
      catch (Exception ex)
      {
           // Do something else
           Console.WriteLine(ex.Message);
      }

  }

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