Initialization of the second device always causes an exception. When initialized with any one I2C device - everything is normal. In what may be the problem?
Error:
I2CDevice gyro = new I2CDevice(new I2CDevice.Configuration(0x69, 400)); // ITG3200
I2CDevice compass = new I2CDevice(new I2CDevice.Configuration(0x1E, 400)); // HMC5843
Ok:
//I2CDevice gyro = new I2CDevice(new I2CDevice.Configuration(0x69, 400)); // ITG3200
I2CDevice compass = new I2CDevice(new I2CDevice.Configuration(0x1E, 400)); // HMC5843
Ok:
I2CDevice gyro = new I2CDevice(new I2CDevice.Configuration(0x69, 400)); // ITG3200
//I2CDevice compass = new I2CDevice(new I2CDevice.Configuration(0x1E, 400)); // HMC5843
A first chance exception of type ‘System.InvalidOperationException’ occurred in Microsoft.SPOT.Hardware.dll
An unhandled exception of type ‘System.InvalidOperationException’ occurred in Microsoft.SPOT.Hardware.dll
[/quote]
No it doesn’t! You can have multiple thread and each is reading I2C! The only thing you need to do is declare a global I2CDevice object that they all can share. But then each thread has I2CConfiguration
I only showed you a simple idea on how to use multiple I2C devices but that code is NOT complete NOR it will do anything. You will need to add some mechanism to make your code threads safe like Mike suggested
Ok, thanks.
That is what I had in mind when talking about the meaninglessness of threads in this case. Too high costs of separate threads and the loss of the blocked sensor readings. In this case, preferably poll I2C sensors in a separate class.
You can use my [url]I2CBus - Project Showcase - Netduino Forums I2CBus class I created that implements the singleton pattern. This ensures that you will only have one instance of I2CDevice. All you have to do is pass in different I2CDevice configurations when using read or write transactions.