<?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>Read connectivity statistics with nrf_modem_at_scanf results in error</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/100263/read-connectivity-statistics-with-nrf_modem_at_scanf-results-in-error</link><description>Hello there, 
 
 I want to monitor the connectivity stats during runtime on the nrf9160. 
 To do so, I start the measurement with: 
 
 And read the stats with: 
 
 The start of measurement works fine, without any error. But when reading the stats it returns</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 09 Jun 2023 06:37:53 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/100263/read-connectivity-statistics-with-nrf_modem_at_scanf-results-in-error" /><item><title>RE: Read connectivity statistics with nrf_modem_at_scanf results in error</title><link>https://devzone.nordicsemi.com/thread/430112?ContentTypeID=1</link><pubDate>Fri, 09 Jun 2023 06:37:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b9a04b33-22f7-4d4d-b691-131313bdffbb</guid><dc:creator>&amp;#216;yvind</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/members/mroeber"&gt;MoritzR&lt;/a&gt;&amp;nbsp;, I found the reason for why it fails with -77. The&amp;nbsp; nrf_modem_at_scanf(cmd, fmt, ...) function needs to pass one % character in cmd, and two in fmt. Also, the &amp;quot;=&amp;quot; should be &amp;quot;:&amp;quot; in&amp;nbsp;&amp;quot;%%XCONNSTAT=%i,%i,%i,%i,%i,%i&amp;quot;. Here is what worked for me:&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void connstat_read(void)
{
	int err, smsrx, smstx, datarx, datatx, pmax, paver = 0;

	err = nrf_modem_at_printf(&amp;quot;AT%%XCONNSTAT=1&amp;quot;);
	if (err &amp;lt; 0) {
		printk(&amp;quot;Failed to get system mode, error: %d\n&amp;quot;, err);
	} else {
		printk(&amp;quot;Result: %d\n&amp;quot;, err);
	}
	err = nrf_modem_at_scanf(&amp;quot;AT%XCONNSTAT?&amp;quot;,&amp;quot;%%XCONNSTAT: %i,%i,%i,%i,%i,%i&amp;quot;,		
		&amp;amp;smstx,
		&amp;amp;smsrx,
		&amp;amp;datatx,
		&amp;amp;datarx,
		&amp;amp;pmax,
		&amp;amp;paver);
	if (err &amp;lt; 0) {
		printk(&amp;quot;Failed to get system mode, error: %d\n&amp;quot;, err);
	} else {
		printk(&amp;quot;Stats: %i,%i,%i,%i,%i,%i\n&amp;quot;,
		smstx,
		smsrx,
		datatx,
		datarx,
		pmax,
		paver);
	}
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This will print out the following:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="powershell"&gt;Result: 0
Stats: 0,0,0,0,0,0&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;br /&gt;Øyvind&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read connectivity statistics with nrf_modem_at_scanf results in error</title><link>https://devzone.nordicsemi.com/thread/430077?ContentTypeID=1</link><pubDate>Thu, 08 Jun 2023 20:51:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3f8add50-7f91-4ad4-b77c-55f848261a4e</guid><dc:creator>&amp;#216;yvind</dc:creator><description>&lt;p&gt;Hi Moritz,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I was able to reproduce your last issue, I&amp;#39;m also seeing&amp;nbsp;&lt;span&gt;error code -77 (NRF_EBADMSG). There is an (outdated) blog post on DevZone which provides a guide on XCONNSTAT. Based on this, and the last comment on this blog post, I was able to print the following:&amp;nbsp;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;2023-06-08T20:38:26.894Z DEBUG modem &amp;lt;&amp;lt; Result: 0
2023-06-08T20:38:26.970Z DEBUG modem &amp;lt;&amp;lt; Connection stats: %XCONNSTAT: 0,0,0,0,0,0
2023-06-08T20:38:26.972Z DEBUG modem &amp;lt;&amp;lt; OK
2023-06-08T20:38:26.974Z DEBUG modem &amp;lt;&amp;lt;  | Uptime: 20 seconds&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I added the following to at_monitor sample for a quick test:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void connstat_read(void)
{
	int err;
	char buf[50];
	err = nrf_modem_at_printf(&amp;quot;AT%%XCONNSTAT=1&amp;quot;);
	if (err != 0) {
		printk(&amp;quot;Failed to get system mode, error: %d\n&amp;quot;, err);
	} else {
		printk(&amp;quot;Result: %d\n&amp;quot;, err);
	}
	err = nrf_modem_at_cmd(response, sizeof(response),&amp;quot;AT%%XCONNSTAT?&amp;quot;);		
	if (err != 0) {
		printk(&amp;quot;Failed to get system mode, error: %d\n&amp;quot;, err);
	} else {
		printk(&amp;quot;Connection stats: %s | Uptime: %d seconds\n&amp;quot;, response, k_uptime_get_32() / 1000);
	}
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Let me know how that works for you.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;From nrfxlib\nrf_modem\include\nrf_modem_at.h&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**
 * @brief Send a formatted AT command to the modem.
 *
 * Supports all format specifiers of printf() as implemented by the selected C library.
 * This function can return a negative value, zero or a positive value.
 *
 * @param fmt Command format.
 * @param ... Format arguments.
 *
 * @retval  0 On &amp;quot;OK&amp;quot; responses.
 * @returns A positive value On &amp;quot;ERROR&amp;quot;, &amp;quot;+CME ERROR&amp;quot;, and &amp;quot;+CMS ERROR&amp;quot; responses.
 *	    The type of error can be distinguished using @c nrf_modem_at_err_type.
 *	    The error value can be retrieved using @c nrf_modem_at_err.
 * @retval -NRF_EPERM The Modem library is not initialized.
 * @retval -NRF_EFAULT @c fmt is @c NULL.
 * @retval -NRF_EINVAL Bad format @c fmt.
 * @retval -NRF_ENOMEM Not enough shared memory for this request.
 * @retval -NRF_ESHUTDOWN If modem was shut down.
 */
int nrf_modem_at_printf(const char *fmt, ...);

/**
 * @brief Send an AT command to the modem and read the formatted response
 *	  into the supplied argument list.
 *
 * Supports all the format specifiers of scanf() as implemented by the selected C library.
 * This function does not support retrieving the modem response beyond reading
 * the formatted response into the argument list.
 *
 * @param cmd AT command.
 * @param fmt Response format.
 * @param ... Variable argument list.
 *
 * @returns The number of arguments matched.
 * @retval -NRF_EPERM The Modem library is not initialized.
 * @retval -NRF_EFAULT @c cmd or @c fmt are @c NULL.
 * @retval -NRF_EBADMSG No arguments were matched.
 * @retval -NRF_ENOMEM Not enough shared memory for this request.
 * @retval -NRF_ESHUTDOWN If the modem was shut down.
 */
int nrf_modem_at_scanf(const char *cmd, const char *fmt, ...);

/**
 * @brief Send a formatted AT command to the modem
 *	  and receive the response into the supplied buffer.
 *
 * @param buf Buffer to receive the response into.
 * @param len Buffer length.
 * @param fmt Command format.
 * @param ... Format arguments.
 *
 * @retval  0 On &amp;quot;OK&amp;quot; responses.
 * @returns A positive value On &amp;quot;ERROR&amp;quot;, &amp;quot;+CME ERROR&amp;quot;, and &amp;quot;+CMS ERROR&amp;quot; responses.
 *	    The type of error can be distinguished using @c nrf_modem_at_err_type.
 *	    The error value can be retrieved using @c nrf_modem_at_err.
 * @retval -NRF_EPERM The Modem library is not initialized.
 * @retval -NRF_EFAULT @c buf or @c fmt are @c NULL.
 * @retval -NRF_EINVAL Bad format @c fmt, or @c len is zero.
 * @retval -NRF_ENOMEM Not enough shared memory for this request.
 * @retval -NRF_E2BIG The response is larger than the supplied buffer @c buf.
 * @retval -NRF_ESHUTDOWN If the modem was shut down.
 */
int nrf_modem_at_cmd(void *buf, size_t len, const char *fmt, ...);&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Kind regards,&lt;br /&gt;Øyvind&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read connectivity statistics with nrf_modem_at_scanf results in error</title><link>https://devzone.nordicsemi.com/thread/429561?ContentTypeID=1</link><pubDate>Tue, 06 Jun 2023 12:56:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8b841a39-3264-4981-b192-dca4ed76e3d9</guid><dc:creator>MoritzR</dc:creator><description>&lt;p&gt;Hello Oyvind,&lt;/p&gt;
&lt;p&gt;yes, CONFIG_NRF_MODEM_LIB=y is included.&lt;/p&gt;
&lt;p&gt;Modem firmware version is 1.3.4.&lt;/p&gt;
&lt;p&gt;I adopted the following from the given example:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;int smsrx, smstx, datarx, datatx, pmax, paver = 0;
	int ret = nrf_modem_at_scanf(&amp;quot;AT%%XCONNSTAT?&amp;quot;,&amp;quot;%%XCONNSTAT=%i,%i,%i,%i,%i,%i&amp;quot;,		
		&amp;amp;smstx,
		&amp;amp;smsrx,
		&amp;amp;datatx,
		&amp;amp;datarx,
		&amp;amp;pmax,
		&amp;amp;paver);
	printk(&amp;quot;Ret: %i\n&amp;quot;,ret);
	printk(&amp;quot;Stats: %i,%i,%i,%i,%i,%i\n&amp;quot;,
		smstx,
		smsrx,
		datatx,
		datarx,
		pmax,
		paver);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And it returns error code -77 (NRF_EBADMSG No arguments were matched) But it should be alright as the example message in the documentation is &amp;quot;&lt;code&gt;&lt;span&gt;%&lt;span class="resultofText"&gt;XCONNSTAT&lt;/span&gt;=2,3,45,60,708,650&lt;/span&gt;&lt;/code&gt;&amp;quot;.&lt;/p&gt;
&lt;p&gt;I must be missing something...&lt;/p&gt;
&lt;p&gt;Thanks for your help&lt;/p&gt;
&lt;p&gt;Kind regards, Moritz&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read connectivity statistics with nrf_modem_at_scanf results in error</title><link>https://devzone.nordicsemi.com/thread/428266?ContentTypeID=1</link><pubDate>Tue, 30 May 2023 18:27:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0b51e168-c54b-48f8-b690-adf73846dc8d</guid><dc:creator>&amp;#216;yvind</dc:creator><description>&lt;p&gt;Hello Maritz,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Have you done as the error states? In you prj.conf have you added CONFIG_NRF_MODEM_LIB?&lt;/p&gt;
&lt;p&gt;Are you using one of our samples as a starting point? Have you looked at our &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.3.0/nrf/samples/nrf9160/at_monitor/README.html#nrf9160-at-monitor"&gt;nRF9160:AT Monitor sample&lt;/a&gt;? &lt;em&gt;The AT monitor sample demonstrates how to use the AT monitor library and define AT monitors to receive AT notifications from the Modem library.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;br /&gt;Øyvind&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>