[英]Serial monitor, DataReceived handler misunderstanding C#, WPF
I'm developing a serial monitor for my application in WPF, programming in C#. 我正在为WPF中的应用程序开发串行监视器,并使用C#进行编程。 I have trouble managing the DataReceived event, because i want a real time monitor like HyperTerminal or TeraTerm for example (I'm not using them because I want my terminal to be a part of an ethernet communication tool, wich I already developed using winPcap).
我在管理DataReceived事件时遇到了麻烦,因为我想要一个例如HyperTerminal或TeraTerm的实时监视器(我不使用它们是因为我希望我的终端成为以太网通信工具的一部分,直到我已经使用winPcap开发过) 。
I have to read some data from my microcontroller, display it on the textBox (It just prints a menu and the list of commands available) and when it finishes the loading sequence I would like to interact with it, nothing special, just send a "flash-" command to program the fpga of the board. 我必须从微控制器读取一些数据,将其显示在textBox上(它只打印菜单和可用命令列表),完成加载顺序后,我想与之进行交互,没什么特别的,只需发送一个“ flash-“命令对板的fpga进行编程。
My application goes in exception when I try to update the textbox.text with the data received. 当我尝试使用接收到的数据更新textbox.text时,我的应用程序异常。 I tried to search everywhere but despite a lot of examples, I didn't catch something which is explaining the code properly.
我尝试在任何地方搜索,但是尽管有很多示例,但我没有发现可以正确解释代码的内容。
Here is the code, thanks in advance 这是代码,在此先感谢
namespace WpfApplication1 {
/// <summary>
/// Interaction logic for SerialMonitor.xaml
/// </summary>
public partial class SerialMonitor : Window {
//VARIABLES
public SerialPort comPort = new SerialPort();
public SerialMonitor() {
//initialization
InitializeComponent();
scanPorts();
}
private void scanPorts() {
textBoxIndata.Clear();
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports) {
comboBoxPorts.Items.Add(port);
}
}
private void openComBtn_Click(object sender , RoutedEventArgs e) {
comPort.Parity = Parity.None;
comPort.DataBits = 8;
comPort.ReadTimeout = 500;
comPort.StopBits = StopBits.One;
if (comboBoxPorts.SelectedItem != null && comboBoxPorts.SelectedItem != null) {
comPort.PortName = comboBoxPorts.SelectedItem.ToString();
comPort.BaudRate = Convert.ToInt32(comboBoxBaud.Text);
try {
//Open port and add the event handler on datareceived
comPort.Open();
comPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
if (comPort.IsOpen) {
label1.Content = "COM PORT OPEN";
}
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) {
}
//function to update the textBox, didn't manage to get it working
private void updateUI (string s) {
}
//CLOSE AND EXIT BUTTONS
private void closeComBtn_Click(object sender , RoutedEventArgs e) {
if (comPort.IsOpen) {
comPort.Close();
label1.Content = "COM PORT CLOSED";
}
}
private void exitBtn_Click(object sender , RoutedEventArgs e) {
if (comPort.IsOpen) {
comPort.Close();
}
this.Close();
}
}
} }
I got now the problem that when i send my command using SerialPort.Write(string cmd) , I can't read back the answer... 我现在遇到的问题是,当我使用SerialPort.Write(string cmd)发送命令时,我无法读回答案...
EDIT: Fixed everything, I will post the code if anyone is interested in programming a tool like this one :) 编辑:修复了所有问题,如果有人对这样的工具编程感兴趣,我将发布代码:)
DataReceived
event returns on another/secondary thread, which means you will have to marshal back to the UI thread to update your TextBox
DataReceived
事件在另一个/辅助线程上返回,这意味着您将必须重新编组回UI线程才能更新TextBox
SerialPort.DataReceived Event SerialPort.DataReceived事件
The DataReceived event is raised on a secondary thread when data is received from the SerialPort object.
从SerialPort对象接收数据时,在辅助线程上引发DataReceived事件。 Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception.
由于此事件是在辅助线程而非主线程上引发的,因此尝试修改主线程中的某些元素(例如UI元素)可能会引发线程异常。 If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread.
如果需要修改主窗体或控件中的元素,请使用Invoke将更改请求回发,这将在适当的线程上完成工作。
You can use Dispatcher.BeginInvoke
or Dispatcher.Invoke Method
to marshal back to the main thread 您可以使用
Dispatcher.BeginInvoke
或Dispatcher.Invoke Method
将其编组回主线程
Exmaple 〔实施例
Application.Current.Dispatcher.Invoke(new Action(() => { /* Your code here */ }));
or 要么
someControl.Dispatcher.Invoke(new Action(() => { /* Your code here */ }));
我现在遇到的问题是,当我使用SerialPort.Write(string cmd)发送命令时,我无法读回答案...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.