Skip to content
ADoutputs_linux.py 4.34 KiB
Newer Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 22 16:29:22 2019

@author: liaf-ankylosaurus-admin
"""

from uldaq import (get_daq_device_inventory, DaqDevice, InterfaceType,
                   DigitalDirection, DigitalPortIoType, AOutFlag, Range)

#empty some variables and defining interface type
daq_device = None
dio_device = None
port_to_write = None
port_info = None
interface_type = InterfaceType.USB
descriptor_index = 0
port_types_index = 0

# Get descriptors for all of the available DAQ devices.
devices = get_daq_device_inventory(interface_type)
number_of_devices = len(devices)
if number_of_devices == 0:
    raise Exception('Error: No DAQ devices found')

print('Found', number_of_devices, 'DAQ device(s):')
for i in range(number_of_devices):
    print('  ', devices[i].product_name, ' (', devices[i].unique_id, ')', sep='')

# Create the DAQ device object associated with the specified descriptor index.
daq_device = DaqDevice(devices[descriptor_index])

# Get the DioDevice object and verify that it is valid.
dio_device = daq_device.get_dio_device()
if dio_device is None:
    raise Exception('Error: The device does not support digital output')
    
# Create a aoDevice object from the first descriptor.
ao_device = daq_device.get_ao_device() 

# Verify the specified DAQ device supports analog output.
if ao_device is None:
    raise Exception('Error: The DAQ device does not support analog output')
ao_info = ao_device.get_info()

    
# Establish a connection to the DAQ device.
descriptor = daq_device.get_descriptor()
if not daq_device.is_connected():
    print('\nConnecting to', descriptor.dev_string, '- please wait...')
    daq_device.connect()
    print('\n', descriptor.dev_string, 'ready')

def _configure_digital_outs():
        global port_types_index
        # Get the port types for the device(AUXPORT, FIRSTPORTA, ...)
        dio_info = dio_device.get_info()
        port_types = dio_info.get_port_types()

        if port_types_index >= len(port_types):
            port_types_index = len(port_types) - 1

        port_to_write = port_types[port_types_index]

        # Get the port I/O type and the number of bits for the first port.
        port_info = dio_info.get_port_info(port_to_write)

        # If the port is bit configurable, then configure the individual bits
        # for output; otherwise, configure the entire port for output.
        if port_info.port_io_type == DigitalPortIoType.BITIO:
            # Configure all of the bits for output for the first port.
            for bit_number in range(port_info.number_of_bits):
                dio_device.d_config_bit(port_to_write, bit_number,
                                        DigitalDirection.OUTPUT)
        elif port_info.port_io_type == DigitalPortIoType.IO:
            # Configure the entire port for output.
            dio_device.d_config_port(port_to_write, DigitalDirection.OUTPUT)
            
_configure_digital_outs() 
            
class daq_AO(object):
    def __init__(self, out_num, min_value = -10, max_value = 10, output_range = Range.BIP10VOLTS):
        self.out_num = out_num
        self.output_range = output_range
        self.max = max_value
        self.min = min_value
        self.current_value = 0
        
    #methods  
    def set_out(self, value):
        try:
            ao_device.a_out(self.out_num, self.output_range, AOutFlag.DEFAULT, float(value))
            self.current_value = value
        except:
            raise("Failing to set analog output %i", self.out_num)
    
#    def set_out(self, value):
#        print('Analog out %i set to %f' % (self.out_num, value) )
#        return value, self.out_num
       

        
class daq_DO(object):
    global port_types_index, dio_device
    def __init__(self, out_num):
        self.out_num = out_num
           
        self.dio_info = dio_device.get_info()
        self.port_types = self.dio_info.get_port_types()
        self.port_to_write = self.port_types[port_types_index]

    def set_out(self, bit_value):
        try:
            # Output the value to the board
            dio_device.d_bit_out(self.port_to_write, self.out_num, bit_value)
        except:
            raise("Failing to set digital output %i", self.out_num)
            
#    def set_out(self, bit_value):
#        print('Digital out %i set to %s' % (self.out_num, bit_value) )
#        return bit_value, self.out_num
#