I still don't think that there is a need for a ring buffer here.
However, if you want to use one, it must be defined at class level and not within your receive handler where it goes out of scope after each event.
You already have a buffer:
rxBuffer
. To make that a ring buffer all you have to do is handling the write access using
rxOffset
accordingly:
int bytesToRead = min(bytesCount, rxBuffer.Length - rxOffset);
port.Read(rxBuffer, rxOffset, bytesToRead);
rxOffset += bytesToRead;
if (rxOffset >= rxBuffer.Length)
{
bytesToRead = bytesCount - bytesToRead;
port.Read(rxBuffer, 0, bytesToRead);
rxOffset = bytesToRead;
}
Now you have a ring buffer. Depending on what to do with that you might implement a reader having another class level variable defining the current read position (and checking that
rxOffset
will not overrun that read position).
But implementing the above you might recognise that it does not solve your problem. Your problem is actually not how to implement a ring buffer but to define how the received data has to be handled (processed). That is a task which must be done before writing any line of code. Once you have defined that, you can think about possible implementations.