Serial Attach Method

25 Feb 2011

Hiya,

I am currently doing my master's project partially using an mbed, designing a inertial navigation system integrated with GPS and a barometric altimeter using an Extended Kalman Filter.

My problem is reproduced in the code as below:

#include "mbed.h"

class tes{
    public:
        
        Serial* p;
        
        tes(Serial* pc, Serial* gps){
            p = pc;
            wait(1);
            p->putc('y');
            
            gps->attach(this, &tes::rx, Serial::RxIrq);
        }
    
        void rx() {
            DigitalOut l(LED2);
            l = 1;
            p->putc('x');
        
        }
};

Serial gps(p28, p27);
Serial pc(USBTX, USBRX);




int main() {  
    
    tes t(&pc, &gps);
    
    
    
    gps.baud (115200);
    pc.baud(115200);
    
    pc.printf("test\r\n");
    
}

When this code is run, two problems occur: 1) The 'x' character is never printed to the pc output, but the LED does come on. 2) The 'y' character is never printed to the pc output.

Playing around with the code, I found that problem 1 can be solved by the following three changes: 1) Declaration of tes t made outside of function main, below the serial declarations. 2) t declared as tes* t = new tes(&pc, &gps), inside function main. 3) t declared as tes* t = new tes(&pc, &gps), outside function main, below the serial declarations.

I do not understand why these should work but the original code does not. I also tried moving the gps.attach call to the main function with the corresponding changes, however the results were the same as for above. However, none of these changes solve problem 2.

Hope that is enough information for someone to help me, I've started pulling my hair out now!

Cheers, Rob

25 Feb 2011

One thing I note about your code is that you change the baud rates of the serial ports, but only after the tes contructor has been executed. So it seems that 'x' and 'y' are printed at different serial speeds, which explains why you don't see the 'y'.

It might also confuse your terminal program, which might explain your other problem. But I'm not sure about that - changing the place where you initialize the objects should not change the behaviour. On the other hand, all the changes you describe change the timing when stuff gets initialized.

25 Feb 2011

Thanks Hendrik, that makes perfect sense about the 'y'. It seems as though the putc('x') is not getting called as an interrupt is occurring before the code is run. This confuses me as even at 115200, the mbed should have 1000 cycles between incoming bits and a full 8000+ between incoming bytes?!

25 Feb 2011

I have made slight changes to my code so it now reads:

#include "mbed.h"

class tes{
    public:
        
        Serial* p;
        Serial* gp;
        
        tes(Serial* pc, Serial* gps){
            p = pc;
            gp = gps;
            wait(1);
            //p->putc('y');
            
            gps->attach(this, &tes::rx, Serial::RxIrq);
        }
    
        void rx() {
            DigitalOut l(LED2);
            l = 1;
            p->putc(gp->getc());
        
        }
};

Serial gps(p28,p27);
Serial pc(USBTX,USBRX);

int main() {  


    gps.baud (19200);
    pc.baud(115200);
    
    pc.printf("test\r\n");
    
    tes t(&pc, &gps);
      
}

I now note that the interrupt is called, but only for the first 16 bytes of data received. The other ways of instantiating t continue to work.

25 Feb 2011
  • It even works if I now change the line in void rx() to p->putc('x'), however if gps data is printed, it is limited to the first 16 chars.