【发布时间】:2018-05-31 08:23:57
【问题描述】:
我正在为我的 WPF 应用程序开发一个串行监视器,用 C# 编程。 我无法管理 DataReceived 事件,因为我想要一个实时监视器,例如 HyperTerminal 或 TeraTerm(我没有使用它们,因为我希望我的终端成为以太网通信工具的一部分,我已经使用 winPcap 开发了) .
我必须从我的微控制器读取一些数据,将其显示在文本框上(它只是打印一个菜单和可用命令列表),当它完成加载序列时,我想与它进行交互,没什么特别的,只是发送“flash-”命令对板子的 fpga 进行编程。
当我尝试使用收到的数据更新 textbox.text 时,我的应用程序出现异常。我尝试到处搜索,但尽管有很多示例,但我没有找到正确解释代码的内容。
这是代码,在此先感谢
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();
}
}
}
我现在遇到的问题是,当我使用 SerialPort.Write(string cmd) 发送命令时,我无法回读答案...
编辑:修复了所有问题,如果有人对编写这样的工具感兴趣,我将发布代码:)
【问题讨论】:
-
你得到了什么异常?
-
这是 DataReceivedHandler private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { string inData = comPort.ReadExisting(); textBoxIndata.AppendText(inData); } 但它给了我一个线程冲突
-
"调用线程无法访问该对象,因为另一个线程拥有它。"
标签: c# wpf serial-port serial-communication