<?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>How to empty RX buffer on nrf52840 UARTE0</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/123855/how-to-empty-rx-buffer-on-nrf52840-uarte0</link><description>Hi 
 I want to read in a sequenze of character over UARTE0 pheripheral on the NRF52840. I use the function getchar() but the first two characters I get is always the last two characters received which is always \n and \r. I solve this by running getchar</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 27 Aug 2025 18:34:18 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/123855/how-to-empty-rx-buffer-on-nrf52840-uarte0" /><item><title>RE: How to empty RX buffer on nrf52840 UARTE0</title><link>https://devzone.nordicsemi.com/thread/546974?ContentTypeID=1</link><pubDate>Wed, 27 Aug 2025 18:34:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:976c380a-e4d3-4b7c-9165-ca442a498f6a</guid><dc:creator>Oystein</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;Thansk a lot, the information about scanf, I was not aware of all the smart features. I will implemt as needed.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Vidar wrote:&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Because of this I woud only expect \n to remain in your FIFO&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;yes, when I read the FIFO buffer (using the RXD:PTR pointer there is only one characters \n, but there is also a static bufffer&lt;pre class="ui-code" data-mode="text"&gt;static app_fifo_t                  m_rx_fifo;                               /**&amp;lt; RX FIFO buffer for storing data received on the UART until the application fetches them using app_uart_get(). */&lt;/pre&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I guess this static buffer contains the last two characters. I did not investiage it anymore. I just run getchar 2 times to solve it.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;//Run getchar() 2 times to empty the UARTE0 RX buffer. 
    getchar();
    getchar();&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;kr&lt;/p&gt;
&lt;p&gt;&amp;Oslash;ystein&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to empty RX buffer on nrf52840 UARTE0</title><link>https://devzone.nordicsemi.com/thread/546421?ContentTypeID=1</link><pubDate>Fri, 22 Aug 2025 08:50:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8ddd211c-d472-4df4-a8ed-780c594dcfc2</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Thanks for the additional information. It sounds like our&lt;a href="https://docs.nordicsemi.com/bundle/sdk_nrf5_v17.1.0/page/lib_cli.html"&gt; CLI library&lt;/a&gt; might be more suitable for what you are trying do. Anyway, scanf will call __getchar() -&amp;gt; app_uart_get() in a loop until it has read the first termination character from the UART driver’s FIFO buffer before exiting.&amp;nbsp; Because of this I woud only expect \n to remain in your FIFO. It may also be worth&amp;nbsp;adding that not all terminals use the same line endings by default.&lt;/p&gt;
[quote user="Oystein"]This times I know its two bytes, but maybe next time in some other program it may be 10 bytes. I would like to run a routine that check how many unread bytes there is and clear all of them?[/quote]
&lt;p&gt;The app uart library which is used by the stdio functions does not expose any functions to peek into the FIFO buffer unfortunately.&lt;/p&gt;
[quote user="Oystein"]smarter low level UARTE0 register I can set to clear RX buffer[/quote]
&lt;p&gt;It seems like you&amp;nbsp;have scanf&amp;nbsp;ignore a leading newline by inserting a whitespace before the format specifier. So&amp;nbsp;scanf(&amp;quot; %s&amp;quot;, string1); instead of&amp;nbsp;scanf(&amp;quot;%s&amp;quot;, string1);&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1755852352180v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://cplusplus.com/reference/cstdio/scanf/"&gt;https://cplusplus.com/reference/cstdio/scanf/&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Oystein"]program it may be 10 bytes.[/quote]
&lt;p&gt;I would consider setting a max width for scanf to not allow the string buffer to potentially overflow.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1755852444003v2.png" alt=" " /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to empty RX buffer on nrf52840 UARTE0</title><link>https://devzone.nordicsemi.com/thread/546377?ContentTypeID=1</link><pubDate>Thu, 21 Aug 2025 21:03:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c2e69f2c-79c0-49be-a993-65387784836e</guid><dc:creator>Oystein</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;        memset(string1, 0, sizeof(string1));
        memset(string2, 0, sizeof(string2));  
        scanf(&amp;quot;%s&amp;quot;, string1);
        i=0;

        while( (i &amp;lt; 256) &amp;amp;&amp;amp; (string1[i]!=&amp;#39;(&amp;#39;) &amp;amp;&amp;amp; (string1[i]!=0) ) {
          string2[i] = string1[i];
          i=i+1;
        }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I use&amp;nbsp;nRF5_SDK_17.1.0_ddde560, use the UART pheripheral example/PCA10056 as a start base for my project and develped it from there.&lt;/p&gt;
&lt;p&gt;I use scanf function to read in te command string which always is appended with &lt;strong&gt;\n\r&lt;/strong&gt; by my UART serial terminal program (REALTERM)&lt;/p&gt;
&lt;p&gt;I am not completly sure about this but as I understand it, SCANF function reads in all characters until a &lt;strong&gt;whitepspace&lt;/strong&gt;, or &lt;strong&gt;\r&lt;/strong&gt;, or &lt;strong&gt;\n&amp;nbsp;&lt;/strong&gt;is sent. So the last two characters sent&amp;nbsp;&lt;strong&gt;\n\r&amp;nbsp;&lt;/strong&gt;is never read out from the RX buffer. So thats why always the first two characters I read in when I run&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;else if ( strcmp(string2, &amp;quot;send_binary_lpc_code&amp;quot;) == 0 )&amp;nbsp;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;are always &lt;strong&gt;\r\n&lt;/strong&gt;, and I would like to clear the RX buffer before using getchar(). I have solved it for this use case bu just running getchar two times first, but is there some smarter low level UARTE0 register I can set to clear RX buffer. This times I know its two bytes, but maybe next time in some other program it may be 10 bytes. I would like to run a routine that check how many unread bytes there is and clear all of them?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to empty RX buffer on nrf52840 UARTE0</title><link>https://devzone.nordicsemi.com/thread/546328?ContentTypeID=1</link><pubDate>Thu, 21 Aug 2025 11:12:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dd522f02-d75f-44e3-874c-3eb152a5fbcd</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Would it be an option to just append \n\r to&amp;nbsp;every command?&lt;/p&gt;
&lt;p&gt;E.g.,&lt;/p&gt;
&lt;p&gt;&lt;span&gt;else if ( strcmp(string2, &amp;quot;send_binary_lpc_code&lt;strong&gt;\r\n&lt;/strong&gt;&amp;quot;) == 0 ) {&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I’m not aware of any examples using getchar from stdio in our SDKs, and the actual implementation will also depend on which SDK and toolchain you are using.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>