10 years, 1 month ago.

Serial.attach() error with assigning Memberfunction

Hi guys i want to write a class for a barcodescanner and want to use the interrupt to read the bar code and step through a statemachine. But I only get error when i try to assign the function read() of my Scanner class.

include the mbed library with this snippet

class Scanner{
    public:
        //constructor
        Scanner(PinName tx, PinName rx);
        
        //Peripheral
        Serial serial;    
        
        //Functions
        void read();
        void enable();
        void disable();
        
        //Variables
        string answer;
        int tTimeout;
};

Scanner::Scanner(PinName tx, PinName rx):serial(tx, rx){
    this->tTimeout = 2000;
    
    this->serial.attach(&this->read); 
/* Error: A pointer to a bound function may only be used to call the function in "scanner.cpp", Line: 8, Col: 32 */

}

void Scanner::read(){
    //some function    
}

In the handbook there is just an example to assign global functions. I think I have to use the overloaded function:

void attach ( T * tptr, void(T::*)(void) mptr, IrqType type = RxIrq )

But even if i try to use attach with this as tptr and this->read as mptr it does not work so how do i assign the memberfunction and the object itself?

Could you give a good example?

Thank you for your attention

1 Answer

10 years, 1 month ago.

What should work is:

this->serial.attach(this, &Scanner::read);  //(The initial this-> isn't required)

Accepted Answer