<?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>MODBUS and serial disable</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/112884/modbus-and-serial-disable</link><description>We use custom board with NRF52840 and a MODBUS CO2 sensor. We are struggling with power consumption when the serial is enabled. The goal is to take measurements and then put the board to sleep, after a certain time, wake board up and take new measurements</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 11 Jul 2024 11:38:35 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/112884/modbus-and-serial-disable" /><item><title>RE: MODBUS and serial disable</title><link>https://devzone.nordicsemi.com/thread/493384?ContentTypeID=1</link><pubDate>Thu, 11 Jul 2024 11:38:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dd125576-c5cd-43e2-81f8-29549a69c7e9</guid><dc:creator>Tiit</dc:creator><description>&lt;p&gt;I can get rid of Interface already used with modbus_disable and that is also working as expected on DK board.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MODBUS and serial disable</title><link>https://devzone.nordicsemi.com/thread/493382?ContentTypeID=1</link><pubDate>Thu, 11 Jul 2024 11:36:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2703f41c-b697-4ec7-bb4a-59fa2b06a830</guid><dc:creator>Tiit</dc:creator><description>&lt;p&gt;Strange .. I did a test with DK and my sensor and it is working OK. It is something in my custom boards code then, that is not functioning. I will check what is the problem.&lt;/p&gt;
&lt;p&gt;I tried with that:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/sys/util.h&amp;gt;
#include &amp;lt;zephyr/drivers/gpio.h&amp;gt;
#include &amp;lt;zephyr/pm/pm.h&amp;gt;
#include &amp;lt;zephyr/pm/device.h&amp;gt;
#include &amp;lt;zephyr/pm/state.h&amp;gt;
#include &amp;lt;zephyr/modbus/modbus.h&amp;gt;

#include &amp;lt;zephyr/logging/log.h&amp;gt;
LOG_MODULE_REGISTER(mbc_sample, LOG_LEVEL_DBG);

static int client_iface;

const static struct modbus_iface_param client_param = {
	.mode = MODBUS_MODE_RTU,
	.rx_timeout = 10000,
	.serial = {
		.baud = 9600,
		.parity = UART_CFG_PARITY_NONE,
		.stop_bits_client = UART_CFG_STOP_BITS_1,
		},
};

#define SENSOR_UID 104
#define MODBUS_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(zephyr_modbus_serial)

static const struct device *const uart_modbus =  DEVICE_DT_GET(DT_NODELABEL(uart0));

static int init_modbus_client(void)
{
	const char iface_name[] = {DEVICE_DT_NAME(MODBUS_NODE)};

	client_iface = modbus_iface_get_by_name(iface_name);

	return modbus_init_client(client_iface, client_param);
}

void measure_co2() {
	uint16_t input_reg_data[4] = {0};
	int err;

	init_modbus_client();

	while(true){
		//Read CO2 value
		err = modbus_read_input_regs(client_iface, SENSOR_UID, 0, input_reg_data, 4);
		
		if (err != 0) {
			LOG_ERR(&amp;quot;MODBUS error %d&amp;quot;, err);
			if(true){
				LOG_INF(&amp;quot;Trying once more...&amp;quot;);
			}else{
			break;
			}
		} else {

			uint16_t co2_value = input_reg_data[3];

			if (input_reg_data[0] != 0)	{
			}else{
				LOG_INF(&amp;quot;CO2 Level: %d&amp;quot;, co2_value);

			break;
			}
		}
	k_sleep(K_MSEC(1000));
	}
}

int main(void)
{
	int err = 0;
	
	for (int i=1;i&amp;lt;5;i++){
		measure_co2();
		
		k_sleep(K_MSEC(1000));
		
		err = pm_device_action_run(uart_modbus, PM_DEVICE_ACTION_SUSPEND);
		if(err) LOG_INF(&amp;quot;PM Error: %d&amp;quot;,err);
		
		k_sleep(K_MSEC(5000));
		LOG_INF(&amp;quot;Sleep 5 sec...&amp;quot;);

		err = pm_device_action_run(uart_modbus, PM_DEVICE_ACTION_RESUME);
		if(err) LOG_INF(&amp;quot;PM Error: %d&amp;quot;,err);
				
		measure_co2();
		
		k_sleep(K_MSEC(1000));
	}

}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And the log is:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;00&amp;gt; *** Booting nRF Connect SDK v3.5.99-ncs1 ***
00&amp;gt; [00:00:00.538,177] &amp;lt;inf&amp;gt; modbus_serial: RTU timeout 4010 us
00&amp;gt; [00:00:00.548,278] &amp;lt;wrn&amp;gt; modbus: Client wait-for-RX timeout
00&amp;gt; [00:00:00.548,278] &amp;lt;err&amp;gt; mbc_sample: MODBUS error -116
00&amp;gt; [00:00:00.548,278] &amp;lt;inf&amp;gt; mbc_sample: Trying once more...
00&amp;gt; [00:00:01.558,471] &amp;lt;wrn&amp;gt; modbus: Client wait-for-RX timeout
00&amp;gt; [00:00:01.558,502] &amp;lt;err&amp;gt; mbc_sample: MODBUS error -116
00&amp;gt; [00:00:01.558,502] &amp;lt;inf&amp;gt; mbc_sample: Trying once more...
00&amp;gt; [00:00:02.558,624] &amp;lt;inf&amp;gt; mbc_sample: CO2 Level: 674
00&amp;gt; [00:00:08.558,746] &amp;lt;inf&amp;gt; mbc_sample: Sleep 5 sec...
00&amp;gt; [00:00:08.558,776] &amp;lt;err&amp;gt; modbus: Interface already used
00&amp;gt; [00:00:08.568,878] &amp;lt;wrn&amp;gt; modbus: Client wait-for-RX timeout
00&amp;gt; [00:00:08.568,878] &amp;lt;err&amp;gt; mbc_sample: MODBUS error -116
00&amp;gt; [00:00:08.568,878] &amp;lt;inf&amp;gt; mbc_sample: Trying once more...
00&amp;gt; [00:00:09.568,969] &amp;lt;inf&amp;gt; mbc_sample: CO2 Level: 674
00&amp;gt; [00:00:10.569,061] &amp;lt;err&amp;gt; modbus: Interface already used
00&amp;gt; [00:00:10.579,193] &amp;lt;wrn&amp;gt; modbus: Client wait-for-RX timeout
00&amp;gt; [00:00:10.579,193] &amp;lt;err&amp;gt; mbc_sample: MODBUS error -116
00&amp;gt; [00:00:10.579,193] &amp;lt;inf&amp;gt; mbc_sample: Trying once more...
00&amp;gt; [00:00:11.579,284] &amp;lt;inf&amp;gt; mbc_sample: CO2 Level: 674
00&amp;gt; [00:00:17.579,437] &amp;lt;inf&amp;gt; mbc_sample: Sleep 5 sec...
00&amp;gt; [00:00:17.579,467] &amp;lt;err&amp;gt; modbus: Interface already used
00&amp;gt; [00:00:17.589,569] &amp;lt;wrn&amp;gt; modbus: Client wait-for-RX timeout
00&amp;gt; [00:00:17.589,569] &amp;lt;err&amp;gt; mbc_sample: MODBUS error -116
00&amp;gt; [00:00:17.589,569] &amp;lt;inf&amp;gt; mbc_sample: Trying once more...
00&amp;gt; [00:00:18.589,660] &amp;lt;inf&amp;gt; mbc_sample: CO2 Level: 675
00&amp;gt; [00:00:19.589,752] &amp;lt;err&amp;gt; modbus: Interface already used
00&amp;gt; [00:00:19.599,884] &amp;lt;wrn&amp;gt; modbus: Client wait-for-RX timeout
00&amp;gt; [00:00:19.599,884] &amp;lt;err&amp;gt; mbc_sample: MODBUS error -116
00&amp;gt; [00:00:19.599,884] &amp;lt;inf&amp;gt; mbc_sample: Trying once more...
00&amp;gt; [00:00:20.599,975] &amp;lt;inf&amp;gt; mbc_sample: CO2 Level: 675
00&amp;gt; [00:00:26.600,128] &amp;lt;inf&amp;gt; mbc_sample: Sleep 5 sec...
00&amp;gt; [00:00:26.600,158] &amp;lt;err&amp;gt; modbus: Interface already used
00&amp;gt; [00:00:26.610,260] &amp;lt;wrn&amp;gt; modbus: Client wait-for-RX timeout
00&amp;gt; [00:00:26.610,260] &amp;lt;err&amp;gt; mbc_sample: MODBUS error -116
00&amp;gt; [00:00:26.610,260] &amp;lt;inf&amp;gt; mbc_sample: Trying once more...
00&amp;gt; [00:00:27.610,351] &amp;lt;inf&amp;gt; mbc_sample: CO2 Level: 675
00&amp;gt; [00:00:28.610,443] &amp;lt;err&amp;gt; modbus: Interface already used
00&amp;gt; [00:00:28.620,574] &amp;lt;wrn&amp;gt; modbus: Client wait-for-RX timeout
00&amp;gt; [00:00:28.620,574] &amp;lt;err&amp;gt; mbc_sample: MODBUS error -116
00&amp;gt; [00:00:28.620,574] &amp;lt;inf&amp;gt; mbc_sample: Trying once more...
00&amp;gt; [00:00:29.620,666] &amp;lt;inf&amp;gt; mbc_sample: CO2 Level: 675&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The sensor is not answering always in first try, and thats OK.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Anyway.. thanks for testing it and giving good advise.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MODBUS and serial disable</title><link>https://devzone.nordicsemi.com/thread/493378?ContentTypeID=1</link><pubDate>Thu, 11 Jul 2024 11:19:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c740ba53-6021-41fa-9ef0-82797a30e2ed</guid><dc:creator>runsiv</dc:creator><description>&lt;p&gt;Could you upload the logs, prj.conf and the modbus part of your application just in case there is something there&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;
&lt;p&gt;Runar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MODBUS and serial disable</title><link>https://devzone.nordicsemi.com/thread/493351?ContentTypeID=1</link><pubDate>Thu, 11 Jul 2024 09:44:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c84345bc-8888-409c-8ae6-8eec1455244e</guid><dc:creator>Tiit</dc:creator><description>&lt;p&gt;Hmm .. strange. OK .. I try again here,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MODBUS and serial disable</title><link>https://devzone.nordicsemi.com/thread/493346?ContentTypeID=1</link><pubDate>Thu, 11 Jul 2024 09:35:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:453707e6-871e-4d3c-8132-e578b9cd4d0e</guid><dc:creator>runsiv</dc:creator><description>&lt;p&gt;I&amp;#39;m just playing a bit around with the Modbus RTU client and server sample using two DKs. I don&amp;#39;t have access to my PPK today but I see no issue when I add the following&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;pm_device_action_run(uart,PM_DEVICE_ACTION_SUSPEND);

k_msleep(sleep);

pm_device_action_run(uart,PM_DEVICE_ACTION_RESUME);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Log&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;Client side
[00:00:15.360,961] &amp;lt;inf&amp;gt; mbc_sample: Coils state 0x07
[00:00:20.388,000] &amp;lt;inf&amp;gt; mbc_sample: Coils state 0x00
server side
[00:00:18.276,062] &amp;lt;inf&amp;gt; mbs_sample: Coil write, addr 2, 1
[00:00:23.289,672] &amp;lt;inf&amp;gt; mbs_sample: Coil read, addr 0, 1
[00:00:23.289,672] &amp;lt;inf&amp;gt; mbs_sample: Coil read, addr 1, 1
[00:00:23.289,703] &amp;lt;inf&amp;gt; mbs_sample: Coil read, addr 2, 1
[00:00:23.303,222] &amp;lt;inf&amp;gt; mbs_sample: Coil write, addr 0, 0
[00:00:23.303,222] &amp;lt;inf&amp;gt; mbs_sample: Coil write, addr 1, 0
[00:00:23.303,222] &amp;lt;inf&amp;gt; mbs_sample: Coil write, addr 2, 0
[00:00:28.316,864] &amp;lt;inf&amp;gt; mbs_sample: Coil read, addr 0, 0
[00:00:28.316,864] &amp;lt;inf&amp;gt; mbs_sample: Coil read, addr 1, 0
[00:00:28.316,894] &amp;lt;inf&amp;gt; mbs_sample: Coil read, addr 2, 0
[00:00:28.329,193] &amp;lt;inf&amp;gt; mbs_sample: Coil write, addr 0,&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MODBUS and serial disable</title><link>https://devzone.nordicsemi.com/thread/493318?ContentTypeID=1</link><pubDate>Thu, 11 Jul 2024 08:12:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:eb798a57-61d0-403d-9dfd-fa5fd609fe0f</guid><dc:creator>Tiit</dc:creator><description>&lt;p&gt;Yes .. I toke MODBUS RTU Client Sample as base for my code. I tried modbus_disable before sleep and reinit after wake, but it does still not communicate anymore (the error -116 should be timeout).&lt;/p&gt;
&lt;p&gt;I have right now only one DK board and I can&amp;#39;t try the MODBUS RTU Server and Client sample with UART sleep -- I think there would be the same problem.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MODBUS and serial disable</title><link>https://devzone.nordicsemi.com/thread/493312?ContentTypeID=1</link><pubDate>Thu, 11 Jul 2024 07:50:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f4f46145-3a48-4e81-a0d4-68e88065cb1d</guid><dc:creator>runsiv</dc:creator><description>&lt;p&gt;I have to check a bit, it&amp;#39;s modbus RTU you are using right?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;
&lt;p&gt;Runar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MODBUS and serial disable</title><link>https://devzone.nordicsemi.com/thread/493156?ContentTypeID=1</link><pubDate>Wed, 10 Jul 2024 12:30:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4d8c102b-27df-4166-875e-536220879eea</guid><dc:creator>Tiit</dc:creator><description>&lt;p&gt;NCS 2.6.0 and if I try to suspend and wake UART, then after wake-up I get:&amp;nbsp;&amp;nbsp;MODBUS error -116 -- then only reset makes MODBUS working again.&lt;/p&gt;
&lt;p&gt;How to check what is using HFCLK? We are using BLE for communication, but that is also suspended when board goes to sleep.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MODBUS and serial disable</title><link>https://devzone.nordicsemi.com/thread/493128?ContentTypeID=1</link><pubDate>Wed, 10 Jul 2024 10:58:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:69d7a3cf-2f8e-4f58-b5d4-a56f5ed7f6a9</guid><dc:creator>runsiv</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;Which SDK are you using?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In Zephyr you can not easliy disable a device so I would suggest using &lt;a href="https://docs.nordicsemi.com/bundle/ncs-latest/page/zephyr/services/pm/device.html#introduction"&gt;Device Power Management&amp;nbsp;&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;span&gt;PM_DEVICE_ACTION_SUSPEND&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;A idle current of 300µA sounds a bit high, I would suspect that you have some floating pins or maybe the HF crystal in used.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Regards&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Runar&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>