# -*- coding: utf-8 -*- """ Created on Thu Oct 25 15:35:49 2018 @author: Usuario For now, this will work with one measruement computing daq. It is expected to be the USB-3105 and the board number will be 0. If the daq is not found, whenever trying to send signals, the command will be printed in the console. """ import warnings try: from mcculw import ul from mcculw.enums import InterfaceType, ULRange, InfoType, BoardInfo, DigitalIODirection from mcculw.ul import ULError from props.digital import DigitalProps from props.ao import AnalogOutputProps except: warnings.warn('Problem importing ul from mcculw, Thus might happen in Linux') import uldaq as ul from uldaq import InterfaceType from uldaq import Range as ULRange try: ul.ignore_instacal() #instacal will be ignored except NameError: print('Instacall not ignored because mcculw not imported') except AttributeError: print('Instacall not ignored because you imported uldaq and there is no instacal in that case') # First of all, I check which daqs are available and print it. devices = ul.get_daq_device_inventory(interface_type=InterfaceType.USB) if len(devices) == 1 : device = devices[0] print('daq %s is connected' % device.product_name) else: warnings.warn('No daq connected') def discover_device(board_num, board_name): """ Checks if daq is found. """ devices = ul.get_daq_device_inventory(InterfaceType.USB) #get device inventory device = next((device for device in devices if board_name in device.product_name), None) if device != None: #if it is found, try: ul.create_daq_device(board_num, device) #create an instance except ULError: ul.release_daq_device(board_num) ul.create_daq_device(board_num, device) warnings.warn('The board number %i was already in use, and it was released and redefined ' % board_num); finally: return True return False daqfound = discover_device(0, 'USB-3105') class daq_AO(object): def __init__(self, out_num, board_num = 0, output_range = ULRange.BIP10VOLTS): global daqfound self.out_num = out_num self.board_num = board_num self.output_range = output_range self.current_value = None if daqfound: ul.set_config(InfoType.BOARDINFO, self.board_num, self.out_num, BoardInfo.DACRANGE, self.output_range) self.ao_props = AnalogOutputProps(self.board_num) #methods if daqfound: def set_out(self, value): try: ul.v_out(self.board_num, self.out_num, self.output_range, value) self.current_value = value except ULError as e: self.show_ul_error(e) else: def set_out(self, value): print('Analog out %i set to %f' % (self.out_num, value) ) self.current_value = value return value, self.out_num class daq_DO(object): def __init__(self, out_num): global daqfound self.board_num = 0 self.out_num = out_num if daqfound: self.digital_props = DigitalProps(self.board_num) # Find the first port that supports output, defaulting to None # if one is not found. self.port = next( (port for port in self.digital_props.port_info if port.supports_output), None) # If the port is configurable, configure it for output if self.port != None and self.port.is_port_configurable: try: ul.d_config_port( self.board_num, self.port.type, DigitalIODirection.OUT) except ULError as e: self.show_ul_error(e) if daqfound: def set_out(self, bit_value): try: # Output the value to the board ul.d_bit_out(self.board_num, self.port.type, self.out_num, bit_value) except ULError as e: self.show_ul_error(e) else: def set_out(self, bit_value): print('Digital out %i set to %s' % (self.out_num, bit_value) ) return bit_value, self.out_num