def main(): global rate daq_device = None ai_device = None ai_info = None descriptor_index = 0 range_index = 0 interface_type = InterfaceType.USB low_channel = 0 high_channel = 3 samples_per_channel = 10000 rate = 100 flags = AInScanFlag.DEFAULT event_types = (DaqEventType.ON_DATA_AVAILABLE | DaqEventType.ON_END_OF_INPUT_SCAN | DaqEventType.ON_INPUT_SCAN_ERROR) ScanParams = namedtuple('ScanParams', 'buffer high_chan low_chan descriptor status') # set the scan options for a FINITE scan ... to set the scan options for # a continuous scan, uncomment the line that or's the SO_CONTINUOUS option # into to the scanOptions variable scan_options = ScanOption.DEFAULTIO # scan_options |= ScanOption.CONTINUOUS try: # 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 from the specified descriptor index. daq_device = DaqDevice(devices[descriptor_index]) # Get the AiDevice object and verify that it is valid. ai_device = daq_device.get_ai_device() if ai_device is None: raise Exception('Error: The DAQ device does not support analog ' 'input') # Verify the device supports hardware pacing for analog input. ai_info = ai_device.get_info() if not ai_info.has_pacer(): raise Exception( '\nError: The specified DAQ device does not support' ' hardware paced analog input') # Establish a connection to the DAQ device. descriptor = daq_device.get_descriptor() print('\nConnecting to', descriptor.dev_string, '- please wait...') daq_device.connect() # The default input mode is SINGLE_ENDED. input_mode = AiInputMode.SINGLE_ENDED # If SINGLE_ENDED input mode is not supported, set to DIFFERENTIAL. if ai_info.get_num_chans_by_mode(AiInputMode.SINGLE_ENDED) <= 0: input_mode = AiInputMode.DIFFERENTIAL # Get the number of channels and validate the high channel number. number_of_channels = ai_info.get_num_chans_by_mode(input_mode) if high_channel >= number_of_channels: high_channel = number_of_channels - 1 channel_count = high_channel - low_channel + 1 # Get a list of supported ranges and validate the range index. ranges = ai_info.get_ranges(input_mode) if range_index >= len(ranges): range_index = len(ranges) - 1 # Allocate a buffer to receive the data. data = create_float_buffer(channel_count, samples_per_channel) # Store the user data for use in the callback function. scan_status = {'complete': False, 'error': False} user_data = ScanParams(data, high_channel, low_channel, descriptor, scan_status) # Enable the event to be notified every time 100 samples are available. available_sample_count = 100 daq_device.enable_event(event_types, available_sample_count, event_callback_function, user_data) print('\n', descriptor.dev_string, 'ready', sep='') print(' Function demonstrated: daq_device.enable_event()') print(' Channels: ', low_channel, '-', high_channel) print(' Input mode: ', input_mode.name) print(' Range: ', ranges[range_index].name) print(' Samples per channel: ', samples_per_channel) print(' Rate: ', rate, 'Hz') print(' Scan options:', display_scan_options(scan_options)) try: input('\nHit ENTER to continue\n') except (NameError, SyntaxError): pass system('clear') # Start the finite acquisition. rate = ai_device.a_in_scan(low_channel, high_channel, input_mode, ranges[range_index], samples_per_channel, rate, scan_options, flags, data) # Wait here until the scan is done ... events will be handled in the # event handler (eventCallbackFunction). # The scan_status values are set in the event handler callback. while not scan_status['complete'] and not scan_status['error']: sleep(0.1) except KeyboardInterrupt: pass except (ValueError, NameError, SyntaxError): pass except Exception as e: print('\n', e) finally: if daq_device: if daq_device.is_connected(): # Stop the acquisition if it is still running. if ai_device and ai_info and ai_info.has_pacer(): ai_device.scan_stop() daq_device.disable_event(event_types) daq_device.disconnect() daq_device.release()
class DAQ: def __init__(self, options, cb): self.serial = options['serial'] self.rate = int(options['rate']) self.samples_per_channel = int(options['samples_per_channel']) self.low_channel = int(options['low_channel']) self.high_channel = int(options['high_channel']) self.channel_count = int(self.high_channel) - int(self.low_channel) + 1 self.flags = AInScanFlag.DEFAULT self.scan_option = ScanOption.CONTINUOUS self.event_types = DaqEventType.ON_DATA_AVAILABLE | DaqEventType.ON_END_OF_INPUT_SCAN | DaqEventType.ON_INPUT_SCAN_ERROR self.ScanParams = namedtuple('ScanParams', 'buffer high_chan low_chan') self.event_callback = cb self.status = ScanStatus self.daq_device = None self.ai_device = None self.data = None self.input_mode = None self.scan_event_parameters_daq = None self.range = options['range'] def configure_mode(self, mode): if mode == "DIFFERENTIAL": self.input_mode = AiInputMode.DIFFERENTIAL elif mode == "SINGLE_ENDED": self.input_mode = AiInputMode.SINGLE_ENDED if self.range == '1': self.range = Range.BIP1VOLTS elif self.range == '2': self.range = Range.BIP2VOLTS elif self.range == '4': self.range = Range.BIP4VOLTS elif self.range == '5': self.range = Range.BIP5VOLTS elif self.range == '10': self.range = Range.BIP10VOLTS elif self.range == '20': self.range = Range.BIP20VOLTS elif self.range == '15': self.range = Range.BIP15VOLTS elif self.range == '30': self.range = Range.BIP30VOLTS elif self.range == '60': self.range = Range.BIP60VOLTS def initialize(self): connected_devices = get_daq_device_inventory(InterfaceType.USB) number_of_devices = len(connected_devices) if number_of_devices == 0: raise Exception( 'OLYMPUS: RUNTIME ERROR - DAQ DEVICE NOT CONNECTED') for daqs in connected_devices: if daqs.unique_id == self.serial: self.daq_device = DaqDevice(daqs) self.ai_device = self.daq_device.get_ai_device() self.daq_device.connect() self.daq_data = create_float_buffer(self.channel_count, self.samples_per_channel) self.scan_event_parameters_daq = self.ScanParams( self.daq_data, self.high_channel, self.low_channel) return True def begin_acquisition(self): self.daq_device.enable_event( self.event_types, self.channel_count * self.samples_per_channel, self.event_callback, self.scan_event_parameters_daq) self.ai_device.a_in_scan(self.low_channel, self.high_channel, self.input_mode, self.range, self.samples_per_channel, self.rate, self.scan_option, self.flags, self.daq_data)