I have an Atmel Atxmega128A1 microcontroller that is sending a message to the PC (c#) application through a serial COM port. Occasionally I start receiving a large string that looks like this (PcIocBus is a class):
"PcIocBus: Invalid message RX: 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00"
in my Visual Studio debug output.
Every time the 0s show up we get an exception
A first chance exception of type System.ArgumentException occurred in system.dll Additional information: Offset and length were out of bounds for the array or count is greater than the nymber of elements from index to the end of the source collection.
which is showing up at the 'Port.Read..." line.
private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] rxBuff = new byte[255];
int bufferSize = Port.BytesToRead;
try
{
Debug.WriteLine("Buffer Size: " + bufferSize);
Port.Read(rxBuff, 0, Port.BytesToRead);
foreach (byte newByte in rxBuff)
{
InBytes.Enqueue(newByte);
}
}
catch (Exception)
{
Debug.WriteLine("PcIocBus: Invalid message RX: " + BitConverter.ToString(rxBuff)):
}
}
Does anyone might know why this is giving me the errors? I've tried setting the rxBuff
array to the same size of the buffer because the buffer (for some reason) does go over 255 sometimes, but I still get the 0s and the exception.