<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>The best way to handle time-consuming processes</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/68674/the-best-way-to-handle-time-consuming-processes</link><description>I want to know the best way to handle time-consuming processes. 
 I am using an external module used for AT commands via UART. It is necessary to judge the success or failure of each command based on the response received from the UART. (Depending on</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 24 Nov 2020 10:13:28 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/68674/the-best-way-to-handle-time-consuming-processes" /><item><title>RE: The best way to handle time-consuming processes</title><link>https://devzone.nordicsemi.com/thread/281487?ContentTypeID=1</link><pubDate>Tue, 24 Nov 2020 10:13:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dc6d5a9b-a603-496b-b03a-dfd8a1d2a41b</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I understand the issue, and as I see it there are a few approaches to this.&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t know how your current readAndCheckResponse() is implemented, but I guess you have some sort of timeout inside this call, but you want to do this without blocking the rest of the application, right?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I believe you need to split the timeout functionality from the UART RX part. The timeout can be handled using the app_timer.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;For an example of how to use the app_timer, you can check how the ble_app_hrs example uses the app_timer to trigger (simulated) battery level measurements.&lt;/p&gt;
&lt;p&gt;Let us take this command as an example:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    sendCommand(&amp;quot;AT+CONNECT=mqttSever.io,1883&amp;quot;);
    // It may take up to 10 seconds to receive the response of this command
    if (!readAndCheckResponse(&amp;quot;OK&amp;quot;, 10000)) {
        return false;
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;After the call to sendCommand(&amp;quot;...&amp;quot;); you can start an app timer with a 10 second timeout. If this timer times out before you receive the OK, that would be the same as this returning false.&lt;/p&gt;
&lt;p&gt;Then you would need some way to check the incoming UART response. I don&amp;#39;t know if you have an interrupt based way of doing so at this point? I suggest you look into the way that the ble_app_uart example handles UART interrupts, and saves up the received bytes until it either receives a linefeed or the buffer is full before it sends the data over BLE. Perhaps you can use some of the same logic to check for &amp;quot;OK&amp;quot;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Remember that if you receive an &amp;quot;OK&amp;quot; before the timeout, you can stop the app timer, and restart it with new timeout value. It is also possible to pass a custom pointer into your app_timer_start() call that you can use to maintain some sort of state machine.&lt;/p&gt;
&lt;p&gt;If you are interrested, and you may find it useful, please check the attached main.c file that is a modified ble_app_uart main.c file from SDK17.0.0. If you run it and press button 4, you should see some log information from a couple of timeout events wich has passed the &amp;amp;my_context parameter, which is used as a state machine. The timer is started in the BSP_EVENT_KEY_3 event in the bsp_event_handler().&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/8664.main.c"&gt;devzone.nordicsemi.com/.../8664.main.c&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>