dotnet/runtime

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

クローズ

#97,646 opened on 2024/01/29

 (5 件のコメント) (0 件のリアクション) (0 人の担当者)C# (5,445 件のフォーク)batch import
area-System.IO.Portshelp wanted

Repository metrics

Stars
 (17,886 個のスター)
PR merge metrics
 (平均マージ 12d 11h) (30d で 661 merged PRs)

説明

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

コントリビューターガイド