0
from __future__ import print_function
from pymodbus.server.asynchronous import StartTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.datastore.database import SqlSlaveContext
from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
class CustomDataBlock( ModbusSequentialDataBlock):
""" A datablock that stores the new value in memory
and performs a custom action after it has been stored.
"""

def setValues(self, address, value):
    """ Sets the requested values of the datastore
    :param address: The starting address
    :param values: The new values to be set
    """
    super( ModbusSequentialDataBlock, self).setValues(address, value)
    print("wrote {} to {}".format(value, address))
    def run_custom_db_server():
# ----------------------------------------------------------------------- # 
# initialize your data store
# ----------------------------------------------------------------------- # 
block  = CustomDataBlock(0,[0]*100)
store  = SqlSlaveContext(di=block, co=block, hr=block, ir=block)
context = ModbusServerContext(slaves=store, single=True)

# ----------------------------------------------------------------------- #
# initialize the server information
# ----------------------------------------------------------------------- #

identity = ModbusDeviceIdentification()
identity.VendorName = 'pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'pymodbus Server'
identity.ModelName = 'pymodbus Server'
identity.MajorMinorRevision = '2.2.0'

# ----------------------------------------------------------------------- #
# run the server you want
# ----------------------------------------------------------------------- #

# p = Process(target=device_writer, args=(queue,))
# p.start()
StartTcpServer(context, identity=identity, address=("192.168.1.12", 502))
if __name__ == "__main__":
run_custom_db_server()

我正在从 modscan32 客户端发送命令以在 pymodbus 数据库中存储 32 位无符号整数。但它显示的值与我从 modscan 客户端发送的值不同。这里附有 modscan、pymodbus 数据库和源代码的截图。 Modscan32 视图

pymodbusdb 视图

4

0 回答 0