7 years, 2 months ago.

Why I can't find any example using Serial.write() function ?

When sending data by serial, all the examples use putc(). Why no one uses write() function. If I want to send something like: g|counter, where counter is an int variable, can I use write() function ?

I used the printf to send it, i.e: device.printf("g|"%d,counter) but I am not sure if this is a good way.

Thank you for any help.

2 Answers

7 years, 2 months ago.

The write function is a relative new one, which is only supported on part of the devices. It sends it completely asynchronously on the background, similar to BufferedSerial or MODSERIAL user libraries do. Although they are both easier to use since for them you don't need seperate commands. Also in your example, you would first need to create a string with sprintf, and then you can send that string using the write function.

But to answer your question: No one uses it, since vast majority sticks to the 'old' functions that work on every target.

And to answer your next question: Your Disco board should support write, your LPC1768 won't.

Thanks Erik...So for my example, the prinft() is what anyone would use or putc() is preffered, what is the difference between those two ?

posted by Yacine Bougrinat 07 Feb 2017

Printf is the normal one yes. putc() only does a single character instead of a complete sentence. Puts() does a complete sentence like printf, however it cannot do things like %d, only a fixed sentence can be done using puts.

posted by Erik - 08 Feb 2017
7 years, 2 months ago.

Here is a very handy reference that can help with your questions. http://www.cplusplus.com/

No mbed functions are documented there, only standard ones.

posted by Erik - 08 Feb 2017