Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix boolean writing #13

Merged
merged 1 commit into from
Feb 26, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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