Iot: Development work on Windows 10

Created on 25 Jul 2019  路  6Comments  路  Source: dotnet/iot

I am attempting to do development work on a Windows 10 machine for a project that will be used on a Raspberry Pi. The only issue I am running into is that when I attempt to run my project on the Windows 10 machine I get the following exception No GPIO controllers exist on this system..

Now I understand why that is happening (my laptop obviously doesn't have GPIO pins) but I was wondering if there was a built in way to suppress this error?

area-samples question

Most helpful comment

@alex182 Does your app do other things apart from just controlling the microcontroller? I suppose it does and then that is why you want to just mock that part so you can continue debugging the rest of your code. I'm not sure we have a good way of doing that, but I would suggest two things:

  1. You could locally create a class that extends the GpioDriver which simply doesn't do anything, and then use that to initialize your GpioController. So adding a class that looks like this:
    ```c#
    class GpioDriverMock : GpioDriver
    {
    protected override int PinCount => 0;

    protected override void AddCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback) { }

    protected override void ClosePin(int pinNumber) { }

    protected override int ConvertPinNumberToLogicalNumberingScheme(int pinNumber) => 0;

    protected override PinMode GetPinMode(int pinNumber) => default(PinMode);

    protected override bool IsPinModeSupported(int pinNumber, PinMode mode) => true;

    protected override void OpenPin(int pinNumber) { }

    protected override PinValue Read(int pinNumber) => default(PinValue);

    protected override void RemoveCallbackForPinValueChangedEvent(int pinNumber, PinChangeEventHandler callback) { }

    protected override void SetPinMode(int pinNumber, PinMode mode) { }

    protected override WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken) => default(WaitForEventResult);

    protected override void Write(int pinNumber, PinValue value) { }
    }


And then you could just add this to use this mock when creating your controller:

```c#
using (GpioController controller = new GpioController(PinNumberingScheme.Logical, new GpioDriverMock()))
{
  // Do whatever with your controller.
}

Once you are ready to test in in actual hardware, simply remove the parameters passed in to new GpioController().

  1. The other option would be to just add a try catch to the places where you are hitting the exception, and simply remove them whenever you are ready to deploy.

All 6 comments

@alex182 do you mean i.e.:

using (var controller = new GpioController())
{
// what would you do with `controller`
}

? What would you do with such instance? Could you give some usage example?

@alex182

I am not really answering your question, I know, but I am not sure why you want to execute on the Win10 machine. I have found it reasonably simple to publish the resultant code to a RaspberryPi and then debug using a combination of VS2019 and vsdbg on the Pi. (I guess you could use VSCode but I have not tried it).

This allows you to develop and debug using familiar tools if you are a Windows developer but also see the effects of manipulating Gpio, Spi, I2c etc.

@krwq I am developing an api that runs on the Pi but there are non-GPIO things that I want to be able to debug on my Windows 10 machine (logging, validation etc)

@Frankenslag I am doing that but the deploy takes about 5 minutes each time and I really dont want to be waiting for that when I am tweaking logging/validation

I guess what I am essentially asking is if there is a way to mock the controller somewhere on my Windows 10 machine?

@alex182 Does your app do other things apart from just controlling the microcontroller? I suppose it does and then that is why you want to just mock that part so you can continue debugging the rest of your code. I'm not sure we have a good way of doing that, but I would suggest two things:

  1. You could locally create a class that extends the GpioDriver which simply doesn't do anything, and then use that to initialize your GpioController. So adding a class that looks like this:
    ```c#
    class GpioDriverMock : GpioDriver
    {
    protected override int PinCount => 0;

    protected override void AddCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback) { }

    protected override void ClosePin(int pinNumber) { }

    protected override int ConvertPinNumberToLogicalNumberingScheme(int pinNumber) => 0;

    protected override PinMode GetPinMode(int pinNumber) => default(PinMode);

    protected override bool IsPinModeSupported(int pinNumber, PinMode mode) => true;

    protected override void OpenPin(int pinNumber) { }

    protected override PinValue Read(int pinNumber) => default(PinValue);

    protected override void RemoveCallbackForPinValueChangedEvent(int pinNumber, PinChangeEventHandler callback) { }

    protected override void SetPinMode(int pinNumber, PinMode mode) { }

    protected override WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken) => default(WaitForEventResult);

    protected override void Write(int pinNumber, PinValue value) { }
    }


And then you could just add this to use this mock when creating your controller:

```c#
using (GpioController controller = new GpioController(PinNumberingScheme.Logical, new GpioDriverMock()))
{
  // Do whatever with your controller.
}

Once you are ready to test in in actual hardware, simply remove the parameters passed in to new GpioController().

  1. The other option would be to just add a try catch to the places where you are hitting the exception, and simply remove them whenever you are ready to deploy.

@joperezr thats exactly what I was looking for. And yes the app does more than accessing the micro controller. Thank you.

Cool, closing for now, feel free to reopen if you need any additional help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tragetaschen picture Tragetaschen  路  5Comments

MaxMommersteeg picture MaxMommersteeg  路  4Comments

krwq picture krwq  路  5Comments

Ellerbach picture Ellerbach  路  4Comments

mskuratowski picture mskuratowski  路  3Comments