def __init__(self):
        devices = get_daq_device_inventory(InterfaceType.USB)
        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[0]
        )  #if more than one device, change this to the one you want
        self.daq_device = daq_device

        # Get the DioDevice object and verify that it is valid.
        dio_device = daq_device.get_dio_device()
        self.dio_device = dio_device
        if dio_device is None:
            raise Exception(
                'Error: The device does not support digital output')

        self.connect()
def main():
    daq_device = None
    dio_device = None
    port_to_write = None
    port_info = None

    interface_type = InterfaceType.USB
    descriptor_index = 0
    port_types_index = 0

    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 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')

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

        # 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)

        # Configure the port for output.
        if (port_info.port_io_type == DigitalPortIoType.IO
                or port_info.port_io_type == DigitalPortIoType.BITIO):
            dio_device.d_config_port(port_to_write, DigitalDirection.OUTPUT)

        print('\n', descriptor.dev_string, ' ready', sep='')
        print('    Function demonstrated: dio_device.d_out()')
        print('    Port: ', port_types[port_types_index].name)
        try:
            input('\nHit ENTER to continue\n')
        except (NameError, SyntaxError):
            pass

        system('clear')

        max_port_value = int(pow(2.0, port_info.number_of_bits) - 1)

        reset_cursor()
        print('Active DAQ device: ',
              descriptor.dev_string,
              ' (',
              descriptor.unique_id,
              ')\n',
              sep='')
        try:
            while True:
                try:
                    data = input('Enter a value between 0 and ' +
                                 '{:.0f}'.format(max_port_value) +
                                 ' (or non-numeric character to exit): ')

                    # write the first port
                    dio_device.d_out(port_to_write, int(data))

                    sleep(0.1)
                    cursor_up()
                    clear_eol()
                except (ValueError, NameError, SyntaxError):
                    break
        except KeyboardInterrupt:
            pass

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

    finally:
        if daq_device:
            if dio_device and port_to_write and port_info:
                # before disconnecting, set the port back to input
                if (port_info.port_io_type == DigitalPortIoType.IO
                        or port_info.port_io_type == DigitalPortIoType.BITIO):
                    dio_device.d_config_port(port_to_write,
                                             DigitalDirection.INPUT)
            if daq_device.is_connected():
                daq_device.disconnect()
            daq_device.release()
def main():
    daq_device = None
    dio_device = None
    status = ScanStatus.IDLE

    samples_per_channel = 10000
    rate = 1000
    scan_options = ScanOption.CONTINUOUS
    flags = DInScanFlag.DEFAULT

    interface_type = InterfaceType.USB
    descriptor_index = 0
    port_types_index = 0

    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 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 DAQ device does not support digital input')

        # Verify that the specified device supports hardware pacing for digital input.
        dio_info = dio_device.get_info()
        if not dio_info.has_pacer(DigitalDirection.INPUT):
            raise Exception(
                'Error: The specified DAQ device does not support hardware paced digital input'
            )

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

        # Get the port types for the device(AUXPORT, FIRSTPORTA, ...)
        port_types = dio_info.get_port_types()

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

        port_to_read = port_types[port_types_index]

        # Configure the port for input.
        port_info = dio_info.get_port_info(port_to_read)
        if (port_info.port_io_type == DigitalPortIoType.IO
                or port_info.port_io_type == DigitalPortIoType.BITIO):
            dio_device.d_config_port(port_to_read, DigitalDirection.INPUT)

        low_port = port_to_read
        hi_port = port_to_read
        number_of_ports = hi_port - low_port + 1

        # Allocate a buffer to receive the data.
        data = create_int_buffer(number_of_ports, samples_per_channel)

        print('\n', descriptor.dev_string, ' ready', sep='')
        print('    Function demonstrated: dio_device.d_in_scan()')
        print('    Device name: ', descriptor.dev_string)
        print('    Port: ', low_port.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 acquisition.
        dio_device.d_in_scan(low_port, hi_port, samples_per_channel, rate,
                             scan_options, flags, data)

        try:
            while True:
                try:
                    status, transfer_status = dio_device.d_in_get_scan_status()

                    reset_cursor()
                    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 = ', '{:.6f}'.format(rate), 'Hz\n')

                    index = transfer_status.current_index
                    print('currentTotalCount = ',
                          transfer_status.current_total_count)
                    print('currentScanCount = ',
                          transfer_status.current_scan_count)
                    print('currentIndex = ', index, '\n')

                    for i in range(number_of_ports):
                        clear_eol()
                        print('port =', port_types[i].name, ': ',
                              '{:d}'.format(data[index + i]))

                    sleep(0.1)
                except (ValueError, NameError, SyntaxError):
                    break
        except KeyboardInterrupt:
            pass

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

    finally:
        if daq_device:
            # Stop the acquisition if it is still running.
            if status == ScanStatus.RUNNING:
                dio_device.d_in_scan_stop()
            if daq_device.is_connected():
                daq_device.disconnect()
            daq_device.release()
def main():
    daq_device = None

    interface_type = InterfaceType.USB
    descriptor_index = 0
    port_types_index = 0

    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 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 DAQ device does not support digital input')

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

        # 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_read = port_types[port_types_index]

        # Configure the port for input.
        port_info = dio_info.get_port_info(port_to_read)
        if (port_info.port_io_type == DigitalPortIoType.IO
                or port_info.port_io_type == DigitalPortIoType.BITIO):
            dio_device.d_config_port(port_to_read, DigitalDirection.INPUT)

        print('\n', descriptor.dev_string, ' ready', sep='')
        print('    Function demonstrated: dio_device.d_in()')
        print('    Port: ', port_to_read.name)
        try:
            input('\nHit ENTER to continue\n')
        except (NameError, SyntaxError):
            pass

        system('clear')

        try:
            while True:
                try:
                    reset_cursor()
                    print('Please enter CTRL + C to terminate the process\n')
                    print('Active DAQ device: ',
                          descriptor.dev_string,
                          ' (',
                          descriptor.unique_id,
                          ')\n',
                          sep='')

                    # Read the first port.
                    data = dio_device.d_in(port_to_read)

                    clear_eol()
                    print('Port(', port_to_read.name, ') Data: ', data)

                    sleep(0.1)
                except (ValueError, NameError, SyntaxError):
                    break
        except KeyboardInterrupt:
            pass

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

    finally:
        if daq_device:
            if daq_device.is_connected():
                daq_device.disconnect()
            daq_device.release()
Esempio n. 5
0
def main():
    # Parameters for DaqoDevice.daq_out_scan
    channel_descriptors = []
    samples_per_channel = 2000  # Two second buffer (sample_rate * 2)
    sample_rate = 1000  # Hz
    scan_options = ScanOption.CONTINUOUS
    scan_flags = DaqOutScanFlag.DEFAULT

    # Parameters used when creating channel_descriptors list
    analog_low_channel = 0
    analog_high_channel = 0
    analog_range_index = 0
    digital_low_port_index = 0
    digital_high_port_index = 0

    interface_type = InterfaceType.USB
    daq_device = None
    daqo_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 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])
        daqo_device = daq_device.get_daqo_device()

        # Verify the specified DAQ device supports DAQ output.
        if daqo_device is None:
            raise Exception(
                'Error: The DAQ device does not support DAQ output')

        daqo_info = daqo_device.get_info()

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

        # Configure supported analog input and digital input channels
        amplitudes = []
        samples_per_cycle = int(sample_rate / 10.0)  # 10 Hz sine wave
        supported_channel_types = daqo_info.get_channel_types()
        if DaqOutChanType.ANALOG in supported_channel_types:
            configure_analog_channels(daq_device, analog_low_channel,
                                      analog_high_channel, analog_range_index,
                                      channel_descriptors, amplitudes)
        if DaqOutChanType.DIGITAL in supported_channel_types:
            configure_digital_channels(daq_device, digital_low_port_index,
                                       digital_high_port_index,
                                       channel_descriptors, amplitudes)

        num_channels = len(channel_descriptors)

        # Create a buffer for output data.
        out_buffer = create_float_buffer(num_channels, samples_per_channel)
        # Fill the output buffer with data.
        create_output_data(channel_descriptors, samples_per_channel,
                           samples_per_cycle, amplitudes, out_buffer)

        print('\n', descriptor.dev_string, 'ready')
        print('    Function demonstrated: DaqoDevice.daq_out_scan')
        print('    Number of Scan Channels:', num_channels)
        for chan in range(num_channels):
            chan_descriptor = channel_descriptors[
                chan]  # type: DaqOutChanDescriptor
            print('        Scan Channel', chan, end='')
            print(': type =',
                  DaqOutChanType(chan_descriptor.type).name,
                  end='')
            if chan_descriptor.type == DaqOutChanType.ANALOG:
                print(', channel =', chan_descriptor.channel, end='')
                print(', range =', Range(chan_descriptor.range).name, end='')
            else:
                print(', port =',
                      DigitalPortType(chan_descriptor.channel).name,
                      end='')
            print('')
        print('    Samples per channel:', samples_per_channel)
        print('    Rate:', sample_rate, 'Hz')
        print('    Scan options:', display_scan_options(scan_options))
        try:
            input('\nHit ENTER to continue')
        except (NameError, SyntaxError):
            pass

        system('clear')

        # Start the output scan.
        sample_rate = daqo_device.daq_out_scan(channel_descriptors,
                                               samples_per_channel,
                                               sample_rate, scan_options,
                                               scan_flags, out_buffer)

        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 = daqo_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 daqo_device:
                daqo_device.scan_stop()
            # before disconnecting, set digital ports back to input
            dio_device = daq_device.get_dio_device()
            for chan in channel_descriptors:
                if chan.type == DaqOutChanType.DIGITAL:
                    dio_device.d_config_port(chan.channel,
                                             DigitalDirection.INPUT)
            # Disconnect from the DAQ device.
            if daq_device.is_connected():
                daq_device.disconnect()
            # Release the DAQ device resource.
            daq_device.release()