Serialdevice winui c# fire event when data received

Paul Harvey Chiverton Messina 5 Reputation points
2024-03-19T10:44:13.14+00:00

Dear everybody.

Using Microsoft Visual Studio Community 2022 (64 bits) - Current Versión 17.9.3 on Windows 10 most recent update, Packaged WinUI with C#

I am programming from scratch a new program (WinUI with C#) to manage my motorized telescope and I am trying to communicate with my 32 bit microcontrollers. For now I can list the serial ports and access them. I have been investigating but I need to keep the port open and attend any data that comes in (whenever it comes in) and also the main program can send data through the same serial port opened. I don't know when data will come in, it depends on the microcontroller. On my previous program WinForms I used sytem.IO and i had a delegate thread for SerialDataReceivedEventHandler which got called when any serial data came in and attended it using the DataReceived handler overrride. I cannot find anything similar that I can do with serialdevice in winui to have some kind of async (parallel method / thread) to get fired when data comes in through the serial port and handle it and act in consecuence from the method and pass the data to the mainprogram or tell the main program that serial data is available for processing if necesarry from the main program that is running. This function / method is continuos and should get fired when any data is received.

Any help appreciated.

Best Regards

Paul Harvey Chiverton

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
787 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,946 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jeanine Zhang-MSFT 9,676 Reputation points Microsoft Vendor
    2024-03-20T01:49:37.2466667+00:00

    Hello,

    Welcome to Microsoft Q&A!

    As far as I'm concerned, there is no similar method to ·SerialDataReceivedEventHandler Delegate· in winui3

    You need to use dataReader stay in the while clause. I suggest you could refer to the Doc: DataReader Class

    And you could also refer to the thread: https://stackoverflow.com/questions/58343457/how-to-create-a-datareceived-event-for-serialdevice

    Thank you.

    Jeanine


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.