What could cause repeated InvalidOperationExceptions in I2cDevice's WriteRead()?

I keep getting the following exception when using an I2cDevice’s WriteRead() function.

An unhandled exception of type ‘System.InvalidOperationException’ occurred in GHIElectronics.TinyCLR.Devices.I2c.dll

Does anyone have any advice on what can cause this exception from this method?

In one project I’ve seen it only occur when I build a new version of the project (sometimes, not every time), and I there I am able to hackily solve it by rebuilding the project with a UART write added just before the I2C WriteRead, at which point the code would run happily. I could then remove the added UART write and rebuild to run successfully. This (running the same code with different results after making and correcting a change) felt like some kind of timing issue to me, but all of my code is running in a single thread, so I don’t understand where that kind of issue could come from… I doubt it would be an issue with the implementation of WriteRead itself.

In another project now the exception occurs on every WriteRead and I can’t find any way around it, so I’m forced to try to figure out what’s actually causing the issue.

As always, any help greatly appreciated!

Does it always raise invalid exception or just sometime?

if it is always then it could be invaild slave address, or you may need 2.2K pull up resistors.

Do you have any I2C analyzer?

Hi,

In one project it always raises invalid exception (on every call of WriteRead). In another project on some builds it raises invalid exception (I think on every call of WriteRead), but on builds ‘fixed’ in the way I described above it never does.

Unfortunately I don’t have access to an I2C analyser, I’m sure there’s one in the office, but my home ‘workshop’ is far more limited!

then check your connection and slave address.

The driver take address and do <<1 for write and <<1 + 1 for read. So you may do address>>1 before pass to I2C config, depends on your data sheet

Thanks for the advice, I’ll try this for the one project that fails every time. I just need to deal with with some of the issues that have come up since working from home has kicked off before I can test it. However for the project that only fails on some builds I don’t think this could be the issue - any ideas on what could be causing the issue there?