8 years, 5 months ago.

How to use read() function of RawSerial API ?

Hello. I want to make serial communication(especially receiving) on my DISCOVERY F7 board.

First Sending message, I use Serial.printf() function like this.

<from "main.c">

  1. include "mbed.h" Serial pc(SERIAL_TX, SERIAL_RX); 9600bps void main(){pc.printf("hello serial port!");} </ "main.c"> I succeeded receiving message on my computer.

Next, I've tried receiving message from my computer. I discovered read() function in Serial.h of mbed api, and I thought I should use this for receiving message.I write under code, but of course can't compiling because I'm not able to get how to use this function. What is "Callback", What is "event", What is "char_match" ?

this is read function in Serial.cpp(https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/common/SerialBase.cpp)

read(uint8_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match)

<try_receiving.c> pc.readable(); char buffer[40];char = uint8_t void my_callback(){pc.printf("Callback!");} pc.read(buffer,40,my_callback,??,??); while(1){ if(buffer == "command"){ this is pseudo-code break;} } pc.printf("you sent command via serial comPort!"); </"try_receiving.c">

On the other hand, I succeeded receiving char using Serial.getc() function. this command is get single char data in Serial buffer and if I wrote get_char_sequence() function using getc() I could attain my goal,but not elegant and I have a question what for "read() function" is?

Please teach me -What for Serial.read() function? -How to use Serial.read() function? -What is the best way to receivie char sequence(like command)?

I'm not good at English but you read this.Thanks!

1 Answer

8 years, 5 months ago.

Short version: You don't use it.

The read function is part of the serial_async protocol that is only supported on silicon labs boards and seems to be included in the documentation purely to confuse people and make things harder for beginners.

What you are doing, a loop using if (readable()) { getc() } is be best way to read data from the serial port if you don't need to do anything else at the same time.

If you need to do something else at the same time then you need to use the receive interrupt to tell you when a new byte has arrived, the interrupt can then store the result in a buffer and signal the rest of your program once the end of line character has been received.

Or alternatively you use MODSERIAL which already does all of this for you.

To answer one of your other questions:

A callback is a common thing in c, mainly use with interrupts - it's the function to call (technically a pointer to the function to call) when a specific situation occurs. The function you give must have the expected return type and parameter list (often void).

Finally in the future when posting code please put <<code>> and <</code>> around the code so that it formats correctly and use the preview button to check it worked before posting.

Accepted Answer

Thanks Andy A ! I make getc() loop now and other hand I am trying attach MODSERIAL to DISCO_F7.

I'll use <<code>>and<</code>>.

finally, Where did you know information that "The read function is part of the serial_async protocol that is only supported on silicon labs boards" ? I want to be able to find it myself...

Thanks a lot Andy!

posted by Hiroaki Ogino 06 Nov 2015

There are two ways of telling:

The easy but not 100% reliable method is that if the function description here https://developer.mbed.org/users/mbed_official/code/mbed/docs/9296ab0bfc11//classmbed_1_1SerialBase.html contains the word asynchronous then it probably isn't available.

The more reliable method is if you look at the cpp file you linked to in your question anything inside an #ifdef DEVICE_SERIAL_ASYNCH / #endif block is not available on most mbeds.

posted by Andy A 06 Nov 2015