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, 1 month ago.
I don't understand how to use the function Serial.write()
I try to write a program of serial but it can't be compiled because of a error(Error: No instance of overloaded function "mbed::Serial::write" matches the argument list in "main.cpp", Line: 13, Col: 13). I don't understand how to use the function Serial.write() and I can't find any example.
This is my program:
- include "mbed.h"
Serial sv(PA_9, PA_10, 115200);
int main() {
unsigned char i = 0; unsigned char tab[5] = {0xFF,0x55,0xAA,0x33,0x23};
while(1) { i = (i + 1)%5; while(sv.writeable()==0); sv.write(tab[i],8); wait_us(3); } }
Thank you for your help!
1 Answer
7 years, 1 month ago.
https://os.mbed.com/docs/v5.6/reference/serial.html
So, the function signature of write is write(const uint8_t *buffer, int length)
.
Here, with tab[i]
, you are passing in a single character, not a pointer to a character, as the function requires. In C++, an array of characters can be cast to char*, as the pointer will contain the address of the first element in the array of characters.
http://www.cplusplus.com/doc/tutorial/pointers/ Look for the section "pointers and arrays" to understand more about the above.
So, if you wanted to write the whole tab buffer, the syntax would be:
sv.write(tab,sizeof(tab));
If you want to write a single characher you can use putc:
sv.putc(tab[i])