Send & wait for uart answer


I am trying to implement a rpc service that sends data to an uart and then wait for the response but I have no idea how to “wait” without blocking the event loop.

Here is a basic example of what I am looking for:

I tried mgos_msleep but it does not work, I am not sure where to look for the answer, any help would be greatly appreciated :slight_smile:


You have a few options. First, you wouldn’t go into a loop waiting for data from the UART. You would have a big loop for the entire app. Each time through you would check the see if data is available and process it. In the Arduino world, the textbook example is called “blink without delay” which is the same concept. Try to google that.

Another option is to make a separate task to process incoming data. You can use FreeRTOS for this. I’ll post an example in a separate thread of how to use FreeRTOS.

1 Like

thanks for your anwser, I am used to work with event loops but in this case I am using http as rpc mechanism for mongooseos and I need to send back the answer, that’s where i am. I tried keeping a pointer on the struct mg_rpc_request_info * structure to answer in the uart read callback but it got me a core panic :x

I searched and looked at “blink without delay” but I fail to see how it help me here :frowning:


nevermind I have it running, I got the core panic because I responded twice to the same mg_rpc_request_info.

What I am doing is: send the request & save the mg_rpc_request_info * associated with the request I sent and in the read handler for the uart I respond to the query when I get the result.

So far it looks like it is working.