我对简单的pymodbus服务器实现有一个问题。从我在文档中读到的内容来看,此实现应该为每个从设备具有唯一的从上下文,即写入设备0x01,寄存器地址1,应该是与设备0x02,寄存器1不同的寄存器
在我的例子中,写入寄存器1会写入所有从属地址的寄存器1。有人可以检查一下我的服务器代码,看看我是否遗漏了什么,或者澄清一下我对pymodbus服务器在单标志设置为False时应该如何工作的理解
干杯。代码如下:
#!/usr/bin/env python3
"""
Pymodbus Synchronous Server
--------------------------------------------------------------------------
This synced server is implemented using TCP, with multiple slave contexts
"""
# --------------------------------------------------------------------------- #
# 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
def run_server():
slaves = {
0x01: ModbusSlaveContext(),
0x02: ModbusSlaveContext()
}
context = ModbusServerContext(slaves=slaves, single=False)
# ----------------------------------------------------------------------- #
# 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 = '2.3.0'
# ----------------------------------------------------------------------- #
# run the server
# ----------------------------------------------------------------------- #
StartTcpServer(context, identity=identity, address=("0.0.0.0", 5020))
if __name__ == "__main__":
run_server()
我在RTU服务器上也遇到了类似的问题,在其他方面,我的代码与您的代码差不多
但对我来说,我没有在slave dict中创建separat modbus slavecontext对象,但在您的代码中不是这样
我在这里分享我的代码,也许它能帮助别人
相关问题 更多 >
编程相关推荐