我做错了什么? Wpf串口通信 [英] What do I do wrong? Wpf serialport communication

查看:92
本文介绍了我做错了什么? Wpf串口通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我收到数据时需要显示一些东西。你能告诉我如何修复我的代码吗?我认为调用新的EventHandler无法正常工作。

谢谢!



我尝试了什么: < br $>


Hi,
I need to display something when I receive a data. Could u tell me how to fix my code? I think invoking new EventHandler doesn't work correctly.
Thanks!

What I have tried:

private void button_Click(object sender, RoutedEventArgs e)
{
    try
    {
        if (!sPort.IsOpen)
        {
            sPort.PortName = comboBox.Text;
            sPort.BaudRate = 9600;
            sPort.Parity = Parity.None;
            sPort.DataBits = 8;
            sPort.StopBits = StopBits.One;

            sPort.Open();

            sPort.DataReceived += sPort_DataReceived;
        }
    }
    catch { }
}

private void sPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    Dispatcher.Invoke(new EventHandler(DisplayData));
}

private void DisplayData(object o, EventArgs e)
{
    Example_label.Content = "received";
}

推荐答案

引用:

我需要在收到数据时显示内容。你能告诉我如何修复我的代码吗?我认为调用新的EventHandler无法正常工作。

I need to display something when I receive a data. Could u tell me how to fix my code? I think invoking new EventHandler doesn't work correctly.



没有什么可以解决的,你需要重写你的代码。

但首先你需要了解串口是如何工作的。它是slooooow,非常sloooooow,不要指望你的数据在打开端口后立即在缓冲区中等待。

C#中的串口通信初学者 [ ^ ]


There is nothing to fix, you need to rewrite your code.
But first of all you need to understand how serial port is working. It is slooooow, very sloooooow, do not expect your data to wait in buffer instantly after opening the port.
Serial Comms in C# for Beginners[^]

sPort.DataReceived += sPort_DataReceived;



串口对象不是数据的存储空间,有缓冲区,但你不能用来存储一些数据。

带有C#示例的串行端口通信教程 - CodeSamplez.com [ ^ ]


使用工作示例作为开始尽可能地指出。这显示了如何从SerialPort读取:

SerialPort.DataReceived事件(System.IO.Ports) [ ^ ]

您设置和打开的顺序port是至关重要的(实例化,设置参数并附加datareceived处理程序)。您的示例有许多错误,可能需要一些背景阅读。此示例可让您走上正确的道路: Microsoft Visual C#Express的简单序列 [ ^ ]: c# - 如何从串口读写 - 堆栈溢出 [ ^ ]



你的应该设计应用程序来缓冲传入的数据(可能在任何时候发生)。然后,您的应用程序的其他部分可能会根据需要使用此数据。





请参阅:使用WPF,RS232和PIC通信的串行通信 [ ^ ]
Use working examples as a starting point wherever possible. This shows how to read from a SerialPort:
SerialPort.DataReceived Event (System.IO.Ports)[^]
The order in which you set up and open the port is crucial (instantiate, set parameters and attach datareceived handler). Your example has many errors and a little background reading may be necessary. This example should get you on the right track: Simple Serial for Microsoft Visual C# Express[^] also: c# - How to Read and Write from the Serial Port - Stack Overflow[^]

Your application should be designed to buffer the incoming data (which may occur at any time). This data may then be used as needed by other parts of your application.

[edit following reply]
See: Serial Communication using WPF, RS232 and PIC Communication[^]


您使用WPF标记了您的问题,但您的代码看起来更像Windows窗体。使用绑定到UI的属性 ViewModel PropertyChanged 事件将自动发送到UI线程,因此您可以避免所有交叉线程问题( DataReceived 始终发生在另一个线程中。

您还应该知道何时不再接收任何字节(查看设备的协议)以再次更改状态消息。
You tagged your question with "WPF", but your code looks more like Windows Forms. Use a ViewModel with properties bound to the UI. The PropertyChanged event will automagically be sent to the UI thread, so you avoid all cross-threading issues (DataReceived happens always in a different thread).
And you should also find out when you won't receive any further bytes (look at the protocol for your device) to change the status message again.


这篇关于我做错了什么? Wpf串口通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆