Search code examples
python-3.xmeasurementdaq-mx

DAQ USB-1208LS Python 3.4


Need some help here, I have a DAQ Measurement Computing USB-1208LS and I need some idea on how to control the DAQ.

I have installed UniversalLibrary but I can find any example. The idea is very simple, I need to read 5 different voltages, 3.3v 5.0v 7.50v, 10.10v and 12.00v.

Thanks all for you help.


Solution

  • This can be done by interfacing to the UniversalLibrary through the ctypes module.

    I don't have a DAQ handy to make sure this works, but this is dug from some old DAQ code I have and demonstrates basic control of the 1208LS.

    # Pulled from some old 2.6 code
    from ctypes import *  # Old code, bad import
    from time import sleep
    
    # DAQ configuration parameters
    # Check the Universal Library documentation for your DAQ
    BOARD = 0
    LOW_CHANNEL = 0
    HIGH_CHANNEL = 0
    GAIN = 15  # This samples in the +-20 volt range
    RATE = 100  # Samples per second
    SAMPLES = 100  # Number of samples to record
    OPTIONS = 1  # Run the DAQ in background mode so it isn't blocking
    
    # Load the DAQ DLL
    mcdaq = windll.LoadLibrary("cbw32")
    
    # Initialize memory handle where the DAQ will pass data
    mem_handle = mcdaq.cbWinBufAlloc(SAMPLES)
    
    # The DAQ will update this with the actual rate used
    actual_rate = c_long(RATE)
    
    # Start the sampling
    start_status = mcdaq.cbAInScan(BOARD, LOW_CHANNEL, HIGH_CHANNEL, SAMPLES,
                                   byref(actual_rate), GAIN, mem_handle, OPTIONS)
    if start_status != 0:
        # An error occured starting the DAQ
        exit(1)
    
    sleep(2)  # Do other stuff while the DAQ is recording
    
    # Make sure the scan is stopped and check the scan status
    status = mcdaq.cbStopIOBackground(BOARD, 1)
    if status != 0:
        # An error occured, check documentation for the specific status code
        pass
    
    # Now pull the data
    # Check the status of the DAQ and find out how many samples have been recorded
    stat = c_int()
    current_count = c_long()  # Count of how many samples to retrieve from memory
    current_index = c_long()
    s = mcdaq.cbGetIOStatus(BOARD, byref(stat), byref(current_count),
                            byref(current_index), 1)
    # Make an array to hold the data
    sample_array_type = c_ushort * current_count.value
    data_array = sample_array_type()
    
    # Retrieve data from DAQ memory
    status = mcdaq.cbWinBufToArray(mem_handle, byref(data_array), 0,
                                   current_count.value)
    # Values in data_array will be in DAQ counts
    

    For full documentation on enum values and function signatures read the Universal Library documentation.