The Netduino forums have been replaced by new forums at community.wildernesslabs.co.
This site has been preserved for archival purposes only
and the ability to make new accounts or posts has been turned off.
I have seen that the nearest thing to 2d array[,] is jagged array[][].
The jagged array is usually contain address (or pointer) to another arrays, its kind of index of arrays.
My code is very complicated, but I'll make it simple in order to present my problem.
(The actual code is much more complicated, but the idea is presented below)
int[] numbers1 = new int[5];
int[] numbers2 = new int[5];
int[] numbers3 = new int[5];
int[] temporary = new int[5];
..
.Some code to fill up all the arrays with numbers..
...
int [][] numbersArr = new int[3][];
tmporary = numbers1;
numberArr[0] = temporary;
tmporary = numbers2;
numberArr[1] = temporary;
tmporary = numbers3;
numberArr[2] = temporary;
.
some more code which involve changing the all the "numbers#" arrays
The result is numberArr[0] numberArr[1] NumberArr[2] points to the same array, which is "temporary"
but what I need actually that it will have a copy of "numbers" Arrays.(Which you'd get if you use 2d array)
I have tried to use "CopyTo" but I get the same result.
The actual code is getting data from COM1, and store it in 2d array, so I have mass of data, and the actual code is in a "while" loop, (about 64 loops)
But I always end with all 64 arrays in numbersArr points to the same temporary COM buffer.
I copied the example code and ran it (you obviously didn't since it has compile errors ) and it works fine. I ran it both on the PC and the Netduino. Here's the code I ran ... you must be doing something else. Note that if you reuse the numbers1..3 arrays that will also change the values in the numbersArr.
OK, so here's the problem. You load the data into XBuffer, then you store a pointer to that bffer into InBuff, you load the next set of data into XBuffer and store the same pointer into the next element of inBuff, and so on. One way to fix it would be to move the new of XBuffer into the loop. As in:
for (Y = 0; Y < Ymax; Y++)
{
Xbuffer = new byte[Xmax];
for (X = 0; X < Xmax; X++)
{
XBuffer[X] = buffer[X + Y * 4];
}
InBuff[Y] = XBuffer;
}
The down side of this is that this will use up twice the amount of memory. If you use the data from the initial buffer it would save the memory.
The pattern used by orenduino may be acceptable for a PC, but sucks a lot of RAM for a Netduino (and may run out of memory).
Some tips:
prefer the old-plain-loop (in a dedicated Thread) over the event handler. If you use the serial for high data rate, the event could fire many times and -let's say- uselessly. Just leave a loop running for incoming data, then interleave each cycle with a brief pause (e.g. 10ms or more). Less elegant, but always under control.
avoid, avoid, avoid re-instantiating (i.e. creating) brand new object on every call. The "buffer" and "XBuffer" arrays are created and destroyed on each event handling, and that's requires a lot of memory and an intensive garbage collection overhead. That is, you'll experience some "pauses" due the memory cleanup. Prefer a long-living instance (e.g. "InBuff"), and scratch your head with some tricky pattern in order to save precious ram.
the Read method of the serial comes already with a copy-oriented signature: just specify the byte-array reference, then the offset and the actual length to be read. Simply store an integer for the offset, the data will be automatically placed in the buffer.
Finally, why do you use two separate array for receive and transmit? The same mechanism seen for RX can also be applied for TX...
Good luck!
Biggest fault of Netduino? It runs by electricity.