Search code examples
c#uwpserial-portwindows-community-toolkitvirtual-serial-port

C# UWP - cannot connect to the serial port


I'm working on UWP app and need to read data from a serial port and display them on the graph.

I'm getting 3 random exception messages :

Message 1 : "The operation completed successfully"

Message 2 : "An attempt was made to reference a token that does not exist"

Message 3 : "The requested lookup key was not found in any active activation context"

I don't have a live connection, but I installed a simulator. When I'm trying to establish a connection from the console app it works fine.

Here is my code, which is really simple:

_serialPort = new SerialPort(portName, baudRate);
_serialPort.DataReceived += _serialPort_DataReceived;
try
{
    _serialPort.Open();
}
catch (Exception ex)
{

}

From the _serialPort_DataReceived method I want to update the graph.

I know that there is some limitation with the serial port on UWP, but I can't find an appropriate solution.

There are similar questions on Stack Overflow, but none of the answers are a solution to my problem.


Solution

  • I'm working on UWP app and the case is to read data from a serial port and show them on the graph.

    In UWP we use Windows.Devices.SerialCommunication namespace to access serial port. For using above namespace we need add the following capability.

    <Capabilities>
      <DeviceCapability Name="serialcommunication">
        <Device Id="any">
          <Function Type="name:serialPort" />
        </Device>
      </DeviceCapability>
    </Capabilities>
    

    For the detail steps, please refer this code sample .