Simulating I2C Communication - Panda

Hi, I’m awaiting the delivery of my panda, and in the meantime I’m getting the code ready.

Is it possible to test I2C code without the physical device?

And while I’m here, whats wrong with this? I really can’t work out why it’s complaining, it throws an System.ArgumentNullException.

            I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[7];

            byte[] Cps_adr = new byte[1] { data_adr };

            xActions[0] = I2CDevice.CreateWriteTransaction(Cps_adr);

            byte[][] Cps_val = new byte[6][];

            for (int i = 1; i <= 6; i++)
                xActions[i] = I2CDevice.CreateReadTransaction(Cps_val[i-1]);


The exception is thrown on the first iteration of the for loop, so when i = 1.

Btw in case you’re wondering, the device increments the read address itself each time it is accessed, so hopefully if I send 6 reads, I should be able to read the 6 registers containing data.

PS: I’m new to C#, so go easy on me :).

Maybe because Cps_val[] contains empty arrays when you pass it to the CreateReadTransaction method ?

In the NETMF samples folder there is an I2C emulator ( temperature ) Why not just adjust this code to suit?

Cheers Ian

I thought that’s it’s after, an empty byte array to use as a buffer?

CreateReadTransaction(byte[] buffer) - I assumed its asking for an empty byte to receive the data. What would be the correct way of making it do what I want? (I’m after an array containing the 6 registers, 3-8)

And thanks Ian, I’ll look into the sample one.

Edit: Sorry, where do I get the sample folder from?

Hi Xarren,

You have allocated an array that has 6 null references.

 byte[][] Cps_val = new byte[6][];

You have to do another loop to allocate actual byte arrays

CreatReadTransaction expects a buffer (doesn’t matter if it is empty or not),but you have to allocate it.

for( int i = 0; i < 6; i++)
    //you need to allocate enough space here, make sure the buffer is big enough
    //change 2 to whatever is expected
    Cps_val[i] = new byte[2]; 

My sample folder is here

Yours will be same place I should imagine
but the others are correct the buffer in the transaction is null

Cheers Ian

@ Bec a fuel : can we call you Chris?

Of course you can !
Or maybe [italic]Christophe[/italic], so that our already existing Chris would not take flames not directed to him ? :smiley:

Thank you for all the help guys.

And thats rather cumbersome, that loop, but I guess thats typical for microsoft - Still, microsoft or not, Im suprised I can’t do byte[6][1], which is what i tried to start with.

@ Xarren What ever you do don’t declare like I did once " new byte(2,2) "

In net its fine but in it just fails with no help warnings whatsoever…

Cheers Ian

According to the Wikipedia [url][/url] NETMF does not support multi-dimensional arrays at all. So I’m surprised the code even compiled/linked. Perhaps suitable error checking doesn’t exist to flag this at compile time.

byte[,] : multidimensional array, not supported
byte[][] : array of arrays or jagged array, supported

Thanks for all the input guys, but since everyone is being rather sketchy about this, is it because everyone else does it a certain other way and I’m just going about this completely the wrong way? How would you guys do it? In general what do you use to store linked data in NETMF?

Xarren… On fezzer you’ll find loads of different drivers for compases, RTC’s Eeproms and loads more… This is the beauty of this forum… we’re all helping each other…

Write some code… Paste it here… and we’ll all help you…

There are a number of data storage options… bec_a_fuel (chris) has just finished an I2C eeprom driver… up to 2048 megabit (256k) data storage.

Wire up an SD card and you’ll find drivers all ready built in (several users have connected SD cards to panda )

Cheers Ian

Cheers for pointing that website out to me, it looks great :slight_smile:

Theoretically you don’t need the loop to set each instance.

You can use:

            byte[][] b = new byte[6][] { new byte[2], 
                                        new byte[2] , 
                                        new byte[2] , 
                                        new byte[2] , 
                                        new byte[2] , 
                                        new byte[2] };

But I always just get “?” then I look at the array?


That’s a visual C# thing! I create a jagged array of around 16 x 30 and I can’t see the variables in it also just ???.

Cheers Ian

NETMF does support ArrayList though. So you can create an ArrayList of any flavor of array (any object for that matter), Which gives you in effect a 2D array.