<?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>Reading UART input in CLI command</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/68578/reading-uart-input-in-cli-command</link><description>I have the CLI module over UART enabled, with one command that continuously prints out the readings from a sensor. What I&amp;#39;m trying to do is stopping this continuous printout if the user types CTRL+C. The sample code is below: 
 static void sensor_printout_cmd</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 20 Nov 2020 00:48:09 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/68578/reading-uart-input-in-cli-command" /><item><title>RE: Reading UART input in CLI command</title><link>https://devzone.nordicsemi.com/thread/281010?ContentTypeID=1</link><pubDate>Fri, 20 Nov 2020 00:48:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1c908fd7-0d0b-4e90-affa-e73f93ca4418</guid><dc:creator>Henry_Chou</dc:creator><description>&lt;p&gt;You the uart handle to get the RX data is the best way in your case.&lt;/p&gt;
&lt;p&gt;Like this&lt;/p&gt;
&lt;p&gt;void uart_event_handle(app_uart_evt_t * p_event)&lt;br /&gt;{&lt;br /&gt; static uint8_t data_array[256];&lt;br /&gt; static uint8_t index = 0;&lt;/p&gt;
&lt;p&gt;switch (p_event-&amp;gt;evt_type)&lt;br /&gt; {&lt;br /&gt; /**@snippet [Handling data from UART] */&lt;br /&gt; case APP_UART_DATA_READY:&lt;br /&gt; //UNUSED_VARIABLE(app_uart_get(&amp;amp;data_array[index]));&lt;br /&gt; app_uart_get(&amp;amp;data_array[index]);&lt;br /&gt; index++;&lt;/p&gt;
&lt;p&gt;if ((data_array[index - 1] ==&lt;span&gt;ASCII_FOR_CTRL_C)&lt;/span&gt;|| (index &amp;gt;=255))&lt;br /&gt; {&lt;br /&gt; data_array[index]=0;&lt;br /&gt; index = 0;&lt;br /&gt; memcpy(uart_rx_buff,data_array,sizeof(data_array));&lt;/p&gt;
&lt;p&gt;uart_state.rx_finsh=set;&lt;br /&gt; }&lt;br /&gt; break;&lt;br /&gt; /**@snippet [Handling data from UART] */&lt;br /&gt; case APP_UART_COMMUNICATION_ERROR:&lt;br /&gt; APP_ERROR_HANDLER(p_event-&amp;gt;data.error_communication);&lt;br /&gt; break;&lt;/p&gt;
&lt;p&gt;case APP_UART_FIFO_ERROR:&lt;br /&gt; APP_ERROR_HANDLER(p_event-&amp;gt;data.error_code);&lt;br /&gt; break;&lt;/p&gt;
&lt;p&gt;default:&lt;br /&gt; break;&lt;br /&gt; }&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Your sensor handle code:&lt;/p&gt;
&lt;p&gt;&lt;span&gt;static void sensor_printout_cmd(nrf_cli_t const* p_cli, size_t argc, char** argv)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;while(!uart_state.rx_finsh) /* rx is from UART, check here if byte read is CTRL+C */&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;uint8_t buf[3];&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;read_from_sensor(buf);&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;nrf_cli_fprintf(...);&amp;nbsp;&lt;strong&gt;/* print sensor readings */&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;/*do something if you want to handle&amp;nbsp;&lt;span&gt;data_array[index] */&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Do the rx_finish job with uart_rx_buff here&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>