Skip to content

Commit

Permalink
flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
favalex committed Dec 15, 2017
1 parent 7bbcbd5 commit f2ee604
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 22 additions & 6 deletions bin/modbus
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,27 @@ import umodbus.exceptions

REGISTER_RE = re.compile('([cdhi]@)?(\d*)(/.*)?')


def flatmap(f, items):
return chain.from_iterable(map(f, items))


def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)


def fail(fmt, *args):
print(fmt.format(args))
sys.exit(1)


def dump(xs):
return ' '.join('{:02x}'.format(x) for x in xs)


def read_registers(modbus_type, address, pack_types):
if modbus_type in 'cd':
n_registers = 0
Expand All @@ -39,7 +44,7 @@ def read_registers(modbus_type, address, pack_types):
n_bytes += struct.calcsize(pack_type)
assert n_bytes % 2 == 0

n_registers = n_bytes // 2
n_registers = n_bytes // 2

reader = {
'c': 'read_coils',
Expand Down Expand Up @@ -72,9 +77,10 @@ def read_registers(modbus_type, address, pack_types):

return result


def write_registers(modbus_type, address, values):
if modbus_type == 'c':
values = [v[1] for v in values] # drop the pack type
values = [v[1] for v in values] # drop the pack type
if len(values) == 1:
# TODO validate value, should be boolean
message = modbus.write_single_coil(slave_id, address, int(values[0]))
Expand All @@ -87,14 +93,12 @@ def write_registers(modbus_type, address, values):
n_bytes = struct.calcsize(pack_type)
assert n_bytes % 2 == 0

n_registers = n_bytes // 2

if 'f' in pack_type or 'd' in pack_type:
value = float(value)
else:
value = int(value, 0)

words.extend([ h << 8 | l for h, l in grouper(struct.pack(pack_type, value), 2) ])
words.extend([h << 8 | l for h, l in grouper(struct.pack(pack_type, value), 2)])

message = modbus.write_multiple_registers(slave_id, address, words)

Expand All @@ -103,6 +107,7 @@ def write_registers(modbus_type, address, values):

return modbus.send_message(message, connection)


class Access:
def __init__(self, name, modbus_type, write, address, pack_type, value=None):
self.names = [name]
Expand Down Expand Up @@ -153,14 +158,20 @@ class Access:
return (name or address for (name, address) in zip(self.names, self.addresses))

def __repr__(self):
return "{}@{}/{}{}".format(self.modbus_type, self.address(), self.pack_types, "={}".format(self.values) if self.write else "")
return "{}@{}/{}{}".format(self.modbus_type,
self.address(),
self.pack_types,
"={}".format(self.values) if self.write else "")


def by_type(access):
return (access.modbus_type, access.write, access.endianness())


def by_address(access):
return access.address()


def group_accesses(accesses):
grouped = []

Expand All @@ -179,6 +190,7 @@ def group_accesses(accesses):

return grouped


def parse_access(register, name, write, value):
modbus_type, address, pack_type = re.match(REGISTER_RE, register).groups()

Expand Down Expand Up @@ -212,6 +224,7 @@ def parse_access(register, name, write, value):

return Access(name, modbus_type, write, address, pack_type, value)


def parse_accesses(s, registers):
accesses = []

Expand Down Expand Up @@ -239,6 +252,7 @@ def parse_accesses(s, registers):

return accesses


def parse_registers(registers_filename):
registers = {}

Expand All @@ -258,6 +272,7 @@ def parse_registers(registers_filename):

return registers


def connect_to_device(args):
if args.device[0] == '/':
from serial import Serial, PARITY_NONE
Expand Down Expand Up @@ -300,6 +315,7 @@ def connect_to_device(args):

return modbus, slave_id, connection


parser = argparse.ArgumentParser()
parser.add_argument('-r', '--registers')
parser.add_argument('-s', '--slave-id', type=int)
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 120

0 comments on commit f2ee604

Please sign in to comment.