10 years, 2 months ago.

Mbed stops sending serial data

I am trying to get the characters entered from the pc console one by one and put them into a character array, the final character is ";" (value 59). When I get this character I want to stop collecting the values and then provide the string to be printed back to the console.

It starts by working but after pressing 4 characters the debug statement in the loop stops outputting to the pc console. I dont know what is happening so I have posted here in the hope someone could tell me what the problem is. I have included my code below,

Thanks, Nick

#include "mbed.h"

//Declerations
Serial pc(USBTX, USBRX);

//Functions
void compileString(Serial s, char* m){
    int counter = 0;
    bool done = false;
    s.printf("Compiling \r\n");
    char c;
    while(!done){
        if(s.readable()){
            c = s.getc();
            pc.printf("Character: %d counter %d \r\n",(int) c, counter);
            if(counter+1 == sizeof(m)){
                *(m+counter) = 59;
                done = true;
            }
            if(c == 59){
                *(m+counter) = c;
                counter++;
                done = true;
            }
            else{
                *(m+counter) = c;
                counter++;
            }
        }
    }
}

//Main Function
int main() {
    pc.baud(9600);
    char msg[100];
    pc.printf("Running \r\n");
    
    while(1){
        if(pc.readable()){
            compileString(pc, msg);
            pc.printf("String: %s \r\n", msg);
        }
    }
}

1 Answer

10 years, 2 months ago.

First of all, in general it is better to only send a pointer to the Serial object to a function, so it doesn't completely copy it. Then you only need to use for example c = s->getc();.

Then the issue (maybe also others), is that sizeof doesn't do what you want it to do: see for example http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/. Although that one isn't the clearest imo. What is comes downto, if you try sizeof(msg) in your main function, it will return 100, since it is an array that consists of 100 bytes. But if you do sizeof(m) in your function, the answer is 4. Since it is a pointer to a char (despite that an array is essentially the same), and since we are using 32-bit ARMs, that has a size of 4-byte.

Now that linked article gives some options, but in the end C++ simply doesn't know the length of an array once you passed it to another function. Either add it as seperate function argument, or make define it somewhere at the top of your code. Last option is to initalize your msg array in a certain way, that you always know when you reach for example '\0', that you have no more space left. (just make sure the others are initialized not to be \0).

That is immediatly also something else you have to take into account, which is what is probably also going wrong and the reason I guess it doesn't print String:, the printf function also doesn't know how long your msg array is. What it does is printing everything, until it reaches the null-terminator ('\0'). If there is none, it won't be a happy printf ;).

Accepted Answer

Thanks for your reply, i have removed "sizeof(m)" and replaced it with the number at this moment in time, I have also added debug messages on every other line and have discovered that the while loop in the compileString function finishes and exits but the program does not return to the main loop.

Do you have any idea on why this is the case?

EDIT

I have solved this issue, it was the pointers for the serial that was causing the issue, i have changed it and it works. Thanks

posted by Nick Oliver 22 Feb 2014