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

/// <summary>
///	Enables/Disables autoranging on the specified channels.
/// </summary>
/// <remarks>
///	
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980AACCurrent_get_AutoRange(BSTR ChannelList, VARIANT_BOOL* Val)
{
	HRESULT hr = S_OK;

	CString sList(ChannelList);
	if( -1 != sList.Find( ',' ) || -1!=sList.Find( ':' ) || -1 != sList.Find( '-' ) ) // a , means more than 1 channel
	{
		return ReportError( E_AGILENT34980A_MULTIPLE_CHANNELS_IN_GET, _T("IAgilent34980AACCurrent_get_AutoRange"), _T("ChannelList"));
	}
	CorrectChannelList( ChannelList, false );
	hr = InstrQueryCommandBool(Val);

	return hr;
}

/// <summary>
///	Enables/Disables autoranging on the specified channels.
/// </summary>
/// <remarks>
///	
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980AACCurrent_put_AutoRange(BSTR ChannelList, VARIANT_BOOL Val)
{
	HRESULT hr = S_OK;

	CorrectChannelList( ChannelList );
	hr = InstrPrintCommand();

	return hr;
}


/// <summary>
///	Sets or gets the AC bandwidth.
/// </summary>
/// <remarks>
///	The instrument uses three different ac filters which enable you to either optimize
/// low frequency accuracy or achieve faster ac settling times. The instrument selects
/// the slow (3 Hz), medium (20 Hz), or fast (200 Hz) filter based on the frequency
/// value that you specify with this command. If you omit the optional ChannelList
/// parameter, this command applies to the internal DMM, independent of any channels
/// or a scan list.
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980AACCurrent_get_Bandwidth(BSTR ChannelList, double* Val)
{
	HRESULT hr = S_OK;

	CString sList(ChannelList);
	if( -1 != sList.Find( ',' ) || -1!=sList.Find( ':' ) || -1 != sList.Find( '-' ) )
	{
		return ReportError( E_AGILENT34980A_MULTIPLE_CHANNELS_IN_GET, _T("IAgilent34980AACCurrent_get_Bandwidth"), _T("ChannelList"));
	}
	CorrectChannelList( ChannelList, false );
	hr = InstrQueryCommand(Val);

	return hr;
}


/// <summary>
///	Sets or gets the AC bandwidth.
/// </summary>
/// <remarks>
///	The instrument uses three different ac filters which enable you to either optimize
/// low frequency accuracy or achieve faster ac settling times. The instrument selects
/// the slow (3 Hz), medium (20 Hz), or fast (200 Hz) filter based on the frequency
/// value that you specify with this command. If you omit the optional ChannelList
/// parameter, this command applies to the internal DMM, independent of any channels
/// or a scan list.
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980AACCurrent_put_Bandwidth(BSTR ChannelList, double val)
{
	HRESULT hr = S_OK;

	CorrectChannelList( ChannelList, false );
	hr = InstrPrintCommand();

	return hr;
}

/// <summary>
///	Selects the measurement range on the specified channel.
/// </summary>
/// <remarks>
///	If the ChannelList parameter is empty, this command applies to the internal
/// DMM, independent of any channels or a scan list.\r\n
/// The following ranges are available: 10 mA (MIN), 100 mA, 1 A (MAX).
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980AACCurrent_get_Range(BSTR ChannelList, double* Val)
{
	HRESULT hr = S_OK;

	CString sList(ChannelList);
	if( -1 != sList.Find( ',' ) || -1!=sList.Find( ':' ) || -1 != sList.Find( '-' ) ) 
	{
		return ReportError( E_AGILENT34980A_MULTIPLE_CHANNELS_IN_GET, _T("IAgilent34980AACCurrent_get_Range"), _T("ChannelList"));
	}
	sList = CorrectChannelList( ChannelList, false );

	RETURN_IF_FAIL( InstrQueryCommand(Val) );
	long lAuto;
	if( ::SysStringLen( ChannelList ) )
		hr = InstrQueryf( "CURR:AC:RANG:AUTO? (@%s)", &lAuto, sList );
	else
		hr = InstrQuery( "CURR:AC:RANG:AUTO?", &lAuto );

	if( lAuto )
		*Val = -(*Val);
	return hr;
}

/// <summary>
///	Selects the measurement range on the specified channel.
/// </summary>
/// <remarks>
///	If the ChannelList parameter is empty, this command applies to the internal
/// DMM, independent of any channels or a scan list.\r\n
/// The following ranges are available: 10 mA (MIN), 100 mA, 1 A (MAX).
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980AACCurrent_put_Range(BSTR ChannelList, double Val)
{
	HRESULT hr = S_OK;

	CString sList = CorrectChannelList( ChannelList );
	RETURN_IF_FAIL( InstrPrintCommand() );
	if( Val<0 )
	{
		if( ::SysStringLen( ChannelList ) )
			hr = InstrPrintf( "CURR:AC:RANG:AUTO 1, (@%s)", sList );
		else
			hr = InstrPrintf( "CURR:AC:RANG:AUTO 1" );
	}
	return hr;
}




/// <summary>
/// Resets all measurement parameters on the specified channels to their default
///  values.    Then, configures the channels for ac current measurements but does
/// not initiate  the scan.
/// </summary>
/// <remarks>
/// If you supply an empty ChannelList, this method applies to the internal DMM,
///  independent of any channels or a scan list.
/// </remarks>
/// <returns>
///	
/// </returns>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Current measurements are allowed only
///  on channels 41 through 44 on the 34921A.
/// </param>
/// <param name="Range">
///	Enter the expected signal value.  Actual ranges available on the instrument
/// are 0.010, 0.100, and 1.0 Amps. If the range value specified is negative, AUTO
/// ranging will be enabled.
/// </param>
/// <param name="Resolution">
///	Select from Least (4.5 digits), Default (5.5 digits) and Best (6.5 digits).
/// </param>
HRESULT Agilent34980A::IAgilent34980AACCurrent_Configure(BSTR ChannelList, double Range, Agilent34980AResolutionEnum Resolution)
{
	HRESULT hr = S_OK;

	CString sList = CorrectChannelList( ChannelList );
	hr = InstrPrintCommand();
	if( Range<0 )
	{
		if( ::SysStringLen( ChannelList ) )
			hr = InstrPrintf( "CURR:AC:RANG:AUTO 1, (@%s)", sList );
		else
			hr = InstrPrintf( "CURR:AC:RANG:AUTO 1" );
	}

	return hr;
}




/// <summary>
/// Resets all measurement parameters on the specified channels to their default
///  values.   	Then, configures the channels for ac current measurements and immediately
/// sweeps  through the specified channels one time.
/// </summary>
/// <remarks>
/// The results are sent directly to the instrument's output buffer. If you supply
/// an empty ChannelList, this method applies to the internal DMM,  independent
/// of any channels or a scan list. Used with 34921A Armature Multiplexer (channels
/// 41 through 44 only)
/// </remarks>
/// <returns>
///	
/// </returns>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Current measurements are allowed only
///  on channels 41 through 44 on the 34921A.
/// </param>
/// <param name="Range">
///	Enter the expected signal value.  Actual ranges available on the instrument
/// are 0.010, 0.100, and 1.0 Amps. If the range value specified is negative, AUTO
/// ranging will be enabled.
/// </param>
/// <param name="Resolution">
///	Select from Least (4.5 digits), DEFault (5.5 digits) and Best (6.5 digits).
/// </param>
HRESULT Agilent34980A::IAgilent34980AACCurrent_Measure(BSTR ChannelList, double Range, Agilent34980AResolutionEnum Resolution, SAFEARRAY** val)
{
	HRESULT hr = S_OK;

	CString sList = CorrectChannelList( ChannelList );

	if( Range<0 )
	{
		// prepare new command for AUTO ranging
		PopInstrCommand();
		CString sCmd;
		sCmd.Format("MEAS:CURR:AC? AUTO, %s, (@%s)", EnumToString<Agilent34980AResolutionEnum>(Resolution), sList );
		PushInstrCommand( sCmd ); // pop is in ForwardingShims.h
	}
	RETURN_IF_FAIL( InstrPrintCommandNoPoll() );
	hr = FormattedReadList( NtlAsciiType_R8, ",", val );

	return hr;
}
