question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating Modbus Server Communication with Client

See original GitHub issue

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). Slave Id : 1 and Port : 255

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()

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:23

github_iconTop GitHub Comments

1reaction
OrangeTuxcommented, Oct 14, 2020

@rahimdzb Modbus requests for slave 1, using function code 3 or 4, addressing registers 0 till 32 will be executed by this callback.

0reactions
rahimdzbcommented, Oct 27, 2020

Sir can you tell me what’s happening exactly in this function ( get_server(Tcpserver,(‘localhost’,502),RequestHandler)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modbus client-server protocol - Campbell Scientific
Only the Modbus client can initiate communications. Modbus servers, in turn, remain silent, communicating only when responding to requests from the Modbus ...
Read more >
4. Creating Modbus server and Modbus client - YouTube
We produced a simple video tutorial showing you what to do after you install the D2000. How should you proceed to efficiently utilize...
Read more >
Modbus TCP and its Client-Server model and MQTT and its ...
Briefly, each client opens a direct connection to a server, forming a 1x1 (one-to-one) connection where the server waits for client requests to...
Read more >
MODBUS Messaging on TCP/IP Implementation Guide V1.0b
The MODBUS messaging service provides a Client/Server communication between devices connected on an Ethernet TCP/IP network.
Read more >
MODBUS TCP/IP Communication Between Client and Server ...
Configure Client Model and Calibrate Parameters · Enter the unique IP address of the server device in the Slave address parameter. · Select...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found