/******************************************************************************
 *                                                                         
 *               Copyright 2004-2006 Agilent Technologies
 *               All rights reserved.
 *
 *****************************************************************************/
 
#include "stdafx.h"
#include "CoAgilent34980A.h"

/// <summary>
/// Takes a measurement value obtained from one of the Read or Fetch methods and
/// determines if the value is a valid measurement value or a value indicating that
/// an overrange condition occurred.
/// </summary>
/// <remarks>
/// 
/// </remarks>
/// <returns>
///	True if the measurement value indicates an overrange condition and false otherwise.
/// </returns>
/// <param name="MeasurementValue">
///	The measurement value from one of the Read or Fetch functions.
/// </param>
HRESULT Agilent34980A::IIviDmmMeasurement_IsOverRange(double MeasurementValue, VARIANT_BOOL* Val)
{
	HRESULT hr = S_OK;

	/*No SCPI --- +1.0E+38 = Positive 	overload,  
	-1.0E+38 = Negative overload
	*/
	if(!Val) return E_POINTER;
	if(MeasurementValue >= +1.0E+38 || MeasurementValue <= -1.0E+38)
		*Val = VARIANT_TRUE;
	else
		*Val = VARIANT_FALSE;
	return hr;
}

/// <summary>
/// Sends a software trigger, which causes the DMM to take a measurement.
/// </summary>
/// <remarks>
/// 
/// </remarks>
/// <returns>
///	
/// </returns>
HRESULT Agilent34980A::IIviDmmMeasurement_SendSoftwareTrigger()
{
	HRESULT hr = S_OK;

	hr = IAgilent34980AMeasurement_SendSoftwareTrigger();

	return hr;
}

/// <summary>
/// Initiates a measurement, waits for the DMM to return to the idle state, and
/// returns an array of values.  If an overrange condition occurs, the corresponding
/// element of the array contains IEEE NaN and the method returns an overrange error.
/// </summary>
/// <remarks>
/// 
/// </remarks>
/// <returns>
///	Array of measured values.
/// </returns>
/// <param name="MaxTimeMilliseconds">
///	The maximum time allowed for the measurement to complete in milliseconds. May
/// also be a value defined in IviDmmMaxTimeEnum.
/// </param>
HRESULT Agilent34980A::IIviDmmMeasurement_ReadMultiPoint(long MaxTimeMilliseconds, SAFEARRAY** Val)
{
	HRESULT hr = S_OK;

	/*	READ is always multi-point unless user has only taken a single reading.
	use "DATA:POINts?" to tell if you need to use the multi-point or the single point fetch based on return value.	*/
	long nTimeout;
	
	GetIOTimeout( &nTimeout );
	SetIOTimeout( MaxTimeMilliseconds );
	PushInstrCommand( "READ?" );
	hr = IAgilent34980AMeasurement_ReadNumbersOnly( _bstr_t(""), Val );
	PopInstrCommand();
	
	SetIOTimeout( nTimeout );
	
	return hr;
}

/// <summary>
/// Initiates a measurement, waits for the DMM to return to the idle state, and
/// returns the measured value.  If an overrange condition occurrs, the value contains
/// IEEE NaN and the method returns an overrange error.
/// </summary>
/// <remarks>
/// 
/// </remarks>
/// <returns>
///	Measurement value.
/// </returns>
/// <param name="MaxTimeMilliseconds">
///	The maximum time allowed for the measurement to complete in milliseconds. May
/// also be a value defined in IviDmmMaxTimeEnum.
/// </param>
HRESULT Agilent34980A::IIviDmmMeasurement_Read(long MaxTimeMilliseconds, double* Val)
{
	HRESULT hr = S_OK;
	CString sResp;

	hr = InstrQuery( "READ?", sResp, DEFAULT_STRING_LENGTH, MaxTimeMilliseconds );
	sResp.Remove( '\n' );
	if( FAILED(hr) || sResp=="" )
		return hr;

	int nOffset = 0;
	if( sResp[0]=='#' )
		nOffset  = (sResp[1]-'0') + 2; 

	*Val = atof( (LPCTSTR)sResp+nOffset );

	return hr;
}

/// <summary>
/// Returns an array of values from a measurement that the Initiate method initiates.
///  If an overrange condition occurs, the corresponding element of the array contains
/// IEEE NaN and the method returns an overrange error.
/// </summary>
/// <remarks>
/// 
/// </remarks>
/// <returns>
///	Array of measured values.
/// </returns>
/// <param name="MaxTimeMilliseconds">
///	The maximum time allowed for the measurement to complete in milliseconds. May
/// also be a value defined in IviDmmMaxTimeEnum.
/// </param>
HRESULT Agilent34980A::IIviDmmMeasurement_FetchMultiPoint(long MaxTimeMilliseconds, SAFEARRAY** Val)
{
	HRESULT hr = S_OK;

	long nTimeout;

	GetIOTimeout( &nTimeout );
	SetIOTimeout( MaxTimeMilliseconds );

	PushInstrCommand( "FETC?" );
	hr = IAgilent34980AMeasurement_FetchNumbersOnly( Val );
	PopInstrCommand();
	
	SetIOTimeout( nTimeout );

	return hr;
}

/// <summary>
/// Returns the measured value from a measurement that the Initiate method initiates.
///  If an overrange condition occurs, the value contains IEEE NaN and the method
/// returns an overrange error.
/// </summary>
/// <remarks>
/// 
/// </remarks>
/// <returns>
///	Measurement value.
/// </returns>
/// <param name="MaxTimeMilliseconds">
///	The maximum time allowed for the measurement to complete in milliseconds. May
/// also be a value defined in IviDmmMaxTimeEnum.
/// </param>
HRESULT Agilent34980A::IIviDmmMeasurement_Fetch(long MaxTimeMilliseconds, double* Val)
{
	HRESULT hr = S_OK;
	CString sResp;

	hr = InstrQuery( "FETC?", sResp, DEFAULT_STRING_LENGTH, MaxTimeMilliseconds );
	sResp.Remove( '\n' );
	if( FAILED(hr) || sResp=="" )
		return hr;

	*Val = atof( (LPCTSTR)sResp );

	return hr;
}

/// <summary>
/// Aborts a previously initiated measurement and returns the DMM to the idle state.
/// </summary>
/// <remarks>
/// 
/// </remarks>
/// <returns>
///	
/// </returns>
HRESULT Agilent34980A::IIviDmmMeasurement_Abort()
{
	HRESULT hr = S_OK;

	hr = IAgilent34980AMeasurement_Abort();
	
	return hr;
}

/// <summary>
/// Initiates a measurement. When this method executes, the DMM leaves the idle
/// state and waits for a trigger.
/// </summary>
/// <remarks>
/// 
/// </remarks>
/// <returns>
///	
/// </returns>
HRESULT Agilent34980A::IIviDmmMeasurement_Initiate()
{
	HRESULT hr = S_OK;

	hr = IAgilent34980AMeasurement_Initiate();

	return hr;
}


