Serial pass through with buffering - help?

28 May 2011

Hi,

One of the features of my project is a serial pass-through connecting the USB serial input port (connected to the PC) to the serial output port on p9 (connects to another device).

Initially, I was using the "Dev::HandleUSBSerial()" ISR in the code snippet below to handle passing through the data, and all was well. But then I wanted to add some logging to the pass through (the "fSnoop.Listen( ch );" line) and it stopped working reliably - it looks like characters are getting dropped after just a few dozen bytes.

The serial ports are operating at just 9600 baud, so I'd think the ARM chip can easily handle stashing the data. Any ideas? As an alternative, I could buffer all of the data before I transmit it; but figuring out when the input stream has stopped is a bit tricky. Is there much overhead to resetting timers?

Thanks, jp

#include "mbed.h"

const int kBufferMax = 600;

class Snoop {
  public:
...
    void Listen( const uint8_t ch );
  private:
...
    uint8_t fBuffer[kBufferMax];
    int fBufferIndex, fLastBufferIndex;
};

void Snoop::Listen( uint8_t ch )
{
    if (fBufferIndex >= kBufferMax)
        fBufferIndex = 0;
    fBuffer[fBufferIndex++] = ch;
}

class Dev {
  public:
...
    void AttachUSBSerial();

  private:
...
    Snoop fSnoop;
    void HandleUSBToLights();
    Serial fPort;
    Serial fUSBPort;
};

void Dev::AttachUSBSerial()
{
    fUSBPort.attach( this, &Dev::HandleUSBSerial );
}

void Dev::HandleUSBSerial()
{
    while (fUSBPort.readable())
    {
        uint8_t ch = fUSBPort.getc();
        fPort.putc( ch );
        // Adding this line causes serial com to fail?!?
        fSnoop.Listen( ch );  
    }
}