1

I read and decode float_32 value using pymodbus.

Before, I decode that with the following code as simple:

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())

But recently I occurred with this error:

TypeError: fromRegisters() got an unexpected keyword argument 'endian'

[UPDATE]

I think the newer version of pymodbus have been modified (endian argument have been deprecated):

A reference: Looks like the arguments changed but not the documentation

Then I changed this line as following:

dec = BinaryPayloadDecoder.fromRegisters(
    req.registers,
    byteorder=Endian.Big,
    wordorder=Endian.Little)

Problem:

Now I want to check the pymodbus version to know which version of decode must be used.

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
  • 1
    If you used pip to install it, you can find out the version using [this](https://stackoverflow.com/questions/10214827/find-which-version-of-package-is-installed-with-pip) – whackamadoodle3000 Jun 25 '18 at 05:06
  • @ᴡʜᴀᴄᴋᴀᴍᴀᴅᴏᴏᴅʟᴇ3000 Thanks, it was helpful for me, but I want this, in code. – Benyamin Jafari Jun 25 '18 at 05:11
  • 1
    The documentations are up to date and you can find them [here](http://pymodbus.readthedocs.io/en/latest/source/library/pymodbus.html#module-pymodbus.payload). Also the new version was introduced in pymodbus v1.4.0, refer release notes https://github.com/riptideio/pymodbus/releases/tag/v1.4.0 – Sanju Jun 25 '18 at 15:14

1 Answers1

2

I found a trick to bypass pymodbus version to decode float_32 values to handle of the difference version of decode function:

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)

Or :

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)

[NOTE]:

Also you can check the PyPi packages version with: pip show <pkg-name>

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150