Hi Everyone,
Another G30 Serial Question. NetMF 4.3.
So I want to use a Circular Buffer. And I also want to heed advice I have received about keeping the SerialDataReceivedEventHandler as tight as possible.
I made a Circular Buffer class. It has a byte array as the Buffer like this:
public byte[] Buffer { get; private set; }
The first thing I want to find out is that if class users Get the Buffer is it going to be a copy? Def don’t want that.
And the private set: I made a function CircularBuffer.Write with two signatures:
public void Write(byte[] data)
{
for ( int i = 0; i < data.Length; i++)
{
Write(data[i]);
}
}
public void Write(byte data)
{
Buffer[WriteIndex] = data;
WriteIndex = (WriteIndex + 1) % Length;
}
WriteIndex is an Interger belonging to an instance of this class. This is the only way it gets updated. Length is const, the size of the buffer.
Again am I making copies of byte arrays when I pass them into Write(byte[] data) ?
Finally, I’m stuck on what I should do in the event handler. Should I
static void BufferIncomingBytes()
{
int NumBytesToRead = UART.BytesToRead;
while (NumBytesToRead > 0)
{
CircularBufferInstance.Write((byte)UART.ReadByte());
NumBytesToRead--;
}
}
or is it better to
static byte[] tempBuffer = new byte[512]; // size of G30 RX Buffer
static void BufferIncomingBytes()
{
int NumBytesToRead = UART.BytesToRead;
UART.Read(tempBuffer, 0, NumToRead);
CircularBufferInstance.Write(tempBuffer);
}
Making this decision is paralyzing to me. Was hoping somebody with experience could shed some light on what the pros and cons are. And maybe if I’m doing anything foolish here.
Perhaps if I did the tempBuffer method I should turn the Write function into one or two calls to array.Copy? (one or two depending on whether it has to wrap around to the front of the buffer)