To start off with, Iām using what looks like modified version of the CodeShare SerialBuffer that Iāve picked up from the forum. Iāve copied what Iām using below.
Commenting out the mySerialBuffer.LoadSerial() doesnāt make a difference. I donāt think itās the processing time of the ISR thatās the problem. As I mentioned in the first post of this thread: overruns occur even when the only thing that happens in the ISR is reading of the serial port buffer and exit. Each time the event is serviced the amount of bytes read are typically quite small (1-10bytes) so for a message of ~60bytes the event handler is called a few times.
Here are a few of the other forum threads that I see have a similar problem:
[ul]RS232 buffer overrun on Cerberus when network is open https://www.ghielectronics.com/community/forum/topic?id=16549&page=1
Serial overruns with ChipworkX https://www.ghielectronics.com/community/forum/topic?id=13036&page=1
RS232 module drops characters using Raptor board https://www.ghielectronics.com/community/forum/topic?id=16336&page=1[/ul]
@ Brett - At the moment the data Iām testing with is just a string from the PC: āTest data 123456789Test data 123456789Test data 123456789\nā
And the response from the G80 is typically something like this (each string is sent every 500ms):
Received: āTest data 123456789Test data 123456789Test data 123456789ā 0
Received: āTest data 123456789Test data 123456789Test data 123456789ā 0
Received: āTest data 123456789Test data 1234567Test data 123456789ā 0
Received: āTest data 123456789Test data 123456789Test data 123456789ā 0
Hereās the SerialBuffer class:
public class SerialBuffer
{
private System.Text.Decoder decoder = System.Text.UTF8Encoding.UTF8.GetDecoder();
private byte[] buffer;
private int startIndex = 0;
private int endIndex = 0;
private char[] charBuffer;
public SerialBuffer(int initialSize)
{
buffer = new byte[initialSize];
charBuffer = new char[256];
}
public void LoadSerial(byte[] data, int startIndex, int length)
{
if (buffer.Length < endIndex + length) // do we have enough buffer to hold this read?
{
// if not, look and see if we have enough free space at the front
if (buffer.Length - DataSize >= length)
{
ShiftBuffer();
}
else
{
// not enough room, we'll have to make a bigger buffer
ExpandBuffer(DataSize + length);
}
}
//Debug.Print("serial buffer load " + bytesToRead + " bytes read, " + DataSize + " buffer bytes before read");
//Array.Copy(data, 0, buffer, endIndex, length);
Array.Copy(data, startIndex, buffer, endIndex, length);
endIndex += length;
}
private void ShiftBuffer()
{
// move the data to the left, reclaiming space from the data already read out
Array.Copy(buffer, startIndex, buffer, 0, DataSize);
endIndex = DataSize;
startIndex = 0;
}
private void ExpandBuffer(int newSize)
{
byte[] newBuffer = new byte[newSize];
Array.Copy(buffer, startIndex, newBuffer, 0, DataSize);
buffer = newBuffer;
endIndex = DataSize;
startIndex = 0;
}
public byte[] Buffer
{
get
{
return buffer;
}
}
public int DataSize
{
get
{
return endIndex - startIndex;
}
}
public void ClearBuffer()
{
startIndex = endIndex;
}
public string ReadLine()
{
lock (buffer)
{
// HACK: not looking for \r, just assuming that \r and \n come together
int lineEndPos = Array.IndexOf(buffer, (byte)0x0A, startIndex, DataSize);
if (lineEndPos > 0)
{
int lineLength = lineEndPos - startIndex;
if (charBuffer.Length < lineLength) // do we have enough space in our char buffer?
{
charBuffer = new char[lineLength];
}
int bytesUsed, charsUsed;
bool completed;
decoder.Convert(buffer, startIndex, lineLength, charBuffer, 0, lineLength, true, out bytesUsed, out charsUsed, out completed);
string line = new string(charBuffer, 0, lineLength);
startIndex = lineEndPos + 1;
//Debug.Print("found string length " + lineLength + "; new buffer = " + startIndex + " to " + endIndex);
return line;
}
else
{
return null;
}
}
}
}