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


/// <summary>
/// Opens all channels on the modules included in the ChannelList, and closes only
/// the specified channels on a multiplexer or switch module.
/// </summary>
/// <remarks>
/// On the multiplexer modules, if any channel in a bank is defined to be part of
/// the scan list, attempting to close another channel (including Analog Bus channels)
/// within the same bank will result in an error. Channel closures in the other
/// bank are allowed as long as no channels are part of the scan list; however,
/// Analog Bus relay closures are not allowed.
/// </remarks>
/// <returns>
///	
/// </returns>
/// <param name="ChannelList">
///	One or more channels in the form sccc.
/// </param>
HRESULT Agilent34980A::IAgilent34980ARoute_CloseExclusive(BSTR ChannelList)
{
	HRESULT hr = S_OK;

	CorrectChannelList( ChannelList );

	hr = InstrPrintCommand();

	return hr;
}


/// <summary>
///	Enables/Disables automatic channel delay on the specified channel.
/// </summary>
/// <remarks>
///	If enabled, the delay is determined by the function, range, integration time,
/// and AC filter setting. Setting a specific channel delay disables the automatic
/// channel delay.
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980ARoute_get_AutoDelay(BSTR ChannelList, VARIANT_BOOL* 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("IAgilent34980ARoute_get_AutoDelay"), _T("ChannelList"));
 	}
	CorrectChannelList( ChannelList, false );
	hr = InstrQueryCommand(Val);

	return hr;
}


/// <summary>
///	Enables/Disables automatic channel delay on the specified channel.
/// </summary>
/// <remarks>
///	If enabled, the delay is determined by the function, range, integration time,
/// and AC filter setting. Setting a specific channel delay disables the automatic
/// channel delay.
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980ARoute_put_AutoDelay(BSTR ChannelList, VARIANT_BOOL Val)
{
	HRESULT hr = S_OK;

	CorrectChannelList( ChannelList );

	hr = InstrPrintCommand();

	return hr;
}


/// <summary>
///	Sets or gets the delay on a multiplexer channel
/// </summary>
/// <remarks>
///	The delay is inserted between the relay closure and the actual measurement on
/// the specified channel when that channel is in the scan list and a scanned measurement
/// occurs. Delays may range from 0 to 60 seconds, with 1E-3 second resolution.
///  The default channel delay is automatic.
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980ARoute_get_Delay(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("IAgilent34980ARoute_get_Delay"), _T("ChannelList"));
	}
	CorrectChannelList( ChannelList, false );
	hr = InstrQueryCommand(Val);

	return hr;
}


/// <summary>
///	Sets or gets the delay on a multiplexer channel
/// </summary>
/// <remarks>
///	The delay is inserted between the relay closure and the actual measurement on
/// the specified channel when that channel is in the scan list and a scanned measurement
/// occurs. Delays may range from 0 to 60 seconds, with 1E-3 second resolution.
///  The default channel delay is automatic.
/// </remarks>
/// <param name="ChannelList">
///	One or more channels in the form sccc. Must contain only 1 entry for the getter.
/// </param>
HRESULT Agilent34980A::IAgilent34980ARoute_put_Delay(BSTR ChannelList, double val)
{
	HRESULT hr = S_OK;

	CorrectChannelList( ChannelList );

	hr = InstrPrintCommand();

	return hr;
}

/// <summary>
///	Sets or gets the 4-wire external scanning configuration for the specified channels without the internal DMM. 
/// </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::IAgilent34980ARoute_get_FWire(BSTR ChannelList, VARIANT_BOOL* 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("IAgilent34980ARoute_get_FWire"), _T("ChannelList"));
 	}
	CorrectChannelList( ChannelList, false );
	hr = InstrQueryCommandBool(Val);

	return hr;
}

/// <summary>
///	Sets or gets the 4-wire external scanning configuration for the specified channels without the internal DMM. 
/// </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::IAgilent34980ARoute_put_FWire(BSTR ChannelList, VARIANT_BOOL Val)
{
	HRESULT hr = S_OK;

	CorrectChannelList( ChannelList );

	hr = InstrPrintCommand();

	return hr;
}


/// <summary>
///	Waits until all switch operations are done, then returns 1.
/// </summary>
/// <remarks>
///	
/// </remarks>
HRESULT Agilent34980A::IAgilent34980ARoute_get_Done(VARIANT_BOOL* Val)
{
	HRESULT hr = S_OK;

	CString sResp;
	hr = InstrQueryCommand(sResp, DEFAULT_STRING_LENGTH );
	sResp.Remove( '+' );
	if( sResp=='1' )
		*Val = VARIANT_TRUE;
	else if( sResp=='0' )
		*Val = VARIANT_FALSE;
	else return ReportError( IDS_E_IVI_UNEXPECTED_RESPONSE );

	return hr;
}


/// <summary>
/// Closes the specified channels on a multiplexer or switch module.
/// </summary>
/// <remarks>
/// On the multiplexer modules, if any channel in a bank is defined to be part of
/// the scan list, attempting to close another channel (including Analog Bus channels)
/// within the same bank will result in an error. Channel closures in the other
/// bank are allowed as long as no channels are part of the scan list; however,
/// Analog Bus relay closures are not allowed.
/// </remarks>
/// <returns>
///	
/// </returns>
/// <param name="ChannelList">
///	One or more channels in the form sccc.
/// </param>
HRESULT Agilent34980A::IAgilent34980ARoute_Close(BSTR ChannelList)
{
	HRESULT hr = S_OK;

	CorrectChannelList( ChannelList );

	hr = InstrPrintCommand();

	return hr;
}

/// <summary>
/// Opens the specified channels on a multiplexer or switch module.
/// </summary>
/// <remarks>
/// The RF Multiplexer modules (34941A, 34942A) and Microwave Switch modules (34946A,
/// 34947A) do not respond to the ROUTe:OPEN commands. To "open" a channel, send
/// the ROUTe:CLOSe command to another channel in the same bank.
/// </remarks>
/// <returns>
///	
/// </returns>
/// <param name="ChannelList">
///	One or more channels in the form sccc.
/// </param>
HRESULT Agilent34980A::IAgilent34980ARoute_Open(BSTR ChannelList)
{
	HRESULT hr = S_OK;

	CorrectChannelList( ChannelList );

	hr = InstrPrintCommand();

	return hr;
}

/// <summary>
///	Gets the closet state of the specified channel.
/// </summary>
/// <remarks>
///	
/// </remarks>
/// <param name="Channel">
///	A single channel in the form sccc.
/// </param>
HRESULT Agilent34980A::IAgilent34980ARoute_get_IsClosed(BSTR Channel, VARIANT_BOOL* Val)
{
	HRESULT hr = S_OK;

	CString sList(Channel);
	if( -1 != sList.Find( ',' ) || -1!=sList.Find( ':' ) || -1 != sList.Find( '-' ) )
	{
		return ReportInvalidValueError( _T("IAgilent34980ARoute_get_IsClosed"), _T("ChannelList"), sList );
	}
	hr = InstrQueryCommandBool(Val);

	return hr;
}

/// <summary>
/// Opens all channels on the specified slot.
/// </summary>
/// <remarks>
/// 
/// </remarks>
/// <returns>
///	
/// </returns>
/// <param name="Slot">
///	Specifies the slot on which all switches are to be opened. Allowable values
/// are 0 through 8, and in the case of 0 ALL switches in the mainframe are opened.
/// </param>
HRESULT Agilent34980A::IAgilent34980ARoute_OpenAll(long Slot)
{
	HRESULT hr = S_OK;
	if( Slot<0 || Slot>8 )
		return ReportInvalidValueError( _T("IAgilent34980ARoute_OpenAll"), _T("Slot"), Slot );

	if( Slot==0 )
		hr = InstrPrintf( "ROUT:OPEN:ALL ALL" );
	else
		hr = InstrPrintCommand();

	return hr;
}
