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.
12 years, 6 months ago.
Serial Communication
Hello,
I've got a problem regarding Serial Communication between a Computer and the board. It was kinda easy, to get single Character inputs with the pc.getc(); command, but i couldnt figure out yet how to get full Strings, like a full Command, that can be put into a String for comparing.
I tried the fgets("%s",string); function, but it didnt work out yet, to get a row of characters. Has anybody an idea how an multi character command could be read and Compared?
both pc.fgets and pc.scanf did not work.
thinking of
pc.fgets(entry, 20);
.....
if(string== "xy")
.....
kind regards
1 Answer
12 years, 6 months ago.
Hi Timo to get a string from the serial you should do:
Serial pc(USBTX, USBRX);
int main() {
char c;
char buffer[128];
pc.gets(buffer, 4);
pc.printf("I got '%s'\n", buffer);
}
To compare the string received with your command you can´t use ==, use strcmp instead like this
if(strcmp(buffer,"on")==0) {
//do something
}
I hope this will help you
Greetings