问题
I'm IoT newbie and I have a project with Schneider Power Meter.
I read voltage raw data from registers using pymodbus
but I don't know how to convert it to the correct value.
Raw data is [24206, 17242]
from registers address 3927-3928
.
Here is my code :
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
def main():
try:
register = int(input("Registers: "))
modbus = ModbusClient(method='rtu', port='COM4', baudrate=9600, timeout=1,
parity='E', bytesize=8
)
modbus.connect()
r = modbus.read_holding_registers(register, 2, unit=1)
print(r.registers)
modbus.close()
return r.registers
except AttributeError as e:
print(e)
return None
if __name__ == "__main__":
main()
Out:
[24206, 17242]
Registers
Voltage
Raw
Can everyone tell me how to do it?
Thanks.
回答1:
Still, I'm waiting for your code, but with my guess, you will need like the following stuff:
Reading and decoding:
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.client.sync import ModbusTcpClient
def validator(instance):
if not instance.isError():
'''.isError() implemented in pymodbus 1.4.0 and above.'''
decoder = BinaryPayloadDecoder.fromRegisters(
instance.registers,
byteorder=Endian.Big, wordorder=Endian.Little
)
return float(decoder.decode_32bit_float())
else:
# Error handling.
print("There aren't the registers, Try again.")
return None
client = ModbusTcpClient('X.X.X.X', port=502)
connection = client.connect()
if connection:
request = client.read_holding_registers(3927, count=2, unit=1)
data = validator(request)
print(data)
client.close()
else:
print('Connection lost, Try again')
[NOTE]:
Your data either is float32 or float32_inverse.
Thus, with the float32 you will have:
wordorder=Endian.Big
And with the float32_inverse you will have:
wordorder=Endian.Little
来源:https://stackoverflow.com/questions/53275427/how-to-calculate-energy-readings-from-the-raw-data-on-an-power-meter