本文介绍了[UWP] [C#]以编程方式在串口上发送和获取数据,无需单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我希望你能帮助我。我正在开发一个UWP应用程序。我使用包含无限循环的以下函数在串行端口上成功发送和接收数据。

I hope you can help me. I am working on a UWP app. I successfully send and receive data on a serial port employing the following function that includes an infinite loop.

private async void Listen()
        {
            try
            {
                if (serialPort != null)
                {
                    dataReaderObject = new DataReader(serialPort.InputStream);

                    // keep reading the serial input
                    while (true)
                    {
                        await ReadAsync(ReadCancellationTokenSource.Token);
                    }
                }
            }
            ...
            finally
            {
                // Cleanup once complete
                if (dataReaderObject != null)
                {
                    dataReaderObject.DetachStream();
                    dataReaderObject = null;
                }
            }
        }

但这仅在我在文本框中写入数据并单击按钮发送时才有效。

But this only works when I write data in a textbox and click a button to send it.

我可以通过哪种方式在没有"Button_Click"的情况下在串口上发送和读取数据。功能?我会在调用Listen()函数之后实现一个在串口上发送和接收数据的函数。我的问题是这个函数是
从未调用,因为没有关联的点击事件,无限循环阻止调用它。

In which way can I send and read data on the serial port without a "Button_Click" function? I would implement a function to send and receive data on the serial port just after the Listen() function is called. My problem is that this function is never called because there is no click events associated and the infinite loop prevent to call it.

请清楚,如你所见,我我是新手。

Please, be clear, as you see, I am a newbie.

非常感谢您提前。

Gabriele

推荐答案

感谢您发布此处。

由于您的问题与UWP有关,我会将此案移至UWP论坛。

Since your issue is related to UWP, I will move the case to UWP forum.

最诚挚的问候,

Hart


这篇关于[UWP] [C#]以编程方式在串口上发送和获取数据,无需单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 11:57