dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.47k stars 4.76k forks source link

SerialPort is not supported on Android ? #86619

Closed Strawberrybeans closed 12 months ago

Strawberrybeans commented 1 year ago

Description

when open serialport Exception : System.DllNotFoundException: 'libSystem.IO.Ports.Native'

Reproduction Steps

1.Create a SerialPort class 2.Configure the serial port settings 3.Open the serial port

Expected behavior

open serialport success

Actual behavior

monodroid-assembly Shared library 'libSystem.IO.Ports.Native' not loaded, p/invoke 'SystemIoPortsNative_SerialPortOpen' may fail

Regression?

No response

Known Workarounds

No response

Configuration

.net core7.0 android 7.1 arm

Other information

No response

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-system-io-ports See info in area-owners.md if you want to be subscribed.

Issue Details
### Description when open serialport Exception : System.DllNotFoundException: 'libSystem.IO.Ports.Native' ### Reproduction Steps 1.Create a SerialPort class 2.Configure the serial port settings 3.Open the serial port ### Expected behavior open serialport success ### Actual behavior monodroid-assembly Shared library 'libSystem.IO.Ports.Native' not loaded, p/invoke 'SystemIoPortsNative_SerialPortOpen' may fail ### Regression? _No response_ ### Known Workarounds _No response_ ### Configuration .net core7.0 android 7.1 arm ### Other information _No response_
Author: Strawberrybeans
Assignees: -
Labels: `untriaged`, `area-System.IO.Ports`
Milestone: -
ghost commented 1 year ago

Tagging subscribers to 'arch-android': @steveisok, @akoeplinger See info in area-owners.md if you want to be subscribed.

Issue Details
### Description when open serialport Exception : System.DllNotFoundException: 'libSystem.IO.Ports.Native' ### Reproduction Steps 1.Create a SerialPort class 2.Configure the serial port settings 3.Open the serial port ### Expected behavior open serialport success ### Actual behavior monodroid-assembly Shared library 'libSystem.IO.Ports.Native' not loaded, p/invoke 'SystemIoPortsNative_SerialPortOpen' may fail ### Regression? _No response_ ### Known Workarounds _No response_ ### Configuration .net core7.0 android 7.1 arm ### Other information _No response_
Author: Strawberrybeans
Assignees: -
Labels: `os-android`, `untriaged`, `area-System.IO.Ports`
Milestone: -
steveisok commented 1 year ago

@akoeplinger we should at least throw a PNSE if it's not implemented as opposed to failing to pinvoke.

wfurt commented 12 months ago

This also may be just packaging. We ship serial port as separate package on Unix https://www.nuget.org/packages/System.IO.Ports/ But we missed some platforms there - like ARM on Alpine. Fix may be as simple as #92145 but I don't have Android to test on.

wfurt commented 12 months ago

also seems like dup of #58740

akoeplinger commented 12 months ago

I researched a bit and it looks like while in theory the serial port devices are there on Android there's a SELinux policy which prevents you from accessing them: https://stackoverflow.com/questions/30742524/why-cant-i-open-write-from-a-serial-port-on-android

That'd really only leave rooted devices or custom OEM versions of Android, I don't think it makes sense for us to support those.

I'll add PNSE and the supported OS attributes.


If you need to access USB-to-serial devices then this library looks like a good alternative choice since it uses the USB Host APIs from Android instead: https://github.com/anotherlab/UsbSerialForAndroid

BaY1251 commented 12 months ago

Devices that can use serial ports are actually all customized systems. The issues with SELinux do not need to be treated as obstacles

BaY1251 commented 12 months ago

It seems that just need to building with NDK? Like Building OpenSSL for Android

akoeplinger commented 12 months ago

Ok yeah as long as users will know what to do when they run into the SELinux issues I guess it doesn't hurt.

@BaY1251 do you have a way to test a fix? I don't have an Android device with serial ports and I'm wondering whether the Linux logic to determine the port names works: https://github.com/dotnet/runtime/blob/303571a0b13169d58716df928280f47b23e5b85a/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Unix.cs#L28

lkhore commented 12 months ago

akoeplinger You can use usb type c otg cable for the test with Android device, Provide whether support or not Which version I need to use for test

akoeplinger commented 12 months ago

Thanks, I've merged the PR and in a few hours we should get packages to test.

BaY1251 commented 12 months ago

@akoeplinger I am happy to test it, Please let me know when the test package is available

This address may be helpful, SocketAssistant

wfurt commented 12 months ago

https://dev.azure.com/dnceng/public/_artifacts/feed/dotnet9/NuGet/System.IO.Ports/versions/9.0.0-ci

You will probably need to add this to you Nuget config. The packages are not pushed to NuGet.org.

<add key="dotnet9" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet9/nuget/v3/index.json" />

But it seems like the current 9.0.0-alpha.1.23607.6 still does not have the change.

BaY1251 commented 12 months ago

Yes. Thank you. i will try later

akoeplinger commented 11 months ago

9.0.0-alpha.1.23608.10 does have the change.

akoeplinger commented 11 months ago

Hmm but I just saw that the corresponding runtime.android-arm64.runtime.native.System.IO.Ports package doesn't seem to be on the feed, while the linux-bionic one added in the same PR is...

akoeplinger commented 11 months ago

I figured out the issue, it is related to DefaultPrimaryRuntimeFlavor being CoreCLR on Android (which sounds wrong) and it skips building the RID specific packages in the Mono leg then. This will need a bit more thinking to not break anything :)

BaY1251 commented 11 months ago

Yes. Unable to resolve dependency 'runtime.android-arm.runtime.native.System.IO.Ports'. So currently it is still unavailable..

akoeplinger commented 11 months ago

@BaY1251 would you mind trying again with version 9.0.0-alpha.1.23614.10? it should work now.

lkhore commented 11 months ago

Hi @akoeplinger , I am getting below error so how i can utilize the io function

{DeviceDriver.CommunicationException: 4223: Unable to connect to , Access was denied.
 ---> System.UnauthorizedAccessException: Access to the port '/dev/ttyUSB0' is denied.
 ---> System.IO.IOException: Permission denied
   --- End of inner exception stack trace ---
   at System.IO.Ports.SafeSerialDeviceHandle.Open(String portName)
   at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
   at System.IO.Ports.SerialPort.Open()
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 151
   --- End of inner exception stack trace ---
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 160
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Connections.SerialConnection.Open() in D:\Repo\Xamerin\MESP\DeviceDriver\Connection\SerialConnection.vb:line 163
   at DeviceDriver.Commands.Dispenser.InitializationCommand.TryCreateConnection(String& ADataLinkName, Int32 ADatalinkBaudrate, BaseConnection& AConnection) in D:\Repo\Xamerin\MESP\DeviceDriver\Commands\Dispenser\InitializationCommand.vb:line 81}
akoeplinger commented 11 months ago

@lkhore yeah as explained in https://github.com/dotnet/runtime/issues/86619#issuecomment-1843502371 SELinux will block this by default, you need to disable it (only possible on rooted phones or custom Android builds)

BaY1251 commented 11 months ago

@akoeplinger I test 'GetPortNames','Open','Write','Read','DiscardInBuffer','DiscardOutBuffer' . It Works! The third-party library can finally be laid off

akoeplinger commented 11 months ago

Awesome, thanks for the confirmation!