adafruit / AdafruitClassLibrary

Windows IoT Core libraries for Raspberry Pi
MIT License
26 stars 11 forks source link

Value cannot be null.:I2C Write Exception: {0} #6

Open FishOfPrey opened 7 years ago

FishOfPrey commented 7 years ago

When attempting to use the nuget packages PCA9685 class I was getting the following error logged after calling SetAllPWM().

Value cannot be null.:I2C Write Exception:

Instead of the nuget package I pulled the latest source down from here. The null reference exception was coming from Device being null. At first I thought it wasn't being set correctly in InitI2CAsync, but then found that InitPCA9685Async was also throwing and logging an exception. With the exceptions being logged and then ignored it's difficult to go straight to the cause of the problem.

Then underlying error was:

Could not load file or assembly 'System.Runtime.WindowsRuntime,...

Found the solution:

the default NuGet package (Microsoft.NETCore.UniversalWindowsPlatform) had to be upgraded to the latest version (5.2.2) as well!

Once I updated UWP to 5.2.3 it started working.

lloydjatkinson commented 7 years ago

I agree, logging the exception and then immediately discarding it is not the approach that should be taken, but it currently is in the method you mentioned:

    public async Task InitPCA9685Async(I2CSpeed i2cSpeed = I2CSpeed.I2C_100kHz)
    {
        try
        {
            await InitI2CAsync(i2cSpeed);

            Reset();
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
            return;
        }
    }

Especially not in an "init" type method, it should either succeed or not and allow the developer to decide what happens when it does not succeed. In this case logging and rethrowing is the correct approach.

e.g:

        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
            throw; // Allow consumer to decide how to handle it.
        }

or:

        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
            throw new IOException("Could not initialise device.", e); 
            // Allow consumer to decide how to handle it and also throw new
            // more specific exception while preserving stack trace.
        }
SindreSB commented 7 years ago

I just had this same issue. The project needs to be updated to UWP 5.4.0.

Solution was to download the source, and update it.

wimdecorte commented 6 years ago

That solution did not work for me. Cloned the project into VS2107, updated the project to UWP latest 6.1.5. Getting the I2C write error and null exception on every second time it runs. The code is run in Windows 10 IoT 10.0.17134.112 with a ThreadPoolTimer...