Listing serial ports winui c# visual studio 2022

Paul Harvey Chiverton Messina 5 Reputation points
2024-03-13T11:06:18.64+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 but microcontrollers. For now I haven't opened any serial port yet. I have a problem listing the serial ports available. Below is the async function. On start this function is invoked and fills in and Observable Collection with the port names that are available and then populates a ComboBox. On start up all works ok and the combobox is filled in as bound to the Observable Collection. All ok. I have a button to invoke this function in order to update at wish the available serial ports. This is where the problem arises. It only works one out of 100 times. As indicated in the function the observable collection gets cleared and then refilled but it doesn't refill with any com ports. That only happens once at start up. Why? I have checked inside this function to see its flow and I have found that nearly everytime (except at startup) this test result if (serialDevice != null) is always null inside the foreach (DeviceInformation serialDeviceInfo in serialDeviceInfos) that always has a 1 count (only one com port available). Why is if (serialDevice != null) always null except at start up? On the other hand, once this function is invoked and is exited it shouldn't get called unless I click the button. The strange behaviour I am encountering also is if I switch to another application (for example Microsoft Outlook) on the windows task bar, just when I click on the task bar to change to another open application this function gets called and as I have explained doesn't get any port names and the ComboBox that had the list of ports appears empty till I shut down the app and rerun it again. At startup all ok, but why doesn't my function update the com ports and why does it get fired when changing to another application on my windows task bar????

Any help well appreciated.

Thank you

In public sealed partial class MainProgram : Window

ObservableCollection<string> PuertosSerieDisponibles = new ObservableCollection<string>();

private async void ActualizarPuertosSerie()

{

DeviceInformationCollection serialDeviceInfos = await DeviceInformation.FindAllAsync(SerialDevice.GetDeviceSelector());

PuertosSerieDisponibles.Clear();

PuertosSerieDisponibles.Add("NO COM");

foreach (DeviceInformation serialDeviceInfo in serialDeviceInfos)

{

try

{

SerialDevice serialDevice = await SerialDevice.FromIdAsync(serialDeviceInfo.Id);

if (serialDevice != null)

{

PuertosSerieDisponibles.Add(serialDevice.PortName);

} else

{

// Nothing for now

}

}

catch (Exception)

{

// Error. Nothing for now

}

}

}

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.
798 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.
11,039 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Paul Harvey Chiverton Messina 5 Reputation points
    2024-03-14T16:02:17.3966667+00:00

    Dear Yang:

    Found the solution to the problem.

    Somehow serialDevice is still held somewhere by the SerialDevice serialDevice = await SerialDevice.FromIdAsync(serialDeviceInfo.Id); instruction and once detected it does not detect it again and indicates null at if (serialDevice != null) so once detected it never detects it again even though the foreach goes through each element in the Device collection where the ports ARE accounted for.

    The solution is that after the detection as not null (the first time it detects it) of the COM port I have added a serialDevice.Dispose() which frees this serialDevice for later detection on another click of the update button. The issue changing from one app in the task bar to another doesn't trigger the function anymore. Maybe some kind of bug or unexpected behaviour but the serialDevice.Dispose() got rid of this issue also.

    All works now.

    Your help has been very much appreciated. Thank you.

    The code of the async function is as follows:

    private async void ActualizarPuertosSerie()

    {

    serialDeviceInfos = await DeviceInformation.FindAllAsync(SerialDevice.GetDeviceSelector());

    PuertosSerieDisponibles.Clear();

    PuertosSerieDisponibles.Add("NO COM");

    foreach (DeviceInformation serialDeviceInfo in serialDeviceInfos)

    {

    try

    {

    SerialDevice serialDevice = await SerialDevice.FromIdAsync(serialDeviceInfo.Id);

    if (serialDevice != null)

    {

    PuertosSerieDisponibles.Add(serialDevice.PortName);

    serialDevice.Dispose();

    } else

    {

    // Nothing for now

    }

    }

    catch (Exception)

    {

    // Error. Nothing for now

    }

    }

    }

    1 person found this answer helpful.

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.