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.
7 years, 6 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
7 years, 6 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 29 Apr 2017The 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++) { serial.putc(chararray[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 29 Apr 2017