How to check pymodbus version for decode a float_32 value?
我使用pymodbus读取和解码浮点值。
之前,我用以下代码简单地解码:
1 2 3 4 5 6 7 8 9 | from pymodbus.client.sync import ModbusTcpClient from pymodbus.constants import Endian from pymodbus.payload import BinaryPayloadDecoder cli = ModbusTcpClient('an-IP') cli.connect() req = cli.read_holding_registers(<hex-address>, count=4) dec = BinaryPayloadDecoder.fromRegisters(req.registers, endian=Endian.Little) print(dec.decode_32bit_float()) |
但最近我出现了这个错误:
1 | TypeError: fromRegisters() got an unexpected keyword argument 'endian' |
[更新]
我认为更新版本的
A reference: Looks like the arguments changed but not the documentation
然后我把这行改为:
1 2 3 4 | dec = BinaryPayloadDecoder.fromRegisters( req.registers, byteorder=Endian.Big, wordorder=Endian.Little) |
问题:
现在,我想检查pymodbus版本以了解必须使用哪个版本的解码。
我发现了一个绕过
1 2 3 4 5 6 7 8 9 | try: '''For pymodbus 1.3.2 and older version.''' dec = BinaryPayloadDecoder.fromRegisters(req.registers, endian=Endian.Little) except: '''For pymodbus 1.4.0 and newer version.''' dec = BinaryPayloadDecoder.fromRegisters(req.registers, byteorder=Endian.Big, wordorder=Endian.Little) |
或:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import inspect if 'endian' in inspect.getargspec(BinaryPayloadDecoder.fromRegisters)[0]: '''For pymodbus 1.3.2 and older version.''' dec = BinaryPayloadDecoder.fromRegisters( req.registers, endian=Endian.Little) else: '''For pymodbus 1.4.0 and newer version.''' dec = BinaryPayloadDecoder.fromRegisters( req.registers, byteorder=Endian.Big, wordorder=Endian.Little) |
[注记]:
也可以使用: