SerialPort *****assert***** and UTF8 issues

For the benefit of others.

I am using 2 serial ports running at 9600baud.

On occasion and mostly when I stopped and restarted the serial port I would get either at assert or BUFFER OVERFLW message on the LCD. This would mainly happen when I woke from a hibernate state and as such didn’t have the debugger running.

Here is an extract of my code

int bytesToRead = SerialHandle.BytesToRead;
byte[] b = new byte[bytesToRead];
SerialHandle.Read(b, 0, b.Length);
tmp = new string(UTF8Encoding.UTF8.GetChars(b));

Eventually I tracked it down to a synchronization error in the received data bytes. The serial stream I am receiving is exclusively ASCII(7 bit) coded strings. So in theory the first bit should always be ‘0’. If this is not the case the UTF8 decoder tries to decode the character as a longer unicode char and causes an error when UTF8 tries to reach beyond the end of my array to decode a multi-byte char.

My current solution as suggested here(C# console to NetDuino help? - Visual Studio - Netduino Forums) is to replace UTF8 with a custom decoder. This will still generate garbage, but that is generally ignored by my parsing code.

//New ASCII conversion to replace UTF8
tmp = new string( Bytes2Chars(b));

        /// <summary>
        /// Converts a byte array to a char array
        /// </summary>
        /// <param name="Input">The byte array</param>
        /// <returns>The char array</returns>
        public static char[] Bytes2Chars(byte[] Input)
            char[] Output = new char[Input.Length];
            for (int Counter = 0; Counter < Input.Length; ++Counter)
                Output[Counter] = (char)Input[Counter];
            return Output;

Unfortunately this is much slower than the native UTF8 method.

[EDIT] Running on the G120 module.

But what does the assert / Buffer overflow has to do with the string conversion?
Normally you get an IndexOutOfBoundsException when you reach beyond the end of the buffer :think:

Yes, but I didn’t have the debugger connected. I think what was happening was the serial port rx buffer was overflowing. I actually had this whole thing within a try.catch block but it didn’t help.

@ hagster -

what version are you using, and what device, please?

g120 with latest firmware and boot loader from the 2014 R1.

some time ago I observe a similar strange behavior.
Receiving data with 9600 baud i always get buffer overrun. By increase the serial speed (i.e. 19200 - 38400 baud) i solve the problem. Don’t know why receiving the same amount of data needs more resources at 9600 baud ??

1 Like

Thanks Daniel. I have control over the baud on one of my inputs so I’ll try that to see what it does.