Skip to content

Commit

Permalink
Correct DT type, added support for DATE_AND_TIME and fixed AdsSymbol …
Browse files Browse the repository at this point in the history
…interpretation of strings
  • Loading branch information
RobertoRoos committed Feb 17, 2021
1 parent 0c93dfe commit 402f3b9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
10 changes: 6 additions & 4 deletions pyads/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@
# plc data types:
PLCTYPE_BOOL = c_bool
PLCTYPE_BYTE = c_ubyte
PLCTYPE_DATE = c_int32
PLCTYPE_DINT = c_int32
PLCTYPE_DT = c_int32
PLCTYPE_DWORD = c_uint32
PLCTYPE_DINT = c_int32
PLCTYPE_INT = c_int16
PLCTYPE_LREAL = c_double
PLCTYPE_REAL = c_float
PLCTYPE_SINT = c_int8
PLCTYPE_STRING = c_char
PLCTYPE_TIME = c_int32
PLCTYPE_TOD = c_int32
PLCTYPE_UBYTE = c_ubyte
PLCTYPE_UDINT = c_uint32
Expand All @@ -49,6 +46,11 @@
PLCTYPE_LINT = c_int64
PLCTYPE_ULINT = c_uint64

PLCTYPE_DATE = PLCTYPE_DWORD
PLCTYPE_DATE_AND_TIME = PLCTYPE_DWORD
PLCTYPE_DT = PLCTYPE_DWORD
PLCTYPE_TIME = PLCTYPE_DWORD

# Datatype unpacking values
DATATYPE_MAP: Dict[Type, str] = {
PLCTYPE_BOOL: "<?",
Expand Down
17 changes: 17 additions & 0 deletions pyads/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ class AdsSymbol:
value for this symbol
"""

# Regex for array - e.g. "ARRAY [1..10] OF DINT"
_regex_array = re.compile(r"ARRAY \[(\d+)..(\d+)\] OF (.*)")
# Regex for matrix - e.g. "matrix_10_int32"
_regex_matrix = re.compile(r"matrix_(\d+)_(.*)_T")
# Regex for list - e.g. "DINT(10)"
_regex_list = re.compile(r"(.*)\((\d+)\)")

def __init__(
self,
Expand Down Expand Up @@ -272,6 +276,19 @@ def get_type_from_str(type_str: str) -> Any:
if scalar_type_str in constants.PLC_ARRAY_MAP:
return constants.PLC_ARRAY_MAP[scalar_type_str](size)

# If list
reg_match = AdsSymbol._regex_list.match(type_str)
if reg_match is not None:

groups = reg_match.groups()
scalar_type_str = groups[0]
size = int(groups[1])

scalar_type = AdsSymbol.get_type_from_str(scalar_type_str)

if scalar_type:
return scalar_type * size

# We allow unmapped types at this point - Instead we will throw an
# error when they are being addressed

Expand Down
6 changes: 5 additions & 1 deletion tests/test_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@ def test_auto_update(self):
self.assertEqual(r_value, 123.456)



class TypesTestCase(unittest.TestCase):
"""Basic test to cover the PLCTYPE_ARR_* functions"""

Expand All @@ -472,6 +471,11 @@ def test_arrays(self):
self.assertSizeOf(constants.PLCTYPE_ARR_UDINT(n), 4 * n)
self.assertSizeOf(constants.PLCTYPE_ARR_USINT(n), 1 * n)

def test_string(self):
type_str = 'STRING(80)' # This is how a string might appear
plc_type = AdsSymbol.get_type_from_str(type_str)
self.assertSizeOf(plc_type, 1 * 80)


if __name__ == "__main__":
unittest.main()

0 comments on commit 402f3b9

Please sign in to comment.