我想创建一个 Modbus 服务器(IP 地址:152.168.96.11 - 与系统相同)和在不同系统中运行的 Modbus 客户端(IP 地址:152.168.96.32)。我的客户端应用程序运行成功,我正在使用 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()