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.
6 years, 2 months ago.
I want send my cmd to serial,but i dont know what function i can use?
char tp[]={0x5A,0xA5,0x07,0x82,0x00,0x01,0xCE,0xC2,0xB6,0xC8};
if on arduino ,i can use example serial.write(tp);
but i found mbed is write (const uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE);
please ask,what is this const event_callback_t &callback and int event=SERIAL_EVENT_TX_COMPLETE ? i only need buffer and length.How should i do?
and,Mbed whats different between puts(); and write();?
mbed doc website are too few examples,only have printf()................
1 Answer
6 years, 2 months ago.
Hello Liu,
I know (from my own experience) how annoying it could be when simple things do not work. So below is my humble attempt for explanation:
In addition to it's basic function, the write
function in mbed enables to call an additional function when a specified event happens.
- To tell the
write
function which function you would like it to call, you must pass it a pointer that is pointing to such function (also known as callback function). That's the meaning of the&callback
argument (address of a function to be called). The function to be called by thewrite
function must meet some criteria, that is, it must be ofevent_callback_t
type. Theconst
beforeevent_callback_t
ensures that thewrite
function is not going to change the&callback
function pointer. That was a brief explanation of:
const event_callback_t &callback
- You must also tell the
write
function when the callback function shall be called, that is, the event on which it shall be called. That's the purpose of theevent
argument. It must be ofint
type (there are various suchint
values predefined by mbed team). The expression=SERIAL_EVENT_TX_COMPLETE
is specifying that the event when all bytes from thebuffer
has been sent to the serial port is used as default value in case when you do not pass anyevent
argument to thewrite
function. That was a simplified explanation of:
int event=SERIAL_EVENT_TX_COMPLETE
However in mbed the write
function isn't defined directly in the Serial
class but rather inherited from the Stream
class (one of the Serial
's predecessors). And because the write
function is declared in Stream
as protected
function, it cannot be called in application software (a program designed by us, users) as a member function of aSerial
object. Instead, we shall use the putc
function (see below how). Nevertheless, I think that the explanation above could be useful anyway.
The main difference between write
and puts
functions:
write
is used to send a specific (known) number of bytes, which is passed as second argument. See here for more details.puts
is used for sending bytes stored in c-style string (an array terminated with null character, which actually is a 0). The number of bytes is not passed to it. Instead, theputs
function checks each byte before sending it. When the byte is a terminating null character ('\0') it stops sending the bytes from the c-style string. The terminating null-character is not sent. Before finishing, it sends one new line character ('\n') as explained here.
Because write
is not available, you can accomplish the task by using the putc
function, for example as follows:
#include <mbed.h> Serial serial(USBTX, USBRX); // default baudrate is 9600 char tp[] = { 0x5A, 0xA5, 0x07, 0x82, 0x00, 0x01, 0xCE, 0xC2, 0xB6, 0xC8 }; int main() { size_t tp_len = (sizeof(tp) / sizeof(*tp)); // calculate the length of tp array (useful for long arrays) for (size_t i = 0; i < tp_len; i++) serial.putc(tp[i]); }
Another option is to define a new function (could be put in your own library) and call it from the main
, for example as follows:
#include <mbed.h> Serial pc(USBTX, USBRX); // default baudrate is 9600 char tp[] = { 0x5A, 0xA5, 0x07, 0x82, 0x00, 0x01, 0xCE, 0xC2, 0xB6, 0xC8 }; void writeToSerial(Serial* serial, char* buf, size_t len) { for (size_t i = 0; i < len; i++) serial->putc(tp[i]); } int main() { size_t tp_len = (sizeof(tp) / sizeof(*tp)); // calculate the length of array writeToSerial(&pc, tp, tp_len); }