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.
9 years, 2 months ago.
I have a problem with using serial class function. Can any one help me?
Serial sri(USBTX,USBRX); event_callback_t serialEventCb;
int main() { char tx_buffer[255]; printf("Serial_Init(format,baud set)\n"); sri.format(); sri.baud(9600);
printf("\nSerial START...\n"); sri.writeable(); printf("Input char:"); sri.write(tx_buffer, sizeof(tx_buffer)); fgets(tx_buffer,sizeof(tx_buffer),stdin); printf("tx_buffer=%s\n",tx_buffer); }
I would like to create a string read/write program. But compiler genreate the error that "Function mbed::Stream::write (declared at /extras/mbed_abea610beb85/Stream.h:52) is inaccessible "sri.write(tx_buffer, sizeof(tx_buffer));"
How can I solve this problem?
1 Answer
9 years, 2 months ago.
What are you trying to do? It's not clear from your program.
You seem to switch between using stdin / stdout and the serial port. On most mbed boards the usb serial port is stdin/out and so this will work but it's not a good habit to have. If you want the program to use the serial port then tell it to use that port.
Also use <<code>> and <</code>> to make your code readable on the message board.
Serial sri(USBTX,USBRX);
event_callback_t serialEventCb; // this isn't used anywhere
int main()
{
char tx_buffer[255];
printf("Serial_Init(format,baud set)\n");
sri.format();
// This sets things to the default format (8-N-1). Since you haven't changed anything it is already in that mode.
sri.baud(9600);
printf("\nSerial START...\n");
// should be sri.printf
sri.writeable();
// pointless. This returns true if there is space in the buffer to write to the port, it doesn't change any settings.
printf("Input char:");
// again sri.printf
sri.write(tx_buffer, sizeof(tx_buffer));
// write() is part of the async protocol that is only supported on a few boards. Even if you had one of the few boards it
// worked on you haven't set the value of tx_buffer so you will be sending random binary data.
fgets(tx_buffer,sizeof(tx_buffer),stdin); // while it would probably work you should indicate the serial port not stdin e.g.
// sri.gets(tx_buffer,sizeof(tx_buffer));
printf("tx_buffer=%s\n",tx_buffer); // again sri.printf
}