Page 1 of 1

MODBUS RTU reading

Posted: Thu Jul 21, 2022 10:19 am
by kakarot
Hello everyone

I am trying to read data from slave using modbus protocol.
My slave device is Elmeasure Lg+5110 energy meter.

Modbus communication parameters are :
baud rate :-9600
mode :- modbus RTU (half duplex)
parity :- None
port :-UART PORT 2 (GPIO 16 & 17)

I am using MAX-485 IC for proper communication between esp32 and slave device which is elmeasure energymeter.
I have attached files here which i am using to send request and read data from slave.
Expected output of the code is to read value and store it in JSON as key value pair.

Output at beginning: {"Avg_Current":0.021621618419885635,"Current_R":0.0648648589849472,"Current_Y":0,"Current_B":0,"Neutral_Current":0,"Avg_VLN":76.359291076660156,"Voltage_R":229.07786560058594,"Voltage_Y":0,"Voltage_B":0,"Avg_VLL":152.64540100097656,"Voltage_RY":229.04116821289062,"Voltage_YB":0,"Voltage_BR":228.89505004882812,"Watt":13.422094345092773,"Watt_R":13.422094345092773,"Watt_Y":0,"Watt_B":0,"Frequency":50.041572570800781,"Avg_PF":0.91303509473800659,"PF_R":0.91303509473800659,"PF_Y":1,"PF_B":1,"Watt_hour":5302.0830078125,"VA_hour":5842.8515625,"VAr_Total":-6.0250649452209473,"VAr_R":-6.0250649452209473,"VAr_Y":0,"VAr_B":0,"VA_Total":14.771618843078613,"VA_R":14.771618843078613,"VA_Y":0,"VA_B":0,"VARi":0,"VARc":0,"THD_VR":1.8450759649276733,"THD_VY":0,"THD_VB":0,"THD_AR":0,"THD_AY":0,"THD_AB":0}

And i got this output for sometime but after some time period value of all parameters are interchanged with each other.

Output after sometime period :
{"Avg_Current":0,"Current_R":0.021621618419885635,"Current_Y":0.0648648589849472,"Current_B":0,"Neutral_Current":0,"Avg_VLN":0,"Voltage_R":76.359291076660156,"Voltage_Y":229.07786560058594,"Voltage_B":0,"Avg_VLL":0,"Voltage_RY":152.64540100097656,"Voltage_YB":229.04116821289062,"Voltage_BR":0,"Watt":228.89505004882812,"Watt_R":13.422094345092773,"Watt_Y":13.422094345092773,"Watt_B":0,"Frequency":0,"Avg_PF":50.041572570800781,"PF_R":0.91303509473800659,"PF_Y":0.91303509473800659,"PF_B":1,"Watt_hour":1,"VA_hour":5302.0830078125,"VAr_Total":5842.8515625,"VAr_R":-6.0250649452209473,"VAr_Y":-6.0250649452209473,"VAr_B":0,"VA_Total":0,"VA_R":14.771618843078613,"VA_Y":14.771618843078613,"VA_B":0,"VARi":0,"VARc":0,"THD_VR":0,"THD_VY":1.8450759649276733,"THD_VB":0,"THD_AR":0,"THD_AY":0,"THD_AB":0}


If anyone know about this problem or have any idea about this problem please comment and replay.
Thank you in advance. :D

Re: MODBUS RTU reading

Posted: Thu Jul 21, 2022 1:43 pm
by ESP_alisitsyn
Hello @kakarot ,

This happens because your slave device responds right after the slave response timeout configured in the master kconfig.
After response timeout, the master starts the next transaction but gets a response from the previous one due to slave processing delay.
The Modbus serial protocol can not determine if the received response is from a previous transaction if the commands are the same in both transactions (no any transaction ID field in the PDU). As a result, you see the Modbus register value requested in the previous transaction and the cycle continues for other values. I propose you set the response time in kconfig menu appropriately.

Could you also let me know which version of esp-idf you are using with the esp-modbus CPP wrapper?

Re: MODBUS RTU reading

Posted: Fri Jul 22, 2022 6:47 am
by kakarot
Hello @ESP_alisitsyn,
Thanks for your responce.

I am using ESP-IDF version 4.4.
Here is the snapshot of my menu-config , as you can see i have changes it value to their maximum values.
I tried using default values and also tried using maximum values and i get the same results in both.
What should i do ?

I want to read data fast to reduce latency here , and reading 40 parameters are not much high volume of data.
Suggest me any solution for it. Your help will be very greatful to me.

Re: MODBUS RTU reading

Posted: Fri Jul 22, 2022 8:52 am
by ESP_alisitsyn
Hello @kakarot,

The problem is in enabled `Modbus stack use timer for 3.5T symbol time measurement` option but the v4.4 will not use this correctly without the patch. I recommend you to use the `esp-modbus` component from:

https://www.github.com/espressif/esp-modbus

In order to use `esp-modbus` component, your application must include the statement below in its `CMakeLists.txt` file to exclude the `freemodbus` component from the build.

Code: Select all

  set(EXCLUDE_COMPONENTS freemodbus)
- This is important addition !

The `main` component folder of your application shall include the component manager manifest file `idf_component.yml` as below:

Code: Select all

dependencies:
  idf: ">=4.1"
  espressif/esp-modbus:
    version: "^1.0"
Refer to `component manager documentation https://docs.espressif.com/projects/esp ... nager.html for more information on how to set up the component manager.
The `esp-modbus` component can be found in `component manager registry` https://components.espressif.com/compon ... esp-modbus

If you do not want to use the component manager please just create the `components/espressif__esp-modbus` folder in your project folder and copy there the context of the component archive file downloaded from the URL shown above. In this case you do not need to create the manifest file as described above.

Then please disable the kconfig option`Modbus stack use timer for 3.5T symbol time measurement` option and enable `Modbus timer uses ISR dispatch method`.

Please let me know if you have issues with this. Thank you.

Re: MODBUS RTU reading

Posted: Mon Jul 25, 2022 9:01 am
by kakarot
Thanks @ESP_alisitsyn for your precious time.
I will make suitable changes as per your suggestion and i will let you know about result i got after that .