1

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.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Goran Belačić
  • 81
  • 2
  • 10
  • How is _"The operation completed successfully"_ an exception message? What type of exception is that? Where is it thrown? – Fildor May 06 '20 at 11:52
  • @Fildor yes, believe me, or not :D It's the "IOException". – Goran Belačić May 06 '20 at 12:00
  • ... odd. And it is from that very catch block in your snippet? – Fildor May 06 '20 at 12:01
  • @Fildor yes, right. – Goran Belačić May 06 '20 at 12:09
  • I don't know how much UWP is at play here but usually my next question would be "are you sure, you safely _**close**_ the port, when your app exits/crashes?" – Fildor May 06 '20 at 12:12
  • 1
    I would say I'm good because the same code works fine in the console app. – Goran Belačić May 06 '20 at 12:22
  • Have a look at this: https://stackoverflow.com/a/22106712/982149 It's a completely different environment, but I think the cause could be in the same "area" ... – Fildor May 06 '20 at 12:27
  • [This](https://techcommunity.microsoft.com/t5/windows-dev-appconsult/how-to-access-virtual-com-port-from-uwp-app/ba-p/720864) may also be interesting for you? – Fildor May 06 '20 at 12:29
  • The creation of your event handler looks weird. Maybe thats the correct syntax for UWP but usually there is a `new` modifier. Something like `_serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);` – Baddack May 06 '20 at 15:21
  • @Baddack Newer C# syntax allow omit evenhandler. You may just `eventfired += AMethodNameMatchingSignatureHere;` – Louis Go May 15 '20 at 03:01

1 Answers1

1

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 .

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • When I run the code sample access is blocked by the system but I never get any prompt to allow access to the serial port. Is there a windows setting somewhere that can be adjusted? – dalchri Aug 05 '22 at 15:21