Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 3 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:
- 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
8 years, 2 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;
}
}