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.
9 years, 7 months ago.
.gets function question
I am using the .gets function in order to access strings from a raspberry pi
I understand that if I have a 3 character string I can use .gets() in order to read these values using
Serial transmission
Serial device(p28, p27); // tx, rx Serial pc(USBTX, USBRX); char value[3]; device.gets(value,3); pc.printf("value: %s \r\n", value);
This will give me a string of 3 characters.
I want to be able to change the device.gets(value,3) to read characters until the \n from the raspberry pi appears. How can I do this if I change from a string from 3 characters to 4.
1 Answer
9 years, 7 months ago.
you can use scanf function or write a loop that read characters from serial and put them in an array of char until the '\n' is readed.
something could be :
char value[100]; int index=0; char ch; do { if (device.readable()) // if there is an character to read from the device { ch = device.getc(); // read it if (index<100) // just to avoid buffer overflow value[index++]=ch; // put it into the value array and increment the index } } while (ch!='\n'); // loop until the '\n' character value[index]='\x0'; // add un 0 to end the c string pc.printf(value);