8 years, 3 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, 3 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.

Accepted Answer

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 CHANG rozen 25 Jan 2016

What is your code, what data do you get?

Just saying that it doesn't work doesn't give us any information to go on.

I assume that in the link you gave when they say something is 16-byte they mean 16 bit.

posted by Andy A 25 Jan 2016
8 years, 3 months ago.

If you are unfamiliar with C/C++ go to http://www.cplusplus.com/reference/cstdio/gets/ , or google C++ gets

thanks!

posted by CHANG rozen 25 Jan 2016