如何使用串口DataReceived事件调用方法

本文关键字:调用 方法 事件 DataReceived 何使用 串口 | 更新日期: 2023-09-27 18:03:29

我正在尝试使用DataReceived事件,但我的方法OnDataReceived()无法从方法Main()调用。我已经通过添加System.Windows.Forms.Application.Exit();行来证明这一点,这将有效地关闭windows窗体应用程序;但它没有。

基本上,我只是想在通过串口接收数据时运行OnDataReceived方法。我希望用arduino.DataReceived += OnDataReceived;做到这一点,但事实证明是不成功的。请随意查看我的评论以获得指导。此外,我还介绍了字符串received和串行端口arduino之外的任何方法,这会影响功能吗?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
    string received;
    private SerialPort arduino;
    private void button2_Click(object sender, EventArgs e)
    {
        comboBox1.Items.Clear();
        string[] ports = SerialPort.GetPortNames();
        foreach (string port in ports)
        {
            comboBox1.Items.Add(port);
        }
        comboBox1.SelectedIndex = 0;
    }
    private void Main(string port)
    {
        using (arduino = new SerialPort(port))
        {
            arduino.Open();
            arduino.DtrEnable = true;
            arduino.RtsEnable = true;
            arduino.BaudRate = 9600;
            arduino.DataReceived += OnDataReceived; //when data is received, call method below.
            //System.Windows.Forms.Application.Exit(); //this works, which means the above line has been run too.
        }
    }
    private void OnDataReceived(object sender, SerialDataReceivedEventArgs e) //this method does not get called from the above method.
    {
        System.Windows.Forms.Application.Exit();
        received = arduino.ReadLine();
    }
    public void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        Main(comboBox1.Text);
        if (checkBox1.Checked)
        {
            checkBox1.Text = "Listening...";
                if (received == "S'r")
                {
                    arduino.Close();
                    //System.Diagnostics.Process.Start("shutdown", "/f /r /t 0");
                    //System.Windows.Forms.Application.Exit();
                }
        }
        else
        {
            checkBox1.Text = "Start";
        }
        }
    }
}

如何使用串口DataReceived事件调用方法

您的问题之一是using块:

using (arduino = new SerialPort(port))
{
    arduino.Open();
    arduino.DtrEnable = true;
    arduino.RtsEnable = true;
    arduino.BaudRate = 9600;
    arduino.DataReceived += OnDataReceived; //when data is received, call method below.
    //System.Windows.Forms.Application.Exit(); //this works, which means the above line has been run too.
}

当代码被执行时,arduino.Dispose被调用,你的SerialPort消失到遗忘中,事件注册也是如此。因此,您的事件永远不会被触发。

To检查事件是否有效。在调试器中使用断点。

编辑:

"null"表示未注册。因为SerialPort已经被处理了

基本上我只是想在通过串口接收数据时运行OnDataReceived方法。

问题是你不运行这个方法。它被设计(像任何其他event)在需要时发射。因此,它将帮助您保持被动,并且您的GUI可以保持响应(因为您不必主动运行它)。

SerialPort打开时,可以对其进行读写操作。所以如果你想监控它,只要打开它,注册到事件,然后等着看会发生什么。

public void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox1.Checked)
    {
        arduino = new SerialPort(port);
        // you should specify these before opening the port
        arduino.DtrEnable = true;
        arduino.RtsEnable = true;
        arduino.BaudRate = 9600;
        // register to the event
        arduino.DataReceived += OnDataReceived;
        //open the port
        arduino.Open();
        // tell the user
        checkBox1.Text = "Listening...";
    }                
}

在事件中,你应该指定当数据到达时你想做什么:

private void OnDataReceived(object sender, SerialDataReceivedEventArgs e) //this method does not get called from the above method.
{
    // first of all read out what you have received!
    received = arduino.ReadLine();
    // do something with the information
    // if you have a condition and really want to close the application or shut down the computer
   // first close the port!!!
   arduino.Close();
}

旧版本:

    System.Windows.Forms.Application.Exit();
    received = arduino.ReadLine();

可能不起作用。这就像你关上了身后的门,然后试图拿起仍然在你关闭的公寓里的电话……