from qcodes.instrument.base import Instrument
from qcodes.utils import validators as vals
class Advantech_PCIE_1751(Instrument):
def __init__(self, name, device_description="PCIE-1751,BID#0", **kw):
super().__init__(name, **kw)
# Create QCoDeS parameters
for i in range(self.port_count()):
self.add_parameter(
'port{}_dir'.format(i),
label='Port {} direction'.format(i),
vals=vals.Enum(0x00, 0x0f, 0xf0, 0xff),
get_cmd=partial(self._get_port_direction, i),
set_cmd=partial(self._set_port_direction, i),
docstring="The direction (input or output) of the digital i/o "
"port nr {}. Possible values are\n"
" 0x00 indicating that all pins of the port are "
"configured as inputs\n"
" 0x0f indicating that pins 0 to 3 are configured "
"as outputs and pins 4 to 7 as inputs\n"
" 0xf0 indicating that pins 0 to 3 are configured "
"as inputs and pins 4 to 7 as outputs\n"
" 0xff indicating that all pins are configured as "
"outputs".format(i))
self.connect_message()
def port_count(self):
"""
Returns the number of ports on the device. Each port contains 8 input
or output pins.
"""
return self.dll.InstantDoCtrl_getPortCount(self.do)
def _get_port_direction(self, i):
"""
Returns the direction of port i as a 8-bit number where for each bit,
the value 0 means that the corresponding pin is set up as an input and
the value 1 means that it is set up as an output.
"""
pcoll = self.dll.InstantDoCtrl_getPortDirection(self.do)
port_objs = self._ICollection_to_list(pcoll, 'PortDirection *')
return self.dll.PortDirection_getDirection(port_objs[i])
def _set_port_direction(self, i, direction):
"""
i is the number of the port to configure
direction has to be one of the following:
0x00 for all pins configured as inputs
0x0f for the 4 lower pins configured as outputs and 4 higher pins
as inputs
0xf0 for the 4 lower pins configured as inputs and 4 higher pins as
outputs
0xff for all pins configured as outputs
"""
pcoll = self.dll.InstantDoCtrl_getPortDirection(self.do)
port_objs = self._ICollection_to_list(pcoll, 'PortDirection *')
self.check(self.dll.PortDirection_setDirection(port_objs[i], direction))
# Create an instance of the Advantech_PCIE_1751 class
daq_board = Advantech_PCIE_1751('daq_board')
# Connect to the DAQ board
daq_board.connect()
# Set the direction of port 0 to all pins as inputs
daq_board.port0_dir(0x00)
# Set the direction of port 1 to pins 0 to 3 as outputs and pins 4 to 7 as inputs
daq_board.port1_dir(0x0f)
# Read the direction of port 0
port0_direction = daq_board.port0_dir()
# Write a value to port 1
daq_board.port1_dir(0b1010)
# Disconnect from the DAQ board
daq_board.disconnect()