Modbus support for PZEM-004T

e14boy
Posts: 2
Joined: Wed Mar 24, 2021 2:54 am

Modbus support for PZEM-004T

Postby e14boy » Fri Apr 09, 2021 8:29 am

The PZEM-004T is a popular module for measuring AC voltage, current, power etc. A datasheet can be found here: https://innovatorsguru.com/wp-content/u ... Manual.pdf (and also attached).

The datasheet states:
The application layer use the Modbus-RTU protocol to communicate. At present, it only supports function codes such as 0x03 (Read Holding Register), 0x04 (Read Input Register), 0x06 (Write Single Register), 0x41 (Calibration), 0x42 (Reset energy).etc.

It also describes reading multiple Input Registers (NOT Holding Registers) as follows:
The command format of the master reads the measurement result is total of 8 bytes) Slave Address + 0x04 + Register Address High Byte + Register Address Low Byte + Number of Registers High Byte + Number of Registers Low Byte + CRC Check High Byte + CRC Check Low Byte.

A simple program calling uart_write_bytes and uart_read_bytes to a PZEM-004T demonstrates the above functions perform as specified, with multiple registers being returned to a buffer and the "reset energy" function doing what it says on the tin. Of course, this does not properly implement the Modbus protocol and is prone to errors. So it is much preferred to use the Free Modbus port.

Unfortunately, it appears the Modbus port does not support reading multiple Input Registers or a way to call the other functions described above i.e. 0x41 (Calibration) and 0x42 (Reset Energy). Is this a shortcoming of the Free Modbus port and can/will these functions be added?
Attachments
PZEM-004T-V3.0-Datasheet-User-Manual.pdf
(405.59 KiB) Downloaded 324 times

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

Re: Modbus support for PZEM-004T

Postby ESP_alisitsyn » Wed Apr 14, 2021 2:05 pm

Hi @e14boy,

The ESP-Modbus supports the standard set of Modbus commands.
mb_standard_commands.png
Standard Modbus function codes supported by ESP-Modbus
mb_standard_commands.png (42.83 KiB) Viewed 4789 times
The PZEM-004T uses some subset of cusom commands (as such 0x41, 0x42) that are not standard and can not be suppurted by ESP-Modbus stack. However architecture of the stack allows to extend list of commands by adding function code handler function and register it in the

Code: Select all

modbus/mb_m.c::xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX]
add the command send function into new file:

Code: Select all

modbus/functions/mbfunc_cal_m.c
then call it in the

Code: Select all

serial_master/modbus_controller/mbc_serial_master.c::mbc_serial_master_send_request()
These functions will not be added to support because they applicable to only PZEM-004T device. There is a way to override functionality of the stack and add your custom commands as described above.

Let me know if you need more information.

e14boy
Posts: 2
Joined: Wed Mar 24, 2021 2:54 am

Re: Modbus support for PZEM-004T

Postby e14boy » Tue Apr 20, 2021 6:00 am

Hi @ESP_alisitsyn, that's great information.

In terms of adding a custom function to mbc_serial_master_send_request, I assume this requires modifying the code for whatever version of ESP-IDF is installed by platformio. And this makes it liable to be overwritten. I'm sure that is manageable but is this assumption even correct? Is there a better way? I assume a similar approach is needed to modify definitions in mbconfig.h that would allow me to disable unwanted functions (for reading coils etc).

Many thanks for your help.

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

Re: Modbus support for PZEM-004T

Postby ESP_alisitsyn » Thu Apr 29, 2021 1:20 pm

Hi @e14boy,

I think it is ok to customize the code of stack as per you project requirements. The standard approach for Modbus devices is to support just basic functions and provide the register map which addresses device specific part of device. If you need to do non-standard things it is ok to modify (override) functionality of specific layer. Please see the example here on how to override the functionality of the stack in your application: https://github.com/alisitsyn/modbus_sup ... ride_slave
The specific cmake file: https://github.com/alisitsyn/modbus_sup ... eLists.txt
would help to override the required behavior and can be independent of the ESP-IDF version installed. I think this approach would work for platformio as well.

Let me know if you have any problems with it.

Who is online

Users browsing this forum: Corand and 102 guests