2

我想创建一个 modbus 服务器(本地主机:ip 地址:152.168.96.11 - 与系统相同)和 modbus 客户端(ip 地址:152.168.96.32)。我的客户端应用程序正在运行,我正在使用 umodbus 服务器应用程序创建 modbus 服务器应用程序。32 位数据交换(为测试目的读取或写入)。

我如何配置 python umodbus 服务器,服务器能够读取和写入数据到客户端 IP 地址

这是 umodbus 服务器应用程序 -

#!/usr/bin/env python
# scripts/examples/simple_tcp_server.py
import random
import logging
from socketserver import TCPServer
from collections import defaultdict

from umodbus import conf
from umodbus.server.tcp import RequestHandler, get_server
from umodbus.utils import log_to_stream

# Create Random Values
rndata = []
for i in range(32):
    rndata.append(random.randint(1,32))

# Add stream handler to logger 'uModbus'.
log_to_stream(level=logging.DEBUG)

# A very simple data store which maps addresss against their values.
data_store = defaultdict(int)

# Enable values to be signed (default is False).
conf.SIGNED_VALUES = True

TCPServer.allow_reuse_address = True
app = get_server(TCPServer, ('152.168.96.11', 255), RequestHandler)


@app.route(slave_ids=[1], function_codes=[3, 4], addresses=list(range(0, 32)))
def read_data_store(slave_id, function_code, address):
    """" Return value of address. """
    return data_store[address]


@app.route(slave_ids=[1], function_codes=[6, 16], addresses=list(range(0, 32)))
def write_data_store(slave_id, function_code, address, value):
    """" Set value for address. """
    data_store[address] = value

# Configuring the Data
write_data_store(1,16,3,784)
write_data_store(1,16,24,678)
rdata1 = read_data_store(1,4,3)
print(rdata1)
rdata2 = read_data_store(1,4,24)
print(rdata2)

if __name__ == '__main__':
    try:
        app.serve_forever()
        print(rdata1)
    finally:
        app.shutdown()
        app.server_close()
4

0 回答 0