Skip to content

Commit ec5ca61

Browse files
committed
Add options for setting serial interface DTR and RTS
1 parent da61227 commit ec5ca61

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

bauer_bsm/cli/bsmtool.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def create_argument_parser():
5353
parser.add_argument('--chunk-size', metavar='REGISTERS', type=cliutil.auto_int, help='maximum amount of registers to read at once', default=chunk)
5454
parser.add_argument('--trace', action='store_true', help='trace Modbus communication (reads/writes)')
5555
parser.add_argument('--verbose', action='store_true', help='give verbose output')
56+
parser.add_argument('--dtr', metavar='VALUE', type=cliutil.auto_bool, help='set serial device DTR line to VALUE (which may be used for controlling test equipment)', default=None)
57+
parser.add_argument('--rts', metavar='VALUE', type=cliutil.auto_bool, help='set serial device RTS line to VALUE (which may be used for controlling test equipment)', default=None)
5658
parser.add_argument('--public-key-format', choices=cryptoutil.PUBLIC_KEY_RENDERER.keys(), help='output format of ECDSA public key (see RFC 5480 for DER and SEC1 section 2.3.3 for details about formats)', default=cryptoutil.PUBLIC_KEY_DEFAULT_FORMAT)
5759

5860
subparsers = parser.add_subparsers(metavar='COMMAND', help='sub commands')
@@ -128,6 +130,23 @@ def create_client_backend(clazz, args):
128130
client = clazz(slave_id=args.unit, name=args.device,
129131
baudrate=args.baud, timeout=args.timeout, max_count=args.chunk_size,
130132
trace=trace)
133+
134+
# Set the two modem control lines DTR and RTS. This might be useful for
135+
# controlling test equipment via this lines.
136+
serial = None
137+
if isinstance(client, BsmClientDevice):
138+
serial = client.modbus_device.client.serial
139+
elif isinstance(client, SunSpecBsmClientDevice):
140+
serial = client.device.modbus_device.client.serial
141+
else:
142+
raise ValueError('Unsupported client class {}'.format(type(client).__name__))
143+
144+
if args.dtr is not None:
145+
serial.dtr = args.dtr
146+
147+
if args.rts is not None:
148+
serial.rts = args.rts
149+
131150
return client
132151

133152

bauer_bsm/cli/util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,27 @@
99
from ..bsm import format as fmt
1010
from ..crypto import util as cryptoutil
1111

12+
import argparse
1213
import string
1314

1415

1516
MODEL_DATA_INDENT = ' '
1617
BSM_MODEL_ID = 64900
1718

1819

20+
def auto_bool(x):
21+
if isinstance(x, bool) or x is None:
22+
return x
23+
24+
lower = x.lower()
25+
if lower in ['true', 't', 'yes', 'y', '1']:
26+
return True
27+
elif lower in ['false', 'f', 'no', 'n', '0']:
28+
return False
29+
else:
30+
raise argparse.ArgumentTypeError('Could not interpret \'{}\' as bool'.format(x))
31+
32+
1933
def auto_int(x):
2034
result = None
2135

0 commit comments

Comments
 (0)