简体   繁体   English

串口myPort.Datareceived C#

[英]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. 你好朋友在C#中有一个从连接的串行设备读取数据的表单,我的问题是我什至更改了myPort.DataReceived方法的表单仍在运行并接收数据。 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. 我已经尝试过在更改表单时将其压缩的命令,但是当您尝试运行myPort.Close时它会崩溃,我相信这就是为什么myPort.DataReceived仍在运行的原因,因此我删除了代码,并继续执行myPort.Close以另一种形式打开。 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: 我认为我的解决方案是停止myPort.DataReceived然后关闭连接,但是找不到解决方法。下面是我的代码摘录:

 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 . 您遇到的问题是,一旦触发事件,您的应用程序将进入函数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. 等待100毫秒会使情况变得更糟。 So my advice is to remove the wait and put a try catch statement around the code to make the thread terminate cleanly. 因此,我的建议是删除等待代码,并在代码周围放置try catch语句,以使线程干净地终止。

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. 此外,与使用成员myPort相比,使用sender读取传入的数据更好,因为sender是触发事件的sender 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. 还建议将DataReceived事件处理程序函数的主体保持在最低限度。 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);
      }

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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