Skip to content

Commit 0ab824c

Browse files
committed
Factor out hexdumps to utility module
1 parent f079614 commit 0ab824c

2 files changed

Lines changed: 30 additions & 25 deletions

File tree

bauer_bsm/bsm/util.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
# SPDX-License-Identifier: Apache-2.0
66

77

8+
def array_chunks(array, length):
9+
for i in range(0, len(array), length):
10+
yield array[i:i + length]
11+
12+
813
def dict_get_case_insensitive(d, search_key):
914
matches = [k for k in d.keys() if k.lower() == search_key.lower()]
1015
matches_len = len(matches)
@@ -15,3 +20,26 @@ def dict_get_case_insensitive(d, search_key):
1520
return d.get(matches[0])
1621
else:
1722
raise ValueError('Ambigous key \'{}\'.'.format(search_key))
23+
24+
25+
def register_hex_string(data, reg_separator=' '):
26+
return reg_separator.join(map(lambda x: '{:02x}{:02x}'.format(x[0], x[1]), zip(data[::2], data[1::2])))
27+
28+
29+
def register_hexdump_bytes(data, offset=0):
30+
chunk_regs = 8
31+
chunk_length = 2 * chunk_regs
32+
33+
start = offset
34+
lines = []
35+
36+
for chunk in array_chunks(data, chunk_length):
37+
# Hex data of registers.
38+
hex_chunk = register_hex_string(chunk, reg_separator=' ')
39+
# Printable characters from data.
40+
printable = ''.join(map(lambda x: chr(x) if x >= 32 and x < 127 else '.', chunk))
41+
42+
lines.append('{:8}: {:40} {}'.format(start, hex_chunk, printable))
43+
start += chunk_regs
44+
45+
return '\n'.join(lines)

bauer_bsm/cli/bsmtool.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from . import util as cliutil
1212
from ..bsm import config
13+
from ..bsm import util as bsmutil
1314
from ..bsm.client import BsmClientDevice, SunSpecBsmClientDevice
1415
from ..crypto import util as cryptoutil
1516
from ..exporter import chargy, ocmf, registers
@@ -181,35 +182,11 @@ def create_sunspec_client(args):
181182
return create_client_backend(SunSpecBsmClientDevice, args)
182183

183184

184-
def into_chunks(array, length):
185-
for i in range(0, len(array), length):
186-
yield array[i:i + length]
187-
188-
189185
def md_trace_print(string):
190186
for line in string.splitlines():
191187
print(line)
192188

193189

194-
def register_hexdump_bytes(data, offset=0):
195-
chunk_regs = 8
196-
chunk_length = 2 * chunk_regs
197-
198-
start = offset
199-
lines = []
200-
201-
for chunk in into_chunks(data, chunk_length):
202-
# Hex data of registers.
203-
hex_chunk = ' '.join(map(lambda x: '{:02x}{:02x}'.format(x[0], x[1]), zip(chunk[::2], chunk[1::2])))
204-
# Printable characters from data.
205-
printable = ''.join(map(lambda x: chr(x) if x >= 32 and x < 127 else '.', chunk))
206-
207-
lines.append('{:8}: {:40} {}'.format(start, hex_chunk, printable))
208-
start += chunk_regs
209-
210-
return '\n'.join(lines)
211-
212-
213190
def trace_modbus_rtu(string):
214191
# Attempt to logically group known Modbus frame formats in the trace output
215192
# from pySunSpec.
@@ -264,7 +241,7 @@ def dump_command(args):
264241
client = create_client(args)
265242

266243
data = client.read(args.offset, args.length)
267-
print(register_hexdump_bytes(data, args.offset))
244+
print(bsmutil.register_hexdump_bytes(data, args.offset))
268245

269246
client.close()
270247

0 commit comments

Comments
 (0)