3 years, 2 months ago.

API Doumentation (for serial?)

In the handbook for Serial https://developer.mbed.org/handbook/Serial

it seems there are some functions lacking in the API documentation. I don't see putc for example.

In concrete I would like to know what size of data can I send in one call of putc. 2 bytes? perhaps 4 bytes?

In general, it seems sometimes the documentation is not updated?

1 Answer

3 years, 2 months ago.

The problem is that these are inherited from another class, the Stream class. Here is its header file: https://github.com/ARMmbed/mbed-os/blob/master/platform/Stream.h

That said, this also is not directly clear. In general it is easier here to just look at the standard C++ functions which this is based on: http://www.cplusplus.com/reference/cstdio/putc/. Now putc is a bad example since that one requires a FILE object in the standard C++ function. But putc stands for put-character. So it sends a single character of 8-bit.

Thank you. Any recommendation if I want to send an array of shorts?(2 bytes each element)

posted by Cristian Fuentes 29 Apr 2017

The Async API might be an option if your target supports it, so that is the 'write' function. Otherwise option 1:

for (int i = 0; i<lenght; i++) {
  serial.putc(shortarray[i] & 0xFF);
  serial.putc(shortarray[i] >> 8);

Or what you can also do:

char *chararray = (char*)shortarray; // Since an array is just a pointer to the start, we are allowed to make it any type, just take into account the length is now twice as long
for (int i = 0; i<length * 2; i++) {

I didn't check if either compiles, so might be typos, but both options should work. Generally for this case I would just use the first option.

posted by Erik - 29 Apr 2017

Thanks. Actually what I meant was that instead of using putc maybe I can use another function (write??) to send the whole array at once?

posted by Cristian Fuentes 02 May 2017

Write should be able to do it yes, if your MCU supports it. (STM ones do I believe). In that case you should just like the second example cast it to a char array. That is the only alternative option to putc that I am aware of.

posted by Erik - 02 May 2017