I2CBus.Read does not alter the input buffer?

Jan 18, 2012 at 12:14 AM

I am pretty new to gadgeteer programming, therefore I assume that I am doing something fundamentally wrong, but cannot figure out what.

I am reading data from an I2C device, which is attached to a Sytech nano mainboard, using I2CBus.Read(). The return value of this call tells me that 4 bytes of data have been read from the device and I believe this, because as soon as I change the I2C address of the device, or power off the device, the return code tells me that it do read 0 bytes.

My problem is that the input buffer which I provide is never altered by the call, i.e. even if I2CBus.Read() tells me that it has read 4 bytes from the device, the 4 byte buffer has exactly the same value as before the call - no matter which value it had prior to the call.

This really confuses me. The data seems to be read correctly from the device, but I cannot get the data.

Please find below the code and the resulting output:

private byte[] _readBuffer32 = new byte[4];

      void ProgramStarted()
      {
         int frequency = 100;

         Gadgeteer.Socket i2cSocket = Gadgeteer.Socket.GetSocket(6, throwExceptionIfSocketNumberInvalid: true, module: null, socketLabel: null);
         GTI.I2CBus adc = new GTI.I2CBus(i2cSocket, 0x68, frequency, null);

         int count = 0;
         int nrBytesRead;

         while (true) {
            for (int i = 0; i < _readBuffer32.Length; ++i) {
               _readBuffer32[i] = (byte)count;
            }
            nrBytesRead = adc.Read(_readBuffer32, 10);
            Debug.Print(count + ": read " + nrBytesRead + " bytes @ " + frequency + " kHz");
            for (int i = 0; i < _readBuffer32.Length; ++i) {
               Debug.Print("readBuffer[" + i + "] = " + _readBuffer32[i]);
            }

            Thread.Sleep(1000);
            count++;
         }
      }

 

Output:

Using mainboard Sytech Designs Ltd Nano version 1.0
0: read 4 bytes @ 100 kHz
readBuffer[0] = 0
readBuffer[1] = 0
readBuffer[2] = 0
readBuffer[3] = 0
1: read 4 bytes @ 100 kHz
readBuffer[0] = 1
readBuffer[1] = 1
readBuffer[2] = 1
readBuffer[3] = 1
2: read 4 bytes @ 100 kHz
readBuffer[0] = 2
readBuffer[1] = 2
readBuffer[2] = 2
readBuffer[3] = 2

private byte[] _readBuffer32 = new byte[4];

      void ProgramStarted()
      {
         int frequency = 100;

         Gadgeteer.Socket i2cSocket = Gadgeteer.Socket.GetSocket(6, throwExceptionIfSocketNumberInvalid: true, module: null, socketLabel: null);
         GTI.I2CBus adc = new GTI.I2CBus(i2cSocket, 0x68, frequency, null);

         int count = 0;
         int nrBytesRead;

         while (true) {
            for (int i = 0; i < _readBuffer32.Length; ++i) {
               _readBuffer32[i] = (byte)count;
            }
            nrBytesRead = adc.Read(_readBuffer32, 10);
            Debug.Print(count + ": read " + nrBytesRead + " bytes @ " + frequency + " kHz");
            for (int i = 0; i < _readBuffer32.Length; ++i) {
               Debug.Print("readBuffer[" + i + "] = " + _readBuffer32[i]);
            }

            Thread.Sleep(1000);
            count++;
         }
      }
Coordinator
Jan 19, 2012 at 11:31 AM

Thanks for the post.  This led us to find a bug in GadgeteerCore - I2CBus.Read() does not work in 2.41.401 - 2.41.500 will fix this (released in the next week or so).   Thanks also to Simon Taylor at SyTech for helping to spot the bug. 

Workaround: in the meantime, you can use the method I2CBus.Execute as below:
adc.Execute(new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(_readBuffer32) }, 10);

Finally, a few links that might interest you:

1) http://blogs.msdn.com/b/net_gadgeteer/archive/2011/12/19/why-not-while-true.aspx - while(true) in ProgramStarted is not a scalable programming model :)

2)http://10rem.net/blog/2011/10/30/building-a-net-gadgeteer-compatible-hardware-and-software-module-der-blinkenled - what you're doing is actually making a Module, not an App, so you might want to consider using the Module template found in the GadgeteerBuilderTemplates msi .

Jan 19, 2012 at 6:54 PM

James, Simon,

thank you so much for the great support!