Modbus slave data storage [IDFGH-3202]

Chriserke
Posts: 9
Joined: Mon Mar 09, 2020 1:56 pm

Modbus slave data storage [IDFGH-3202]

Postby Chriserke » Tue Apr 28, 2020 2:28 am

Hello,

When trying to modify the modbus slave example i've run into some issues.

I'm trying to print the registers values after they have been changed however the value never changes on the esp32 side.
Running the modbus poll tool shows that after i've written a value to the esp32 slave that it has changed after reading again with the tool.

On the esp side the variable doesn't seem to update though for some reason.

This is using esp-idf 4.0

Code: Select all

		if(event & (MB_EVENT_HOLDING_REG_WR | MB_EVENT_HOLDING_REG_RD)) {
			// Get parameter information from parameter queue
			ESP_ERROR_CHECK(mbc_slave_get_param_info(&reg_info, MB_PAR_INFO_GET_TOUT));
			printf("HOLDING %s: time_stamp(us):%u, mb_addr:%u, type:%u, st_address:0x%.4x, size:%u\r\n",
				rw_str,
				(uint32_t)reg_info.time_stamp,
				(uint32_t)reg_info.mb_offset,
				(uint32_t)reg_info.type,
				(uint32_t)reg_info.address,
				(uint32_t)reg_info.size);

			if (reg_info.address == (uint8_t*)&holding_reg_params.data_chan2)
			{
				// do something
				printf("Holding reg value, %f", holding_reg_params.data_chan2);
			}
Example output:
Holding reg value, 0.000000HOLDING WRITE: time_stamp(us):38686618, mb_addr:1, type:1, st_address:0x3ffb2f94, size:4
Holding reg value, 0.000000HOLDING READ: time_stamp(us):42933014, mb_addr:1, type:2, st_address:0x3ffb2f94, size:4
Holding reg value, 0.000000HOLDING READ: time_stamp(us):46748690, mb_addr:1, type:2, st_address:0x3ffb2f94, size:4
Holding reg value, 0.000000HOLDING READ: time_stamp(us):47860313, mb_addr:1, type:2, st_address:0x3ffb2f94, size:4

This same method works fine for the coil registers but i seem to be stuck at trying to make the holding registers work.

So any insight would be much appreciated.

ESP_Alvin
Posts: 195
Joined: Thu May 17, 2018 2:26 am

Re: Modbus slave data storage [IDFGH-3202]

Postby ESP_Alvin » Tue Apr 28, 2020 6:21 am

Moderator's note: edit the topic title for issue tracking, thanks.

ESP_alisitsyn
Posts: 203
Joined: Fri Feb 01, 2019 4:02 pm
Contact:

Re: Modbus slave data storage [IDFGH-3202]

Postby ESP_alisitsyn » Tue Apr 28, 2020 6:48 am

Hi Chriserke,

The approach you showed in your code must work. I guess the reason for the float value = 0.0000 is that you are writing the incorrect value of registers from mb_addr=1 and they are not correspond to correct float value. The

Code: Select all

printf("Holding reg value, %f", holding_reg_params.data_chan2);
will print zeroes in this case. For example if you write the registers from address 1 to [0x0000, 0x40A0] it corresponds to float value = 5.00.

Please try to write correct float value in the registers and let me know if it solves your issue. You could use the ModbusPoll tool and set Setup->Read/Write definition->Display property to float and then write changed float value into slave.

Chriserke
Posts: 9
Joined: Mon Mar 09, 2020 1:56 pm

Re: Modbus slave data storage [IDFGH-3202]

Postby Chriserke » Tue Apr 28, 2020 1:39 pm

Hello,

I've tried some stuff and it seems that changing data_chan0 to 3 from float to uint16 seems to do the trick.

Changing the value in the tool now also changes the value when i try and print it.

However i'm guessing that this is probably not how it's supposed to be done.

ESP_alisitsyn
Posts: 203
Joined: Fri Feb 01, 2019 4:02 pm
Contact:

Re: Modbus slave data storage [IDFGH-3202]

Postby ESP_alisitsyn » Tue Apr 28, 2020 4:29 pm

I agree that it is little bit unclear but this is how standard printf shows the float value made from incorrect bytes. How would you expect to print the float in this case (NAN)? The Modbus stack in this example just transfers the value of registers into its place of concrete parameter but how to print value it is responsibility of user and his particular Modbus device. For example you may print the "float value = 5.00 (0x000040A0)" with the representation in hex form. This is how it was printing in Modbus master example.

Chriserke
Posts: 9
Joined: Mon Mar 09, 2020 1:56 pm

Re: Modbus slave data storage [IDFGH-3202]

Postby Chriserke » Tue Apr 28, 2020 6:18 pm

Thanks for the help i'll see if i can make the floats work some other time.

ESP_alisitsyn
Posts: 203
Joined: Fri Feb 01, 2019 4:02 pm
Contact:

Re: Modbus slave data storage [IDFGH-3202]

Postby ESP_alisitsyn » Wed Apr 29, 2020 5:16 am

The master device must knows register map of your slave (parameter structure) then it can set parameter of any type correctly and stack will transfer it to the slave device parameter structure. Good luck!

Who is online

Users browsing this forum: No registered users and 108 guests