System.Device.Gpio does not work on Mono
See original GitHub issueWhen using SPI and other protocols on Mono the Windows type is being loaded and fails to load because some fields use WinRT types which don’t exist on Linux.
The workaround or quick fix is to replace new WindowsSpiDevice(...)
with Activator.CreateInstance(Type.GetType(...), ...)
- I’m currently investigating if this is a Mono issue or if there is any better solution to fix this. It should also be fixed for other protocols not just SPI.
Environment.OSVersion.Platform: Unix
Unhandled Exception:
System.TypeLoadException: Could not load type of field 'System.Device.Spi.Windows10SpiDevice:_winDevice' (1) due to: Could not load file or assembly 'Windows.Devices.DevicesLowLevelContract, Version=3.0.0.0, Culture=ne utral, PublicKeyToken=null' or one of its dependencies.
at gpio_test.Program.Main (System.String[] args) [0x00044] in <3861456bb5704006a45ca22f1003fbbd>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Could not load type of field 'System.Device.Spi. Windows10SpiDevice:_winDevice' (1) due to: Could not load file or assembly 'Windows.Devices.DevicesLowLevelCo ntract, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
at gpio_test.Program.Main (System.String[] args) [0x00044] in <3861456bb5704006a45ca22f1003fbbd>:0
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
(Raspbian + C# + Mono) Type initializer for 'System.Device ...
Device.Gpio has not been deeply tested against mono, because it is mostly targeting .NET Core. .NET Framework support is for Windows.
Read more >GPIO on Mono / C# - Raspberry Pi Forums
Just wanted to put a link to a post, for anyone using Mono / C#, I have converted the Python file based GPIO...
Read more >GpioController.OpenPin Method (System.Device.Gpio)
Opens a pin in order for it to be ready to use. The driver attempts to open the pin without changing its mode...
Read more >System.Device.Gpio 1.1.0-prerelease.20153.1
The System.Device.Gpio package supports general-purpose I/O (GPIO) pins, PWM, I2C, SPI and related interfaces for interacting with low level ...
Read more >RaspberryIO - Pi's hardware access from .NET
For Debian Stretch. Running .NET Core 3.1; Usage. Run the app on the raspberry. Obtaining Board and System Information; Using the GPIO Pins....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The way to migrate is to remove all references to winmds and
System.Runtime.WindowsRuntime
andSystem.Runtime.InteropServices.WindowsRuntime
and instead just reference theMicrosoft.Windows.SDK.NET
package.By the release of .NET 5 or 6, there should be a way to reference the package implicitly when targeting Windows to reduce the deployment size.
If you find any issues, I suggest reporting them at https://github.com/microsoft/cswinrt.