10 years, 8 months ago.

Use of Serial Question

Hi, i am doing a new library that defines a Serial Object as a member of it, but i can make to work when i create more than one instance of my new class in the main. When i load the program to the mbed its gone wild! all the leds blinking.

My question is: to use a Serial Object in a new Class, what functions do i have to implement. The only code i have is in the constructor

MyClass::MyClass(PinName tx, PinName rx) : pc(tx,rx) {

}

Greetings. I hope someone can help me

2 Answers

10 years, 8 months ago.

I assume what you mean by mbed going wild is leds flashing in the error pattern? A terminal program on your PC connected to the mbed should get a message regarding the nature of the error.

What you show is enough to implement. But which arguments do you use in the instantiation of the second object?

Accepted Answer
Ney Palma
poster
10 years, 8 months ago.

Hi Erik, thanks for the answer, the arguments are this in the first and second instantiation on main

int main() {
    MyClass s1(p13,p14,115200);
    MyClass s2(p27,p28,115200);

  ..... rest of the code
}

If i only instantiate one object of my class the mbed works fine, but when i instantiate the two objects the mbed is showing the led error pattern

My complete code of the constructor is:

MyClass::MyClass(PinName tx, PinName rx, long baudRate) : pc(tx,rx) {
  pc.baud(baudRate);
}

And i have a function to send and receive data:

void MyClass::sendData(char * data) {
    if(pc.writeable()) {
        pc.printf("%s\n",data);
    }
}

char * MyClass::receiveData() {  
    char data[256];  
    if(pc.readable()) {
        pc.gets(data,256);
    }
    
    return data;
}

and some functions that use printf method of the serial object declared (pc). The serial object is declared in the protected zone of the declaration of the Class like this:

class MyClass{
    public:
        MyClass(PinName tx, PinName rx, long baudRate=115200); //declaration of the constructor
        ...... other functions declaration
    protected:
        Serial pc;
        ...... other objects declaration

So what other functions for the serial object i must declare to make it work. I see in some code something about and attach function?

Greetings, and thanks for the help you can give me

As I said, connect it to your terminal program, then you get the error message that causes the leds to flash. In this case it will be: Pinmap not found!.

It is p28, p27, not the other way around as you have.

posted by Erik - 11 Aug 2013

Hi Erik, you´re right! thats the error is showing in the terminal program. I mismatch the pins! what a fool i feel. thanks for your time and your help. Is neccesary to implement a new attach function in my new class like i see in other codes?

Greetings

posted by Ney Palma 11 Aug 2013

Unless you need the attach function to do something specifically, no.

posted by Erik - 11 Aug 2013

Thanks for everything, one last question, in which cases i need to define the new attach function in my new class

Greetings

posted by Ney Palma 11 Aug 2013

When you want the user to be able to have a function called at a certain moment (generally when your class has an interrupt).

Random example, you got a serial sensor which sends 4 bytes, which form together one 32-bit sensor value. Then you can attach in the class an interrupt to the serial port. It looks how many bytes are received, and when four bytes are received it stitches them together and calls the attached user function.

posted by Erik - 11 Aug 2013