Python for Windows program returns incorrect value.


Recently we had a customer that was having a problem with the a_in.py and v_in.py examples. They attached a single-ended 5 volt signal to channel 0 on a USB-1408FS-PLUS. The example program returned an incorrect value of ~3.6 volts. The customer verified that InstaCal returned the correct result. So what gives?

The reason why the a_in.py and v_in.py programs are not reading the input correctly, is because the inputs are set to differential mode, which means one side of the input was left open. How could this be? It's because the example uses the discovery interface to connect to the device, which ignores how you have it set in InstaCal. Therefore, when the board was discovered by default it was in differential mode. 
 
Below is the modified a_in.py example that sets the inputs to single-ended. Also added was the ULRange enum to specify a voltage range. Below is the program listing and the changes have been highlighted.
 

 
"""
File:                       a_in.py
 
Library Call Demonstrated:  mcculw.ul.a_in() or mcculw.ul.a_in_32()
 
Purpose:                    Reads an A/D Input Channel.
 
Demonstration:              Displays the analog input on a user-specified
                            channel.
 
Other Library Calls:        mcculw.ul.to_eng_units()
                                or mcculw.ul.to_eng_units_32()
                            mcculw.ul.release_daq_device()
 
Special Requirements:       Device must have an A/D converter.
                            Analog signal on an input channel.
"""
from __future__ import absolute_import, division, print_function
from builtins import *  # @UnusedWildImport
 
from mcculw import ul
from mcculw.device_info import DaqDeviceInfo
 
# added the following imports
from mcculw.enums import ULRange
from mcculw.enums import AnalogInputMode
from mcculw.ul import ULError, a_input_mode
 
try:
    from console_examples_util import config_first_detected_device
except ImportError:
    from .console_examples_util import config_first_detected_device
 
 
def run_example():
    # By default, the example detects and displays all available devices and
    # selects the first device listed. Use the dev_id_list variable to filter
    # detected devices by device ID (see UL documentation for device IDs).
    # If use_device_detection is set to False, the board_num variable needs to
    # match the desired board number configured with Instacal.
    use_device_detection = True
    dev_id_list = []
    board_num = 0
 
    try:
        if use_device_detection:
            config_first_detected_device(board_num, dev_id_list)
 
        daq_dev_info = DaqDeviceInfo(board_num)
        if not daq_dev_info.supports_analog_input:
            raise Exception('Error: The DAQ device does not support '
                            'analog input')
 
        print('\nActive DAQ device: ', daq_dev_info.product_name, ' (',
              daq_dev_info.unique_id, ')\n', sep='')
 
        ai_info = daq_dev_info.get_ai_info()
        ai_range = ai_info.supported_ranges[0]
        channel = 0
        # ********* add this function
        a_input_mode(board_num, AnalogInputMode.SINGLE_ENDED)
 
        # Get a value from the device
        if ai_info.resolution <= 16:
            # Use the a_in method for devices with a resolution <= 16
            # *************** added ULRange.BIP10VOLTS **********
            value = ul.a_in(board_num, channel, ULRange.BIP10VOLTS)
            # Convert the raw value to engineering units
            # *************** added ULRange.BIP10VOLTS **********
            eng_units_value = ul.to_eng_units(board_num, ULRange.BIP10VOLTS, value)
        else:
            # Use the a_in_32 method for devices with a resolution > 16
            # (optional parameter omitted)
            value = ul.a_in_32(board_num, channel, ai_range)
            # Convert the raw value to engineering units
            eng_units_value = ul.to_eng_units_32(board_num, ai_range, value)
 
        # Display the raw value
        print('Raw Value:', value)
        # Display the engineering value
        print('Engineering Value: {:.3f}'.format(eng_units_value))
    except Exception as e:
        print('\n', e)
    finally:
        if use_device_detection:
            ul.release_daq_device(board_num)
 
 
if __name__ == '__main__':
    run_example()


Posted 5/26/2021 9:55:35 AM by Administrator
https://kb.mccdaq.com/KnowledgebaseArticle50842.aspx