我正在尝试使用DataReceived
事件,但无法从方法OnDataReceived()
调用我的方法Main()
。我通过添加行System.Windows.Forms.Application.Exit();
来证明这一点,这将有效地关闭窗体应用程序;但事实并非如此。
基本上我只想在通过串口接收数据时运行方法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";
}
}
}
}
答案 0 :(得分:1)
您的一个问题是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
已被处置。
基本上我只想在通过串口接收数据时运行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();
可能无效。这就像关闭你身后的门,然后试着拿起仍在你的封闭式公寓里的手机......