Poniższy przykład demonstruje jak za pomocą prostej klasy pomocniczej można manipulować sygnałami IO nody systemu MATES tak jakby sygnały te były elementami tabeli:
import mates
class Dio3(mates.Mates):
"""Represents one MATES-DIO3-MK1 node."""
def __init__(self):
super(Dio3, self).__init__("proxy.mon")
self.node = None
for node in (self.mates_dio3_mk1_1, self.mates_dio3_mk1_2):
# Find first DIO3 node accessible on the bus.
if self.discover_node(node):
self.node = node
break
def get_din(self, channel):
return super(Dio3, self).get_din(self.node, channel)
def set_dout(self, channel, value):
super(Dio3, self).set_dout(self.node, channel, value)
def __getitem__(self, idx):
return self.get_din(idx)
def __setitem__(self, idx, val):
self.set_dout(idx, val)
# First, make sure the node we are using is connected.
m = mates.Mates("proxy.mon", 1)
if m.discover_node(m.mates_dio3_mk1_1):
m.close()
# The with statement below handles opening and closing of
# the MATES handle. See Mates class constructor for details.
with Dio3() as d:
print("First channel: {0}".format(d[0]))
print("Second channel: {0}".format(d[1]))
d[2] = 0
d[3] = 1
# If we have loopback, then the values below will be
# the same as the ones set.
print(d[2])
print(d[3])
else:
m.close()