From 46740fc139d8c929fe6a45b97b0d6f7bec3ddc91 Mon Sep 17 00:00:00 2001 From: Joel Bender Date: Mon, 19 Sep 2016 23:53:57 -0400 Subject: [PATCH] host and port options that can be overridden --- modpypes/client.py | 13 +++++++++++-- modpypes/server.py | 21 +++++++++++++++------ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/modpypes/client.py b/modpypes/client.py index 828255e..9611f62 100644 --- a/modpypes/client.py +++ b/modpypes/client.py @@ -67,8 +67,17 @@ class ConsoleClient(ConsoleCmd, Client): print("address, unit and register required") return - # get the address and unit + # get the address and unit, and register addr, unitID, register = args[:3] + + # address might have a port + if ':' in addr: + addr, port = addr.split(':') + server_address = (addr, int(port)) + else: + server_address = (addr, 502) + + # unit identifier unitID = int(unitID) if _debug: ConsoleClient._debug(" - addr, unitID: %r, %r", addr, unitID) @@ -114,7 +123,7 @@ class ConsoleClient(ConsoleCmd, Client): return # set the destination - req.pduDestination = (addr, 502) + req.pduDestination = server_address req.mpduUnitID = unitID if _debug: ConsoleClient._debug(" - req: %r", req) diff --git a/modpypes/server.py b/modpypes/server.py index d86ba82..ab91738 100644 --- a/modpypes/server.py +++ b/modpypes/server.py @@ -2,13 +2,11 @@ # -*- coding: utf-8 -*- """ -Server -====== - This executable module is a console application for presenting itself as a MODBUS server accepting read and write MODBUS PDUs. """ +import os import math from bacpypes.debugging import bacpypes_debugging, ModuleLogger @@ -29,6 +27,9 @@ from .app import ModbusServer, ModbusException _debug = 0 _log = ModuleLogger(globals()) +# settings +SERVER_HOST = os.getenv("SERVER_HOST", "") +SERVER_PORT = int(os.getenv("SERVER_PORT", 502)) # # ConsoleServer @@ -53,15 +54,23 @@ class ConsoleServer(ConsoleCmd, Client): def main(): # parse the command line arguments parser = ArgumentParser(description=__doc__) - - # now parse the arguments + parser.add_argument( + "--host", type=str, + help="address of host (default {!r})".format(SERVER_HOST), + default=SERVER_HOST, + ) + parser.add_argument( + "--port", type=int, + help="server port (default {!r})".format(SERVER_PORT), + default=SERVER_PORT, + ) args = parser.parse_args() if _debug: _log.debug("initialization") if _debug: _log.debug(" - args: %r", args) # local IO functions - bind(ConsoleServer(), ModbusServer()) + bind(ConsoleServer(), ModbusServer(port=args.port)) _log.debug("running")