Connecting to PCIE 1751 by Advantech in Python
Instrument Card
Peripheral Component Interconnect Express (PCIe or PCI-E) is a serial expansion bus standard for connecting a computer to one or more peripheral devices.
PCIE-1751 is a 48-bit digital I/O card for the PCI Express bus. Its 48 channels are divided into six 8-bit I/O ports and users can configure each 4-channel per port (nibble) as input or output via software. PCIE-1751 also provides three 32-bit counters.
Device Specification: here
Manufacturer card: ADVANTECH
Advantech is a leader in providing trusted innovative embedded and automation products and solutions.
- Headquarters: Taiwan
- Yearly Revenue (millions, USD): 2313
- Vendor Website: here
Demo: Record temperature over time with a LabJack DAQ board
Connect to the PCIE 1751 in Python
Read our guide for turning Python scripts into Flojoy nodes.
PROTOCOLS > SCPI
Here is an example Python script that uses Qcodes Community to connect to a PCIE 1751 DAQ board:
from qcodes.instrument.base import Instrumentfrom 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 classdaq_board = Advantech_PCIE_1751('daq_board')
# Connect to the DAQ boarddaq_board.connect()
# Set the direction of port 0 to all pins as inputsdaq_board.port0_dir(0x00)
# Set the direction of port 1 to pins 0 to 3 as outputs and pins 4 to 7 as inputsdaq_board.port1_dir(0x0f)
# Read the direction of port 0port0_direction = daq_board.port0_dir()
# Write a value to port 1daq_board.port1_dir(0b1010)
# Disconnect from the DAQ boarddaq_board.disconnect()
Note: This code assumes that you have already installed the necessary Advantech drivers and have generated the _bdaqctrl.h
file as mentioned in the documentation.