我有一个 WPF 测试应用程序,用于评估基于事件的串行端口通信(与轮询串行端口相比).问题是 DataReceived 事件似乎根本没有触发.
I have a WPF test app for evaluating event-based serial port communication (vs. polling the serial port). The problem is that the DataReceived event doesn't seem to be firing at all.
我有一个非常基本的 WPF 表单,其中包含一个用于用户输入的 TextBox、一个用于输出的 TextBlock,以及一个用于将输入写入串行端口的按钮.
I have a very basic WPF form with a TextBox for user input, a TextBlock for output, and a button to write the input to the serial port.
代码如下:
public partial class Window1 : Window
{
SerialPort port;
public Window1()
{
InitializeComponent();
port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
port.DataReceived +=
new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
}
void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Debug.Print("receiving!");
string data = port.ReadExisting();
Debug.Print(data);
outputText.Text = data;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Debug.Print("sending: " + inputText.Text);
port.WriteLine(inputText.Text);
}
}
现在,这里是复杂的因素:
Now, here are the complicating factors:
我正在使用的笔记本电脑没有串口,所以我使用了一款名为 Virtual Serial Port Emulator 的软件来设置 COM2.VSPE 过去的表现令人钦佩,目前尚不清楚为什么它只会与 .NET 的 SerialPort 类一起出现故障,但我提一下以防万一.
The laptop I'm working on has no serial ports, so I'm using a piece of software called Virtual Serial Port Emulator to setup a COM2. VSPE has worked admirably in the past, and it's not clear why it would only malfunction with .NET's SerialPort class, but I mention it just in case.
当我点击表单上的按钮发送数据时,我的超级终端窗口(连接到 COM2)显示数据正在通过.是的,当我想测试我的表单读取端口的能力时,我断开了超级终端.
When I hit the button on my form to send the data, my Hyperterminal window (connected on COM2) shows that the data is getting through. Yes, I disconnect Hyperterminal when I want to test my form's ability to read the port.
我尝试在连接事件之前打开端口.没有变化.
I've tried opening the port before wiring up the event. No change.
我在这里阅读了另一篇帖子,其中其他人遇到了类似的问题.在这种情况下,这些信息都没有帮助我.
I've read through another post here where someone else is having a similar problem. None of that info has helped me in this case.
这里是控制台版本(修改自 http://mark.michaelis.net/Blog/TheBasicsOfSystemIOPortsSerialPort.aspx):
Here's the console version (modified from http://mark.michaelis.net/Blog/TheBasicsOfSystemIOPortsSerialPort.aspx):
class Program
{
static SerialPort port;
static void Main(string[] args)
{
port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
port.DataReceived +=
new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
string text;
do
{
text = Console.ReadLine();
port.Write(text + "
");
}
while (text.ToLower() != "q");
}
public static void port_DataReceived(object sender,
SerialDataReceivedEventArgs args)
{
string text = port.ReadExisting();
Console.WriteLine("received: " + text);
}
}
这应该消除任何关于这是线程问题的担忧(我认为).这也不起作用.再次,超级终端报告通过端口发送的数据,但控制台应用程序似乎没有触发 DataReceived 事件.
This should eliminate any concern that it's a Threading issue (I think). This doesn't work either. Again, Hyperterminal reports the data sent through the port, but the console app doesn't seem to fire the DataReceived event.
编辑 #2:
我意识到我有两个独立的应用程序应该从串口发送和接收,所以我决定尝试同时运行它们...
I realized that I had two separate apps that should both send and receive from the serial port, so I decided to try running them simultaneously...
如果我在控制台应用程序中键入内容,WPF 应用程序 DataReceived 事件将触发,并出现预期的线程错误(我知道如何处理).
If I type into the console app, the WPF app DataReceived event fires, with the expected threading error (which I know how to deal with).
如果我在 WPF 应用程序中键入内容,控制台应用程序 DataReceived 事件将触发,并回显数据.
If I type into the WPF app, the console app DataReceived event fires, and it echoes the data.
我猜测问题出在我使用 VSPE 软件的某个地方,该软件设置为将一个串行端口同时视为输入和输出.通过 SerialPort 类的一些奇怪之处,一个串行端口的实例不能同时是发送者和接收者.反正我觉得解决了.
I'm guessing the issue is somewhere in my use of the VSPE software, which is set up to treat one serial port as both input and output. And through some weirdness of the SerialPort class, one instance of a serial port can't be both the sender and receiver. Anyway, I think it's solved.
我只能推测问题确实出在 Virtual Serial Port Emulator 程序上.这并不是说该软件有问题:到目前为止,VSPE 对我来说效果很好.但是我的代码和我设置 VSPE 连接器的方式之间存在一些冲突.
I can only surmise that the problem was indeed with the Virtual Serial Port Emulator program. this is NOT to say there is a problem with that software: VSPE has worked very well for me so far. But there was some conflict between my code and how I had set up the VSPE connector.
这篇关于.NET SerialPort DataReceived 事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!