Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reading PZEM-004t power sensor (new version v3.0) through Modbus-RTU protocol over TTL UART
# Reading PZEM-004t power sensor (new version v3.0) through Modbus-RTU protocol over TTL UART
# Run as:
# python3 pzem_004t.py
# To install dependencies:
# pip install modbus-tk
# pip install pyserial
import serial
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
# Connect to the slave
serial = serial.Serial(
port='/dev/PZEM_sensor',
baudrate=9600,
bytesize=8,
parity='N',
stopbits=1,
xonxoff=0
)
master = modbus_rtu.RtuMaster(serial)
master.set_timeout(2.0)
master.set_verbose(True)
data = master.execute(1, cst.READ_INPUT_REGISTERS, 0, 10)
voltage = data[0] / 10.0 # [V]
current = (data[1] + (data[2] << 16)) / 1000.0 # [A]
power = (data[3] + (data[4] << 16)) / 10.0 # [W]
energy = data[5] + (data[6] << 16) # [Wh]
frequency = data[7] / 10.0 # [Hz]
powerFactor = data[8] / 100.0
alarm = data[9] # 0 = no alarm
print('Voltage [V]: ', voltage)
print('Current [A]: ', current)
print('Power [W]: ', power) # active power (V * I * power factor)
print('Energy [Wh]: ', energy)
print('Frequency [Hz]: ', frequency)
print('Power factor []: ', powerFactor)
print('Alarm : ', alarm)
# Changing power alarm value to 100 W
# master.execute(1, cst.WRITE_SINGLE_REGISTER, 1, output_value=100)
try:
master.close()
if slave.is_open:
slave.close()
except:
pass
@NA7KR

This comment has been minimized.

Copy link

NA7KR commented Jan 10, 2020

Please, can you help? (new to RS485 )
Traceback (most recent call last):
File "test4.py", line 28, in
data = master.execute(1, cst.READ_INPUT_REGISTERS, 0, 10)
File "/usr/local/lib/python3.7/dist-packages/modbus_tk/utils.py", line 39, in new
raise excpt
File "/usr/local/lib/python3.7/dist-packages/modbus_tk/utils.py", line 37, in new
ret = fcn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/modbus_tk/modbus.py", line 306, in execute
response_pdu = query.parse_response(response)
File "/usr/local/lib/python3.7/dist-packages/modbus_tk/modbus_rtu.py", line 46, in parse_response
raise ModbusInvalidResponseError("Response length is invalid {0}".format(len(response)))
modbus_tk.exceptions.ModbusInvalidResponseError: Response length is invalid 0

@connermacleod69

This comment has been minimized.

Copy link

connermacleod69 commented Feb 3, 2020

Hi,
works like a charm, thanks for sharing.
Regards.

@trytomo

This comment has been minimized.

Copy link

trytomo commented May 26, 2020

Please, can you help? (new to RS485 )
Traceback (most recent call last):
File "test4.py", line 28, in
data = master.execute(1, cst.READ_INPUT_REGISTERS, 0, 10)
File "/usr/local/lib/python3.7/dist-packages/modbus_tk/utils.py", line 39, in new
raise excpt
File "/usr/local/lib/python3.7/dist-packages/modbus_tk/utils.py", line 37, in new
ret = fcn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/modbus_tk/modbus.py", line 306, in execute
response_pdu = query.parse_response(response)
File "/usr/local/lib/python3.7/dist-packages/modbus_tk/modbus_rtu.py", line 46, in parse_response
raise ModbusInvalidResponseError("Response length is invalid {0}".format(len(response)))
modbus_tk.exceptions.ModbusInvalidResponseError: Response length is invalid 0

Check the rx tx pin & try to cross the cable in your USB to TTL dongle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.