dotnet/runtime

System.IO.Ports - SerialDataReceivedEventHandler not fired after device restart

Closed

#97,646 opened on Jan 29, 2024

 (5 comments) (0 reactions) (0 assignees)C# (5,445 forks)batch import
area-System.IO.Portshelp wanted

Repository metrics

Stars
 (17,886 stars)
PR merge metrics
 (Avg merge 12d 11h) (661 merged PRs in 30d)

Description

Description

I have purchased this device (external link) in order to read card number whenever a card is presented to the inbuilt 125 kHz card reader.

Preinstalled, there is an java application (to which I was provided with source code) which can read the number and display it.

Based on that, I have created a .NET MAUI application that can connect to the internal port /dev/ttyS1 and start to listen for received data. It uses the System.IO.Ports library.

My MAUI application works great and whenever I present a 125 kHz card, I can see its number. BUT when I restart the mobile device and launch my application, no data is received upon presenting the card. I have to start the preinstalled application, then I can quit it, launch my application and only then my MAUI application starts to listen for data.

Since I am not a Java programmer, I do not know, what I am missing that the preinstalled application does have and mine does not.

Moreover, in the project, I had to include <PackageReference Include="System.IO.Ports" Version="9.0.0-alpha.1.24058.11" />

because with the current latest stable versions I was getting an error when deploying the code to the device (to a simulator as well, only Windows machine was working).

Reproduction Steps

using System.IO.Ports;

private SerialPort port = new("/dev/ttyS1")
{
    BaudRate = 9600,
};

port.Open();
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

private async void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    port.ReadExisting();
}

Expected behavior

When I present a 125 kHz card to the reader, the program should enter the port_DataReceived method.

Actual behavior

I put breakpoint right at the beginning of the port_DataReceived method, but it did not enter it.

The debugger, though, says that the port is open.

Regression?

No response

Known Workarounds

Only way to get it working, after a device restart, is to launch the pre-installed Java application. Only after then the MAUI application is working flawlessly.

Configuration

<TargetFrameworks>net8.0-android;net8.0-maccatalyst</TargetFrameworks>

Other information

No response

Contributor guide