Buffer problem with Serial

16 Apr 2012

Hey guys! I have found a problem with the Serial Communication. It seems, that some bytes were buffered and are not "readable" till some new bytes arrive. This effect I first noticed, as the communication with XBee Modules failed. It works a bit, and then stop. Not all bytes arrive. After manually injecting "rubbish" bytes it worked again without losses. There is no overflow, only a stupid delay that hangs up the entire communication.

To my program: It will become a Flight Controller. There is a lot of work in the "Mainloop" e.g. Sensor Reading via I2C, Calculations. This takes about 4ms. Another 6ms there is time for handling messages coming in via XBee. These messages were buffered in background, because the buffer of the "Serial" class is only about 16 bytes. For this I am using the MODSERIAL Library. In addition there are some ISRs for reading the PPM signal from the Remote Control Receiver.

This background buffering does not work correctly. I simplified the FlightControl Software to a very basic code:

#include "mbed.h"
#include "MODSERIAL.h"

MODSERIAL PC(USBTX, USBRX, 1024, 1024);

int main()
{    
    PC.baud(230400);
    
    while(1)
    {
        //Do something (simulate work)
        //all incoming messages should be buffered
        wait_ms(1000);
        
        //here the messages were "handled"
        while(PC.readable())
            PC.putc(PC.getc());
    }
}

I have tried different Terminals (Teraterm, SerialPortTerminal, X-CTU) but everywhere the same problem. The program should buffer up to 1024 bytes and then every 1 second send all FIFO-like back. For testing I sended 200 bytes String for better reading. I am sending this String: <<quote>>+---------1234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890098765432112345678901234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890_________|<</quote>>

And here is the protocol of the terminal:

TX:+---------1234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890098765432112345678901234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890_________|

RX:+---------1234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890098765432112345678901234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890_________|

TX:+---------1234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890098765432112345678901234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890_________|

RX:+---------1234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890098765432112345678901234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890______

TX:+---------1234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890098765432112345678901234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890_________|

RX:___|+---------1234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890098765432112345678901234567890ABCDEFGHIJKLMNOPQRSTabcdefghijklmnopqrstuvwxyzzzzz!#+*/-,.-_1234567890_________|

The first packer comes back properly. But the second ends with "1234567890__" what means "_|" is missing, but this was trasmitted with the next packet. This time there were 4 bytes too late. Sometimes there are 12, 16, or 18 bytes in delay. BUT they are not missing.

Where is the buffer mistake?

18 Apr 2012

You might want to check out this forum thread: http://mbed.org/forum/bugs-suggestions/topic/2936 You might be hitting the same race condition that Anthony encountered and fixed.

19 Apr 2012

Yeah! Thank you! This is the solution!

Here is my modified MODSERIAL Library: http://mbed.org/users/BlazeX/libraries/MODSERIAL_mod0/