6 years, 8 months ago.

serial communication

hello, when i am trying to send string with more than 3 character it cant print on serial monitor. it only print always when we use upto 3 character. why?? give me solution for long string. here is my program:

  1. include "mbed.h"

Serial pc(USBTX, USBRX, 9600);

int main() { char buffer[128];

while(1) { pc.gets(buffer, 7); if(strcmp(buffer,"somesh")==0) { pc.printf("Hii '%s'\nWELCOME TO ARM Mbed.\n\n", buffer);

} } }

1 Answer

6 years, 7 months ago.

It's very unclear what you're asking. The application also runs fine on my board, whenever you type 'somesh' on serial it will print the message fine (on FRDM-K64F board).

If you want variable length names then you'll need to use getc() instead. E.g.:

#include "mbed.h"

Serial pc(USBTX, USBRX, 9600);

int main()
{
    char buffer[128] = { 0 };
    uint8_t buffer_ix = 0;

    while (1)
    {
        pc.printf("Type your name: ");

        char c;
        while ((c = pc.getc()) != '\r') {
            buffer[buffer_ix++] = c;

            pc.putc(c); // echo back to the user
        }

        pc.printf("\nHi '%s'\n\n", buffer);

        // reset buffer
        memset(buffer, 0, sizeof(buffer));
        buffer_ix = 0;
    }
}