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



/// <summary>
/// Configures the specified channels for AC voltage measurements but does not initiate
/// the scan.
/// </summary>
/// <remarks>
/// 
/// </remarks>
/// <returns>
///	
/// </returns>
/// <param name="ChannelList">
///	Specifies the list of channels to be configured.
/// </param>
/// <param name="Range">
///	Enter the expected signal value.  Actual ranges available on the instrument
/// are 0.1, 1.0, 10, 100, and 300 Volts. 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::IAgilent34980AACVoltage_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( "VOLT:AC:RANG:AUTO 1, (@%s)", sList );
		else
			hr = InstrPrintf( "VOLT:AC:RANG:AUTO 1" );
	}


	return hr;
}

/// <summary>
///	Enables/Disables autoranging on the specified channel.
/// </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::IAgilent34980AACVoltage_get_AutoRange(BSTR ChannelList, VARIANT_BOOL* Val)
{
	HRESULT hr = S_OK;

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

	return hr;
}

/// <summary>
///	Enables/Disables autoranging on the specified channel.
/// </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::IAgilent34980AACVoltage_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::IAgilent34980AACVoltage_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("IAgilent34980AACVoltage_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::IAgilent34980AACVoltage_put_Bandwidth(BSTR ChannelList, double val)
{
	HRESULT hr = S_OK;

	CorrectChannelList( ChannelList );
	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.
/// The following ranges are available: 100 mV (MIN), 1 V, 10 V, 100 V, 300 V (MAX).
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980AACVoltage_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("IAgilent34980AACVoltage_get_Range"), _T("ChannelList"));
	}
	sList = CorrectChannelList( ChannelList, false );
	RETURN_IF_FAIL( InstrQueryCommand(Val) );
	long lAuto;
	if( ::SysStringLen( ChannelList ) )
		hr = InstrQueryf( "VOLT:AC:RANG:AUTO? (@%s)", &lAuto, sList );
	else
		hr = InstrQuery( "VOLT: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.
/// The following ranges are available: 100 mV (MIN), 1 V, 10 V, 100 V, 300 V (MAX).
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980AACVoltage_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( "VOLT:AC:RANG:AUTO 1, (@%s)", sList );
		else
			hr = InstrPrintf( "VOLT: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 voltage 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.
/// </remarks>
/// <returns>
///	
/// </returns>
/// <param name="ChannelList">
///	One or more channels in the form sccc.
/// </param>
/// <param name="Range">
/// Enter the expected signal value.  Actual ranges available on the instrument
/// are 0.1, 1.0, 10, 100, and 300 Volts.  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::IAgilent34980AACVoltage_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:VOLT: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;
}
