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

How to call a method using the serial port DataReceived event

我正在尝试使用 DataReceived 事件,但无法从方法 Main() 调用我的方法 OnDataReceived()。我已经通过添加行 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";
        }

        }
    }
}

你的问题之一是 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 消失,事件注册也消失了。因此你的事件永远不会被触发。

检查您的活动是否有效。在调试器中使用 breakpoint

编辑:

null值表示已经注销。由于 SerialPort 已被处置。

Basically I just want to run the method OnDataReceived upon receiving data through my serial port.

问题是您没有 运行 这种方法。它被设计为(与任何其他 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();

可能行不通。这就像关上你身后的门,然后试图拿起仍在你封闭的公寓里的 phone...