Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Online uModbus tcp server #119

Open
azat385 opened this issue Jun 29, 2021 · 2 comments
Open

Online uModbus tcp server #119

azat385 opened this issue Jun 29, 2021 · 2 comments

Comments

@azat385
Copy link

azat385 commented Jun 29, 2021

I have started the uModbus tcp server on 89.223.127.70:502 for online debuging of the clients.

Available registers from 0-150 for reading and 6-150 for writing.

Code:

import logging
from socketserver import TCPServer, ThreadingMixIn
from collections import defaultdict

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

from datetime import datetime

log_to_stream(level=logging.DEBUG)
data_store = defaultdict(int)
conf.SIGNED_VALUES = True

host = '0.0.0.0'
port = 502

class ThreadingServer(ThreadingMixIn, TCPServer):
    pass

ThreadingServer.allow_reuse_address = True

try:
    app = get_server(ThreadingServer, (host, port), RequestHandler)
except PermissionError:
    print("You don't have permission to bind on {}".format(port))
    print("Hint: try with a different port (ex:  localhost:50200)")
    exit(1)



@app.route(slave_ids=[1], function_codes=[3, 4], addresses=list(range(0, 150)))
def read_data_store(slave_id, function_code, address):
    """" Return value of address. """
    ts = datetime.now()
    data_store[0] = ts.year
    data_store[1] = ts.month
    data_store[2] = ts.day
    data_store[3] = ts.hour
    data_store[4] = ts.minute
    data_store[5] = ts.second
    return data_store[address]


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


if __name__ == '__main__':
    try:
        app.serve_forever()
    finally:
        app.shutdown()
        app.server_close()
@azat385
Copy link
Author

azat385 commented Jun 29, 2021

Is it possible to run rtu over tcp server (via raw socket or even RFC 2217) with a same data_store?

some how like that:

import serial
from umodbus.client.serial import rtu

serial_port = serial.serial_for_url("socket://0.0.0.0:5020")

@azat385
Copy link
Author

azat385 commented Jun 30, 2021

in fact i need to start tcp ThreadingServer with rtu ADU on a different port.

is it possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant