主流modbus python库比较
主流modbus python库
比较
minimalmodbus 使用
主流modbus python库
当前,用的比较多的Modbus python 库主要有以下几种:
Modbus_tk
Pymodbus
Minimalmodbus
Umodbus
比较
库名称
安装
源码
3rd party依赖
主要功能
modbus_tk
pip install modbus_tk
https://github.com/ljean/modbus-tk
pyserial
Support modbus TCP and RTU, both master and slave。Don’t support modbus ASCII。 重量级
pymodbus
pip install pymodbus
https://github.com/riptideio/pymodbus/
pyserial
Support modbus TCP/RTU/ASCII, master and slave。 重量级
minimalmodbus
pip install minimalmodbus
https://github.com/pyhys/minimalmodbus
pyserial
Only support Modbus RTU and ASCII, and only work in master(clinet). 非常轻量级
uModbus
pip install uModbus
https://github.com/AdvancedClimateSystems/uModbus/
no
Support both Modbus client amd server (both TCP and RTU). Don’t support Modbus ASCII。轻量级
minimalmodbus 使用
Doc:
https://minimalmodbus.readthedocs.io/en/stable/
https://pypi.org/project/minimalmodbus/
API https://minimalmodbus.readthedocs.io/en/stable/apiminimalmodbus.html
read_register(registeraddress, number_of_decimals=0, functioncode=3, signed=False)[source]
Read an integer from one 16-bit register in the slave, possibly scaling it.
The slave register can hold integer values in the range 0 to 65535 (“Unsigned INT16”).
Args:
• registeraddress (int): The slave register address (use decimal numbers, not hex).
• number_of_decimals (int): The number of decimals for content conversion.
• functioncode (int): Modbus function code. Can be 3 or 4.
• signed (bool): Whether the data should be interpreted as unsigned or signed.
Note
The parameter number_of_decimals was named numberOfDecimals before MinimalModbus 1.0
If a value of 77.0 is stored internally in the slave register as 770, then use number_of_decimals=1 which will divide the received data by 10 before returning the value.
Similarly number_of_decimals=2 will divide the received data by 100 before returning the value.
Some manufacturers allow negative values for some registers. Instead of an allowed integer range 0 to 65535, a range -32768 to 32767 is allowed. This is implemented as any received value in the upper range (32768 to 65535) is interpreted as negative value (in the range -32768 to -1).
Use the parameter signed=True if reading from a register that can hold negative values. Then upper range data will be automatically converted into negative return values (two’s complement).
signed Data type in slave Alternative name Range
False Unsigned INT16 Unsigned short 0 to 65535
True INT16 Short -32768 to 32767
Returns:
The register data in numerical value (int or float).
Raises:
TypeError, ValueError, ModbusException, serial.SerialException (inherited from IOError)
write_register(registeraddress, value, number_of_decimals=0, functioncode=16, signed=False)[source]
Write an integer to one 16-bit register in the slave, possibly scaling it.
The slave register can hold integer values in the range 0 to 65535 (“Unsigned INT16”).
Args:
• registeraddress (int): The slave register address (use decimal numbers, not hex).
• value (int or float): The value to store in the slave register (might be scaled before sending).
• number_of_decimals (int): The number of decimals for content conversion.
• functioncode (int): Modbus function code. Can be 6 or 16.
• signed (bool): Whether the data should be interpreted as unsigned or signed.
Note
The parameter number_of_decimals was named numberOfDecimals before MinimalModbus 1.0
To store for example value=77.0, use number_of_decimals=1 if the slave register will hold it as 770 internally. This will multiply value by 10 before sending it to the slave register.
Similarly number_of_decimals=2 will multiply value by 100 before sending it to the slave register.
As the largest number that can be written to a register is 0xFFFF = 65535, the value and number_of_decimals should max be 65535 when combined. So when using number_of_decimals=3 the maximum value is 65.535.
For discussion on negative values, the range and on alternative names, see read_register().
Use the parameter signed=True if writing to a register that can hold negative values. Then negative input will be automatically converted into upper range data (two’s complement).
Returns:
None
Raises:
TypeError, ValueError, ModbusException, serial.SerialException (inherited from IOError)