2

I want to create a modbus server (with local host : ip address : 152.168.96.11 - same as the system) and the modbus client has (ip address : 152.168.96.32). My client application is running and i am creating the modbus server application with the umodbus server application. Data exchange of 32 bits (either read or write for testing purpose).

How do i configure python umodbus server with server able to read and write data into the client ip address

Here is the umodbus server application -

#!/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()
Chandra Sekhar K
  • 317
  • 7
  • 18
  • A server can not read/write in to a client where as it is the other way aroud, the client should be able to read and write to server. If you want your server to be reachable on all of its IPv4 address use `0.0.0.0` in your `get_server` call instead of `152.168.96.11` – Sanju Apr 01 '18 at 05:30

0 Answers0