使用pymodbus模块的Python Modbus服务器

时间:2018-03-25 18:40:53

标签: python server modbus pymodbus

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

如何使用能够将数据读写到客户端IP地址的服务器配置python pymodbus服务器

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

1 个答案:

答案 0 :(得分:1)

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