Python 使用 pymodbus 模块的 Modbus 服务器

Python Modbus Server using pymodbus module

我想在不同的系统(IP 地址:152.168.96.32)中创建一个 Modbus 服务器(IP 地址:152.168.96.11 - 与系统相同)和 Modbus 客户端 运行。我的客户端应用程序 运行 成功,我正在使用 pymodbus 服务器应用程序创建 Modbus 服务器应用程序。 32 位数据交换(为测试目的而读或写)。我想将特定地址的值读写到Modbus客户端。

如何配置 python pymodbus 服务器,使服务器能够读取数据并将数据写入客户端 IP 地址

这里是pymodbus服务器应用程序-

# --------------------------------------------------------------------------- # 
# import the various server implementations
# --------------------------------------------------------------------------- # 
from pymodbus.server.sync import StartTcpServer

from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext

# --------------------------------------------------------------------------- # 
# configure the service logging
# --------------------------------------------------------------------------- # 
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)


def run_server():

    # ----------------------------------------------------------------------- # 
    # initialize your data store
    # ----------------------------------------------------------------------- #
    block = ModbusSequentialDataBlock(0, [888]*32)
    store = ModbusSlaveContext(hr=block)

    slaves  = {
               0x01: store,
              }
    context = ModbusServerContext(slaves=slaves, single=True)

    # ----------------------------------------------------------------------- # 
    # initialize the server information
    # ----------------------------------------------------------------------- # 
    # If you don't set this or any fields, they are defaulted to empty strings.
    # ----------------------------------------------------------------------- # 
    identity = ModbusDeviceIdentification()
    identity.VendorName = 'Pymodbus'
    identity.ProductCode = 'PM'
    identity.VendorUrl = 'http://github.com/riptideio/pymodbus/'
    identity.ProductName = 'Pymodbus Server'
    identity.ModelName = 'Pymodbus Server'
    identity.MajorMinorRevision = '1.0'

    # ----------------------------------------------------------------------- #
    # run the server you want
    # ----------------------------------------------------------------------- # 
    # Tcp:
    StartTcpServer(context, identity=identity, address=('0.0.0.0', 255))


if __name__ == "__main__":
    run_server()

参考 Github 中提出的 issue 并由贡献者跟进。