1
0
mirror of https://github.com/JoelBender/modpypes synced 2025-10-19 22:08:20 +08:00

host and port options that can be overridden

This commit is contained in:
Joel Bender 2016-09-19 23:53:57 -04:00
parent 25ab8f52bf
commit 46740fc139
2 changed files with 26 additions and 8 deletions

View File

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

View File

@ -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")