Arm Mbed OS support forum

Serial object undefined or already defined

I am trying to define ‘pc’ as an object in the Serial class. --> Serial pc(USBTX, USBRX);
this works fine when everything, all functions, are in the main.cpp. but when I broke it into header and source files. I get one of two problems. When I try to define ‘pc’ in the header file, the object is undefined in the source file. And if I define ‘pc’ in the source file it says the object is already defined.

The code I am providing is using the second problem where ‘Serial pc(USBTX, USBRX);’ is already defined (even though it isn’t in any part anywhere.
I believe it might be a problem with the header and source files not working together as none of the variables defined in the header can be reached in the source file.

// Begin of main.cpp

#include "mbed.h"
#include "recWithEndMarker.h"
#include <string>
#include <InterruptIn.h>

using namespace std;

// Used Functions
//string recWithEndMarker();
//void   setUp();
//void   controls();
//void   laserDiodeCurrent();
//void   toggleLD();
//void   eStop();
//void   tuningLD();
//void   tuningTgain();

// Used Variables


int main()
{
    printf("\r\nWelcome to the Diode-Pumped Solid-State Laser\r\nBy Marin Iliev and Chip Durfee\r\n\n<STM Ready>\r\n\n");
    ESTOP.rise(&eStop);
    //ESTOP.fall(&eStop);
      
    while(1){
        setUp();
        controls();
    }    
    
}
// Emergency stop interrupt function. Pin D7 = PF_0
void eStop(){
    LD = 0;
   printf("Emergency Stop detected, Laser Diode disabled.\r\n");
   printf("-----------------------------------------\r\n"); 
   printf("-----------------------------------------\r\n");
}

// recWithEndMarker.cpp function that gets input from user serial communication

#include "recWithEndMarker.h"
#include "mbed.h"

Serial pc(USBTX,USBRX);

// Interact with serial monitor
string recWithEndMarker(){
    string receivedChars;
    char c;
    
    while(c != '\r'){
        c = pc.putc(pc.getc());
        receivedChars.push_back(c);
    }

    printf("\r\n");
    
    return receivedChars;
}

// recWithEndMarker.h

#ifndef RECWITHENDMARKER_H
#define RECWITHENDMARKER_H

#include "mbed.h"

class serialInput{
    public:
        string recWithEndMarker();
        string receivedChars;
        char c;
    private:
    
};

#endif

Hello Vincent,

  • It’s a good practice to start a class name with an upper case character (like SerialInput).

  • If you would like the pc variable to belong to the SerialInput class then declare it inside the class. For example:

#include "mbed.h"

...

class SerialInput {
    public:
        SerialInput(PinName txPin, PinName rxPin);  // constructor
        string recWithEndMarker();
    private:
        Serial pc; 
        string receivedChars;
        char c;
};

And let the constructor initialize it. For example:

SerialInput::SerialInput(PinName txPin, PinName rxPin) : pc(txPin, rxPin) {}

I believe it might be a problem with the header and source files not working together as none of the variables defined in the header can be reached in the source file.

  • When implementing the recWithEndMarker member function tell the compiler that it belongs to the SerialInput class by prepending SerialInput::. Otherwise (as in your code snippet above) it would be an ordinary function with no access to the SerialInput class variables :
string SerialInput::recWithEndMarker(){
    ...
    c = pc.putc(pc.getc());
    ...
}
  • Then create an object of the SerialInput class in the main.cpp and use it. For example:
...
#include "recWithEndMarker.h"
...
SerialInput serialInput(USBTX,USBRX);
...
int main()
{
    ...
    serialInput.recWithEndMarker();
    ...
}

Best regards, Zoltan

1 Like