Skip to content

Commit

Permalink
fix boolean writing
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Forsberg committed Feb 26, 2016
1 parent 3489c72 commit 17932fe
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion server/enip/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,22 @@ def int_validate( x, lo, hi ):
assert lo <= res <= hi, "Invalid %d; not in range (%d,%d)" % ( res, lo, hi)
return res

def bool_validate( b ):
try:
res = int( b ) != 0
return res
except ValueError:
pass
lowered = b.lower()
if lowered == "true":
return True
if lowered == "false":
return False
raise ValueError("Invalid %s; could not be interpreted as boolean" % b)

CIP_TYPES = {
'SSTRING': (enip.SSTRING.tag_type, 0, str ),
'BOOL': (enip.BOOL.tag_type, enip.BOOL.struct_calcsize, bool ),
'BOOL': (enip.BOOL.tag_type, enip.BOOL.struct_calcsize, bool_validate ),
'REAL': (enip.REAL.tag_type, enip.REAL.struct_calcsize, float ),
'DINT': (enip.DINT.tag_type, enip.DINT.struct_calcsize, lambda x: int_validate( x, -2**31, 2**32-1 )), # extra range
'UDINT': (enip.UDINT.tag_type, enip.UDINT.struct_calcsize, lambda x: int_validate( x, 0, 2**32-1 )),
Expand Down

0 comments on commit 17932fe

Please sign in to comment.