def main():
    # Parameters for AoDevice.a_out_scan
    low_channel = 0
    high_channel = 0
    voltage_range_index = 0  # Use the first supported range
    samples_per_channel = 2000  # Two second buffer (sample_rate * 2)
    sample_rate = 1000  # Hz
    scan_options = ScanOption.CONTINUOUS
    scan_flags = AOutScanFlag.DEFAULT

    interface_type = InterfaceType.USB
    daq_device = None
    ao_device = None
    scan_status = ScanStatus.IDLE

    try:
        # Get descriptors for all of the available DAQ devices.
        devices = get_daq_device_inventory(interface_type)
        number_of_devices = len(devices)

        # Verify at least one DAQ device is detected.
        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 a DaqDevice object from the first descriptor.
        daq_device = DaqDevice(devices[0])
        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')

        # Verify the specified DAQ device supports hardware pacing for analog output.
        ao_info = ao_device.get_info()
        if not ao_info.has_pacer():
            raise Exception(
                'Error: The DAQ device does not support paced analog output')

        descriptor = daq_device.get_descriptor()
        print('\nConnecting to', descriptor.dev_string, '- please wait...')
        # Establish a connection to the device.
        daq_device.connect()

        chan_string = str(low_channel)
        num_channels = high_channel - low_channel + 1
        if num_channels > 1:
            chan_string = ' '.join((chan_string, '-', str(high_channel)))

        # Select the voltage range
        voltage_ranges = ao_info.get_ranges()
        if voltage_range_index < 0:
            voltage_range_index = 0
        elif voltage_range_index >= len(voltage_ranges):
            voltage_range_index = len(voltage_ranges) - 1
        voltage_range = ao_info.get_ranges()[voltage_range_index]

        # Create a buffer for output data.
        out_buffer = create_float_buffer(num_channels, samples_per_channel)

        # Fill the output buffer with data.
        amplitude = 1.0  # Volts
        offset = amplitude if voltage_range > 1000 else 0.0  # Set an offset if the range is unipolar
        samples_per_cycle = int(sample_rate / 10.0)  # 10 Hz sine wave
        create_output_data(num_channels, samples_per_channel,
                           samples_per_cycle, amplitude, offset, out_buffer)

        print('\n', descriptor.dev_string, 'ready')
        print('    Function demonstrated: AoDevice.a_out_scan')
        print('    Channel(s):', chan_string)
        print('    Range:', voltage_range.name)
        print('    Samples per channel:', samples_per_channel)
        print('    Sample Rate:', sample_rate, 'Hz')
        print('    Scan options:', display_scan_options(scan_options))
        try:
            input('\nHit ENTER to continue')
        except (NameError, SyntaxError):
            pass

        # Start the output scan.
        sample_rate = ao_device.a_out_scan(low_channel, high_channel,
                                           voltage_range, samples_per_channel,
                                           sample_rate, scan_options,
                                           scan_flags, out_buffer)

        system('clear')
        print('Please enter CTRL + C to terminate the process\n')
        print('Active DAQ device: ',
              descriptor.dev_string,
              ' (',
              descriptor.unique_id,
              ')\n',
              sep='')
        print('    Actual scan rate:   ', sample_rate, 'Hz')

        try:
            while True:
                # Get and display the scan status.
                scan_status, transfer_status = ao_device.get_scan_status()
                if scan_status != ScanStatus.RUNNING:
                    break
                print('    Current scan count: ',
                      transfer_status.current_scan_count)
                print('    Current total count:',
                      transfer_status.current_total_count)
                print('    Current index:      ',
                      transfer_status.current_index)
                stdout.flush()
                sleep(0.1)
                # Clear the previous status before displaying the next status.
                for line in range(3):
                    stdout.write(CURSOR_UP + ERASE_LINE)

        except KeyboardInterrupt:
            pass

    except Exception as e:
        print('\n', e)

    finally:
        if daq_device:
            # Stop the scan.
            if scan_status == ScanStatus.RUNNING:
                ao_device.scan_stop()
            # Disconnect from the DAQ device.
            if daq_device.is_connected():
                daq_device.disconnect()
            # Release the DAQ device resource.
            daq_device.release()
예제 #2
0
def main():
    interface_type = InterfaceType.USB
    output_channel = 0
    daq_device = None

    try:
        # Get descriptors for all of the available DAQ devices.
        devices = get_daq_device_inventory(interface_type)
        number_of_devices = len(devices)

        # Verify at least one DAQ device is detected.
        if number_of_devices == 0:
            raise Exception('Error: No DAQ device is detected')

        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 a DaqDevice object from the first descriptor.
        daq_device = DaqDevice(devices[0])
        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')

        descriptor = daq_device.get_descriptor()
        print('\nConnecting to', descriptor.dev_string, '- please wait...')
        # Establish a connection to the device.
        daq_device.connect()

        ao_info = ao_device.get_info()
        output_range = ao_info.get_ranges()[
            0]  # Select the first supported range.

        print('\n', descriptor.dev_string, 'ready')
        print('    Function demonstrated: AoDevice.a_out')
        print('    Channel:', output_channel)
        print('    Range:', output_range.name)
        try:
            input('\nHit ENTER to continue')
        except (NameError, SyntaxError):
            pass

        system('clear')
        print('Active DAQ device: ',
              descriptor.dev_string,
              ' (',
              descriptor.unique_id,
              ')\n',
              sep='')
        print('*Enter non-numeric value to exit')
        try:
            while True:
                try:
                    # Get and set a user specified output value.
                    out_val = input('    Enter output value (V): ')
                    ao_device.a_out(output_channel, output_range,
                                    AOutFlag.DEFAULT, float(out_val))
                    # Clear the previous input request before the next input request.
                    stdout.write(CURSOR_UP + ERASE_LINE)
                except (ValueError, NameError, SyntaxError):
                    break
        except KeyboardInterrupt:
            pass

    except Exception as e:
        print('\n', e)

    finally:
        if daq_device:
            # Disconnect from the DAQ device.
            if daq_device.is_connected():
                daq_device.disconnect()
            # Release the DAQ device resource.
            daq_device.release()