<?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>Unable to retrieve saved modem response - nrf52840</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/59301/unable-to-retrieve-saved-modem-response---nrf52840</link><description>Hi, 
 So I am currently trying to connect my nrf52840 DK to aws cloud using an LTE modem. My aim is to be able to store responses from the modem and parse them appropriately to use later in a finite state machine to connect/Pub/Sub AWS IoT services. I</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 30 Mar 2020 13:36:39 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/59301/unable-to-retrieve-saved-modem-response---nrf52840" /><item><title>RE: Unable to retrieve saved modem response - nrf52840</title><link>https://devzone.nordicsemi.com/thread/242451?ContentTypeID=1</link><pubDate>Mon, 30 Mar 2020 13:36:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4e62a2a9-3e1d-497c-beab-55a64724578c</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;I have provided as a simple code snippet that illustrates how this can be done, you should modify it according to your implementation, but the approach should be same.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void get_response(char *ptr, int len){
    //Remember to check if i is bigger than len - 1
    //swap the for loop with the do-while loop
    int i = 0; 
    for(i; i &amp;lt; 10; i ++){
        ptr[i] = &amp;#39;a&amp;#39;;
    }
    ptr[i++] = NULL;

}

#define STRING_SIZE 100
int main(void)
{
    //Initialize NRF_LOG
    .
    .
    char string[STRING_SIZE];
    get_response(string, sizeof(string));
    NRF_LOG_INFO(&amp;quot;string: %s&amp;quot;, string);
    .
    .
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unable to retrieve saved modem response - nrf52840</title><link>https://devzone.nordicsemi.com/thread/241127?ContentTypeID=1</link><pubDate>Mon, 23 Mar 2020 12:45:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:83ce5c24-6346-4ceb-81af-bd04d5402452</guid><dc:creator>Abz</dc:creator><description>&lt;p&gt;Thanks for this detailed response Simon.&lt;/p&gt;
&lt;p&gt;Can you please just clarify how to set&amp;nbsp;&lt;em&gt;res_ptr&lt;/em&gt; to some memory address without using malloc. I have limited programming experience so any guidance would be greatly appreciated.&lt;/p&gt;
&lt;p&gt;I have made the&amp;nbsp;changes you advise&amp;nbsp;to the function get_response but the printf still shows a NULL output.&lt;/p&gt;
&lt;p&gt;In the debug terminal I am able to see the correct response from the modem but it does not store the response into the memory address given to get_response function.&lt;/p&gt;
&lt;p&gt;If I am missing anything please do let me know.&lt;/p&gt;
&lt;p&gt;CODE:&lt;/p&gt;
&lt;p&gt;void get_response(unsigned char *response){&lt;/p&gt;
&lt;p&gt;ret_code_t err_code;&lt;/p&gt;
&lt;p&gt;uint8_t * temp_ptr = NULL; // init temp ptr&lt;br /&gt; &lt;br /&gt; temp_ptr = response; // set temp ptr to address where serial data should be stored&lt;/p&gt;
&lt;p&gt;//temp_ptr = (uint8_t *)malloc(sizeof(uint8_t)*2400);&lt;br /&gt; &lt;br /&gt; if(temp_ptr=NULL){printf(&amp;quot;failed to assign memory&amp;quot;);}&lt;/p&gt;
&lt;p&gt;uint8_t c;&lt;br /&gt; &lt;br /&gt;do{&lt;br /&gt; nrf_serial_read(&amp;amp;serial_uart, &amp;amp;c, sizeof(c), NULL, 0);&lt;br /&gt; nrf_serial_write(&amp;amp;serial_uart, &amp;amp;c, sizeof(c), NULL, 0);&lt;/p&gt;
&lt;p&gt;if(c==&amp;#39;K&amp;#39; || c==&amp;#39;R&amp;#39; ){ &lt;br /&gt; nrf_delay_ms(10);&lt;br /&gt; *temp_ptr=c; &lt;br /&gt; printf(&amp;quot;%c&amp;quot;,*temp_ptr);&lt;br /&gt; temp_ptr++;&lt;br /&gt; nrf_serial_read(&amp;amp;serial_uart, &amp;amp;c, sizeof(c), NULL, 0);&lt;br /&gt; nrf_serial_write(&amp;amp;serial_uart, &amp;amp;c, sizeof(c), NULL, 0);&lt;br /&gt; if(c==&amp;#39;\r&amp;#39;){ &lt;br /&gt; *temp_ptr=c; &lt;br /&gt; break;&lt;br /&gt; } &lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;*temp_ptr=c; &lt;br /&gt; printf(&amp;quot;%c&amp;quot;,*temp_ptr);&lt;br /&gt; temp_ptr++;&lt;br /&gt; nrf_delay_ms(15);&lt;br /&gt; bsp_board_led_invert(0);&lt;/p&gt;
&lt;p&gt;} while(1);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;/// main ////&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;int main(void)&lt;br /&gt;{&lt;br /&gt; ret_code_t err_code;&lt;/p&gt;
&lt;p&gt;// Initialize.&lt;br /&gt; err_code=nrf_drv_clock_init();&lt;br /&gt; APP_ERROR_CHECK(err_code);&lt;/p&gt;
&lt;p&gt;nrf_drv_clock_lfclk_request(NULL);&lt;/p&gt;
&lt;p&gt;err_code = app_timer_init();&lt;br /&gt; APP_ERROR_CHECK(err_code);&lt;/p&gt;
&lt;p&gt;bsp_board_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS);&lt;/p&gt;
&lt;p&gt;nrf_serial_init(&amp;amp;serial_uart, &amp;amp;m_uart0_drv_config, &amp;amp;serial_config);&lt;/p&gt;
&lt;p&gt;printf(&amp;quot;\nSTART main\n&amp;quot;);&lt;/p&gt;
&lt;p&gt;static char tx_message[] = &amp;quot;at+cops?\r&amp;quot;;&lt;br /&gt; &lt;br /&gt; &lt;br /&gt; err_code = nrf_serial_write(&amp;amp;serial_uart,&lt;br /&gt; tx_message,&lt;br /&gt; strlen(tx_message),&lt;br /&gt; NULL,&lt;br /&gt; NRF_SERIAL_MAX_TIMEOUT);&lt;/p&gt;
&lt;p&gt;APP_ERROR_CHECK(err_code);&lt;/p&gt;
&lt;p&gt;nrf_delay_ms(100);&lt;/p&gt;
&lt;p&gt;init_modem();&lt;/p&gt;
&lt;p&gt;SendAt_cmd(tx_message);&lt;/p&gt;
&lt;p&gt;uint8_t res_ptr ,i=0,t=0,c;&lt;/p&gt;
&lt;p&gt;get_response(&amp;amp;res_ptr);&lt;br /&gt; &lt;br /&gt; printf(&amp;quot;\nptr = %s\n&amp;quot;,res_ptr);&lt;/p&gt;
&lt;p&gt;&lt;br /&gt; printf(&amp;quot;\nEND&amp;quot;);&lt;br /&gt;}&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unable to retrieve saved modem response - nrf52840</title><link>https://devzone.nordicsemi.com/thread/241120?ContentTypeID=1</link><pubDate>Mon, 23 Mar 2020 12:29:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:139d392a-6003-4d48-badc-dabc685e9fc4</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;I took a quick look at the code in &lt;em&gt;get_response(unsigned char *response)&lt;/em&gt;, and it seems like you are increasing&amp;nbsp;the address (which response is pointing at) of one byte after each iteration. At the end of the function call, the pointer &lt;em&gt;response&amp;nbsp;&lt;/em&gt;will point to the end of the stored string.&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;res_ptr&lt;/strong&gt; is set to NULL initially, then inside the function &lt;em&gt;get_response()&amp;nbsp;&lt;/em&gt;the local variable (see &lt;a href="https://stackoverflow.com/questions/23667497/update-int-variable-in-c-inside-a-function"&gt;here&lt;/a&gt;)&amp;nbsp;&lt;em&gt;response&lt;/em&gt; is changed (not &lt;em&gt;res_ptr&lt;/em&gt; in the main() scope), and when trying to access it in &lt;em&gt;main()&amp;nbsp;&lt;/em&gt;after the function call it will still be NULL&lt;/li&gt;
&lt;li&gt;Due to the explanation above, &lt;strong&gt;ptr&lt;/strong&gt; will point to the end of the stored string, and you will just print the values stored after the string when calling printf&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is how I would go about it:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Declare and set the pointer&amp;nbsp;&lt;em&gt;res_ptr&lt;/em&gt; (to an actual memory address) before running&amp;nbsp;&lt;em&gt;get_response.&amp;nbsp;&lt;/em&gt;You might consider&amp;nbsp;not using malloc, due to the reasons in &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/44062/when-should-i-use-nrf_calloc-malloc-instead-static-var"&gt;this ticket&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Pass the pointer as an argument to &lt;em&gt;get_response()&amp;nbsp;&lt;/em&gt; as you&amp;#39;ve done&lt;/li&gt;
&lt;li&gt;Create a local copy &lt;em&gt;temp_ptr&lt;/em&gt; of the pointer in&amp;nbsp;&lt;em&gt;get_response()&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Store the incoming serial data at the address pointed to by temp_ptr and increase it after each iteration as you&amp;#39;ve done&lt;/li&gt;
&lt;li&gt;In the end, don&amp;#39;t return anything, since the serial data will be stored at the address which&amp;nbsp;&lt;em&gt;res_ptr&amp;nbsp;&lt;/em&gt;is pointing at&lt;/li&gt;
&lt;li&gt;Run&amp;nbsp;&lt;em&gt;printf(&amp;quot;\n%s&amp;quot;,res_ptr);&lt;/em&gt; in main(), and it should work&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>