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, 10 months ago.
what can i read 32byte form Serial
Hi!i want read 32 bytes from serial,but i cant work... Do I use getc() or gets(),and what is gets(x,?) ?what is this number,Im not understand...
2 Answers
8 years, 10 months ago.
Generally you'd want something like:
char buffer[32]; for (int i = 0; i< 32; i++) { buffer[i] = serial.getc(); }
Keep in mind getc() will block until there is data available so to code above will always expect exactly 32 bytes.
As David said, look at the cplusplus.com site for the full syntax for all the commands together with examples.
Also on most serial ports the receive buffer is 16 bytes or less which means that you will need to read the data while it's arriving, if you wait until it's all been sent before you try to read the data then some of it will be lost.
thanks for your answer,but i still cant recive my dust sensor. this is senor data https://www.dfrobot.com/wiki/index.php?title=PM2.5_laser_dust_sensor_SKU:SEN0177 now i can recive Start Character 1 and Start Character 2,but still doesnt recive pm1.0 how can i do?
posted by 25 Jan 20168 years, 10 months ago.
If you are unfamiliar with C/C++ go to http://www.cplusplus.com/reference/cstdio/gets/ , or google C++ gets