Best Practice to Access Gpio info in Torizon using a C# .NET application

Setup

  • Custom Hardware Carrier Board with Verdin imx8m-mini
  • Latest Torizon OS installed
  • C# .Net application
  • Modified device tree (renamed Gpios)

Issue Description
We want to access Gpios from our C# .Net application that runs on Torizon in a container. We can already access it by using the System.Device.Gpio as described on your wiki pages and using the terminal on the Linux itself.

The problem we are having is that we want to access these Gpios using the names we defined in the device tree in the application. This to avoid having hardcoded lines and banks in our application code as the device tree already has this information.

One way of doing this would be using the GpioInfo command and parsing the data, but this feels like a workaround to do this. Is there a better way to accomplish this? A solution using a library for C# .Net would be preferred, but if this is not possible another way is also acceptable.

Greetings @Jorn,

Looking at the functions and methods provided by the LibgpiodDriver class I don’t see an obvious way to get the same information as gpioinfo: LibGpiodDriver Class (System.Device.Gpio.Drivers) | Microsoft Learn

Maybe the QueryComponentInformation() method does something? Not sure I’ve never tried it before.

Other than this, I’m not aware of any other libraries/classes in C# that provide such functionality. Though I admit I’m not very knowledgeable about C# development.

Best Regards,
Jeremias

Hello @jeremias.tx,

Thanks for the quick answer. We tried using the method, but it seems the componentinformation is in regard to the driver itself and not the gpios.

Maybe to ask a different follow-up question: is it good practice to call terminal commands like gpioinfo from code and parse the resulting string result? Or is this something that’s discouraged?

Maybe to ask a different follow-up question: is it good practice to call terminal commands like gpioinfo from code and parse the resulting string result? Or is this something that’s discouraged?

I don’t see why not. Would be an odd limitation to put on yourself unless you have some technical design reason not to. I mean I can easily think of at least a dozen tools/code-bases that do something similar. If an external command like gpioinfo has information that your application needs, then why not parse the information from the tool itself? The alternative would be to implement something that does the same thing as gpioinfo in your code. But then you’re just doing redundant work at that point I feel.

Best Regards,
Jeremias

Ok, that’s good to know. It’s our teams first project running an embedded linux with Torizon so we just wanted to check we are doing things in a correct manner.

Thanks for all the help!

Glad I was able to help.