Variables

SECS defines a few types to transmit data in.

Data Type Class Code
List secsgem.secs.variables.SecsVarArray L
List secsgem.secs.variables.SecsVarList L
Binary secsgem.secs.variables.SecsVarBinary B
Boolean secsgem.secs.variables.SecsVarBoolean TF
ASCII secsgem.secs.variables.SecsVarString A
8-Byte integer secsgem.secs.variables.SecsVarI8 I8
1-Byte integer secsgem.secs.variables.SecsVarI1 I1
2-Byte integer secsgem.secs.variables.SecsVarI2 I2
4-Byte integer secsgem.secs.variables.SecsVarI4 I4
8-Byte float secsgem.secs.variables.SecsVarF8 F8
4-Byte float secsgem.secs.variables.SecsVarF4 F8
8-Byte unsigned integer secsgem.secs.variables.SecsVarU8 U8
1-Byte unsigned integer secsgem.secs.variables.SecsVarU1 U1
2-Byte unsigned integer secsgem.secs.variables.SecsVarU2 U2
4-Byte unsigned integer secsgem.secs.variables.SecsVarU4 U4

Example:

>>> secsgem.SecsVarString(value="TESTString")
A 'TESTString'
>>> secsgem.SecsVarBoolean(value=True)
TF True
>>> secsgem.SecsVarU4(value=1337)
U4 1337

Type arrays

The numeric types can also be an array of that type:

>>> secsgem.SecsVarU1(value=[1, 2, 3, 4])
U1 [1, 2, 3, 4]
>>> secsgem.SecsVarBoolean(value=[True, False, False, True])
TF [True, False, False, True]

The length of this array can be fixed with the length parameter:

>>> secsgem.SecsVarU1(length=3, value=[1, 2, 3])
U1 [1, 2, 3]
>>> secsgem.SecsVarU1(length=3, value=[1, 2, 3, 4])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/bparzella/Developer/secsgem/secsgem/secs/variables.py", line 1694, in __init__
    self.set(value)
  File "/Users/bparzella/Developer/secsgem/secsgem/secs/variables.py", line 1716, in set
    raise ValueError("Value longer than {} chars".format(self.length))
ValueError: Value longer than 3 chars
>>> secsgem.SecsVarString(length=3, value="Hello").get()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/bparzella/Developer/secsgem/secsgem/secs/variables.py", line 776, in __init__
    self.set(value)
  File "/Users/bparzella/Developer/secsgem/secsgem/secs/variables.py", line 794, in set
    raise ValueError("Value longer than {} chars".format(self.length))
ValueError: Value longer than 3 chars

Getting data

The data can be accessed with the secsgem.secs.variables.SecsVarU1.get() method, arrays can be accessed using the index operator:

>>> secsgem.SecsVarU1(value=1).get()
1
>>> secsgem.SecsVarU1(length=3, value=[1, 2, 3]).get()
[1, 2, 3]
>>> secsgem.SecsVarU1(value=1)[0]
1
>>> secsgem.SecsVarU1(length=3, value=[1, 2, 3])[1]
2

Setting data

The data can be set with the secsgem.secs.variables.SecsVarString.set() method, arrays can be updated using the index operator:

>>> v=secsgem.SecsVarU1(length=3, value=[1, 2, 3])
>>> v.set([3, 2, 1])
>>> v
U1 [3, 2, 1]
>>> v[0] = 1
>>> v
U1 [1, 2, 1]

En-/Decoding

The variable types can secsgem.secs.variables.SecsVarArray.encode() and secsgem.secs.variables.SecsVarString.decode() themselves to ASCII data transferrable with the HSMS protocol:

>>> v=secsgem.SecsVarString(value="Hello")
>>> d=v.encode()
>>> d
'A\x05Hello'
>>> secsgem.format_hex(d)
'41:05:48:65:6c:6c:6f'
>>> v.set("NewText")
>>> v
A 'NewText'
>>> v.decode(d)
7
>>> v
A 'Hello'

SecsVarArray

secsgem.secs.variables.SecsVarArray is a special type for a list of the same type. The items of the array can be accessed with the index operator.

>>> v=secsgem.SecsVarArray(secsgem.SecsVarU4(1))
>>> v.set([1, 2, 3])
>>> v
[U4 1, U4 2, U4 3]
>>> v.get()
[1, 2, 3]
>>> v[1]
2

A new item can be appended to the array with the secsgem.secs.variables.SecsVarArray.append() method.

SecsVarList

secsgem.secs.variables.SecsVarList is a special type for a list of the different types. The items of the list can be accessed like properties of the object.

An ordered dictionary is required for the creation, because pythons default dictionary will be randomly sorted. Sorting is essential because both peers need to have the data in the same order.

>>> v=secsgem.SecsVarList(OrderedDict((
...         ("var1", secsgem.SecsVarU4(1)),
...         ("var2", secsgem.SecsVarString()),
...     )), 2)
>>> v.var1=3
>>> v.var2="Hallo"
>>> v
[var1: U4 3, var2: A 'Hallo']
>>> v.var2
'Hallo'
>>> secsgem.format_hex(v.encode())
'01:02:b1:04:00:00:00:03:41:05:48:61:6c:6c:6f'

SecsVarDynamic

secsgem.secs.variables.SecsVarDynamic can take different types, if specified to a certain set of types.

>>> v=secsgem.SecsVarDynamic([secsgem.SecsVarString, secsgem.SecsVarU1])
>>> v.set(secsgem.SecsVarString(value="Hello"))
>>> v
A 'Hello'
>>> v.set(secsgem.SecsVarU1(value=10))
>>> v
U1 10
>>> v.set(secsgem.SecsVarU4(value=10))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/bparzella/Developer/secsgem/secsgem/secs/variables.py", line 176, in set
    raise ValueError("Unsupported type {} for this instance of SecsVarDynamic, allowed {}".format(value.__class__.__name__, self.types))
ValueError: Unsupported type SecsVarU4 for this instance of SecsVarDynamic, allowed [<class 'secsgem.secs.variables.SecsVarString'>, <class 'secsgem.secs.variables.SecsVarU1'>]