dotnet / iot

This repo includes .NET Core implementations for various IoT boards, chips, displays and PCBs.
MIT License
2.16k stars 582 forks source link

System.IO.IOException: Error performing I2c data transfer #163

Closed ZhangGaoxing closed 5 years ago

ZhangGaoxing commented 5 years ago

In Raspberry Pi 3B+ with Raspbian.

When I use UnixI2cDevice.WriteRead(), the program random throw System.IO.IOException: Error performing I2c data transfer. Sometimes 10 calls occur, and sometimes one call occurs. Why?

My Read() Method

public MLX90614Data Read()
{
    byte[] readBuf = new byte[2];
    MLX90614Data data = new MLX90614Data();

    sensor.WriteRead(new byte[] { MLX90614_AMBIENT_TEMP }, readBuf);
    data.AmbientTemp = BitConverter.ToInt16(readBuf, 0) * 0.02 - 273.15;
    sensor.WriteRead(new byte[] { MLX90614_OBJECT_TEMP }, readBuf);
    data.ObjectTemp = BitConverter.ToInt16(readBuf, 0) * 0.02 - 273.15;

    return data;
}

My Main() Method

while (true)
{
    MLX90614Data data = sensor.Read();

    Console.WriteLine($"Ambient Temperature: {data.AmbientTemp} ℃");
    Console.WriteLine($"Object Temperature: {data.ObjectTemp} ℃");
    Console.WriteLine();

    Thread.Sleep(2000);
}

My Terminal Log

True data

Ambient Temperature: 22.79 ℃
Object Temperature: 21.67 ℃

Ambient Temperature: 22.83 ℃
Object Temperature: 21.23 ℃

Then the error suddenly occurred.

Unhandled Exception: System.IO.IOException: Error performing I2c data transfer
Result: -1 ErrorNumber: 121 Error: 131071 ErrorMessage: Remote I/O error
   at System.Devices.I2c.UnixI2cDevice.RdWrInterfaceTransfer(Byte* writeBufferPtr, Byte* readBufferPtr, Int32 writeBufferLength, Int32 readBufferLength)
   at System.Devices.I2c.UnixI2cDevice.Transfer(Byte* writeBuffer, Byte* readBuffer, Int32 writeBufferLength, Int32 readBufferLength)
   at System.Devices.I2c.UnixI2cDevice.WriteRead(Byte[] writeBuffer, Byte[] readBuffer)
   at Iot.Device.MLX90614.MLX90614.Read() in C:\Users\zhang\source\repos\MLX90614\MLX90614\MLX90614.cs:line 52
   at MLX90614.Samples.Program.Main(String[] args) in C:\Users\zhang\source\repos\MLX90614\MLX90614.Samples\Program.cs:line 17
joperezr commented 5 years ago

Looks like you are still using an old version of the API by looking at the callstack. Have you tried instead using a newer version of the package? We recently shipped a new version to NuGet

ZhangGaoxing commented 5 years ago

thanks