//Example of using the Multifunction card 34952A

// """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
//  © Agilent Technologies Inc. 2004, 2005, 2006
//  All rights reserved.
//
// You have a royalty-free right to use, modify, reproduce and distribute
// the Sample Application Files (and/or any modified version) in any way
// you find useful, provided that you agree that Agilent Technologies has no
// warranty, obligations or liability for any Sample Application Files.
//
// Agilent Technologies provides programming examples for illustration only,
// This sample program assumes that you are familiar with the programming
// language being demonstrated and the tools used to create and debug
// procedures. Agilent Technologies support engineers can help explain the
// functionality of Agilent Technologies software components and associated
// commands, but they will not modify these samples to provide added
// functionality or construct procedures to meet your specific needs.
// """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


// NOTE: This include MUST be first, else will not compile cleanly
#include "stdafx.h"

#include "ag34980a.h"
// NOTE: The above include (ag34980A.h) needs some IVI shared component includes.  The path to these
// is often something like "C:\Program Files\IVI\Include".  You need to add this to your includes path at
// either the project level or the system level.  For project level, select Tools\Options, then click
// on "Projects" file tab, select "VC++ Directories".  This brings up a dialog box.  In the upper
// right of this dialog, there is a "Show Directories for" drop down -- select Include files. Then 
// input the include path desired in the window beneath.  You may also need to add an include path
// for Visa.h (which is often in C:\Program Files\Visa\winnt\include).
//
// NOTE: If the above include directory "C:\Program Files\IVI\Include" does not contain the IVI shared files
// (ivi.h, iviDmm.h, iviSwtch.h), then you must get them from the NI web site (www.ni.com).  Once there,
// select the "Support" tab in the upper portion of the screen, this will bring up a new window.  In the
// upper part of that window, do a "Search" for "IVI Compliance" by placing the string "IVI Compliance" in
// the text box just to the left of the "Go" button.  On the web page that comes up, scroll down
// and select the most recent version of "IVI Compliance Version....." for your operating system.  Downloading 
// this will place the appropriate .h files into the include directory mentioned above.

ViSession vi;  // needs to be global so can print errors out

void printErrs(ViStatus s)
{
	char errMssg[300];
	ViInt32 errCode = -1;
	ViStatus s2;

	s2 = ag34980a_GetError(vi, &errCode, (ViInt32)300, errMssg);
	printf("Got Visa error: %ld, %s\n", errCode, errMssg);
	if (errCode = -1074135039)  // instr has errors in its queue
	{
		// print out the instr specific errors
		while ((s2 >= 0) && (errCode != 0))
		{
			s2 = ag34980a_error_query(vi, &errCode, errMssg);
			if ( (s2 > -1) && (errCode != 0) )
				printf("Instr reported err: %ld, %s\n", errCode, errMssg);
		}
	}
}

#define	CHECK(func)	\
do {\
    ViStatus _s = (func);\
    if (_s < 0)\
    {\
	printf("Error calling function: %s\n", #func);\
	printErrs(_s);\
    }\
} while (0)
 // Uncomment and add the following two lines before the "while" above if you wish to exit when
 // you detect an error.
 //	_s = ag34980a_close(vi);\
 //   exit(0);\

int _tmain(int argc, _TCHAR* argv[])
{
//Open a session to the mainframe using the IVI-C host driver, Reset the instrument
	ViSession vi;
	ViStatus viStatus;
	char respStr[256];
	char byteResp;
	int  index;
	ViReal64 real64Resp;


	viStatus = ag34980a_init( "TCPIP0::156.140.92.93::INSTR", VI_FALSE, VI_TRUE, &vi );
	
	if (viStatus < 0)
	{
		fprintf(stderr,"Failed to open the instrument.");
		exit(0);
	}

	//Create buffer to hold error responses
	ViInt32 nCount = 0;    // holds return count

	// Get card type of slot 1
	CHECK(ag34980a_GetCardType(vi, 1, 256,respStr,&nCount));
	printf("Card type was: %s\n", respStr);

//	Configure channels 1 and 2 as output, and channel 3 as Input	
//	viStatus = ag34980a_ConfigureDigital34952A (vi, "1001", AG34980A_VAL_CHANNEL_WIDTH_WORD,
//                                      AG34980A_VAL_DIRECTION_OUTPUT);

	CHECK(ag34980a_SetAttributeViInt32(vi, "1001", AG34980A_ATTR_DIGITAL_WIDTH ,
		AG34980A_VAL_CHANNEL_WIDTH_WORD) ); 

	CHECK(ag34980a_SetAttributeViInt32(vi, "1001", AG34980A_ATTR_DIGITAL_DIRECTION,
		AG34980A_VAL_DIRECTION_OUTPUT) ); 


//	viStatus = ag34980a_ConfigureDigital34952A (vi, "1003", AG34980A_VAL_CHANNEL_WIDTH_BYTE,
//                                      AG34980A_VAL_DIRECTION_INPUT);

	CHECK(ag34980a_SetAttributeViInt32(vi, "1003", AG34980A_ATTR_DIGITAL_WIDTH ,
		AG34980A_VAL_CHANNEL_WIDTH_BYTE) ); 

	CHECK(ag34980a_SetAttributeViInt32(vi, "1003", AG34980A_ATTR_DIGITAL_DIRECTION,
		AG34980A_VAL_DIRECTION_INPUT) ); 

//	Write 6255 as Word to channels 1 and 2
	CHECK(ag34980a_WriteWord (vi, "1001", 6255));
//	Write Byte of 85 to channel 2 
	CHECK(ag34980a_WriteByte (vi, "1002", 85));
//	Write Byte of 70 to channel 1 
	CHECK(ag34980a_WriteByte (vi, "1001", 70));
//  Read byte on channel 3 (input chan)
	CHECK(ag34980a_ReadByte(vi, "1003", &byteResp));
	printf("Byte response from channel 1003 was: %x\n", byteResp);

//  Output 4.0 Volts on DAC (channel 6)
//  In our example, we wired this channel to the Gate input of the totalizer to enable it
	viStatus = ag34980a_ConfigureSourceVoltage(vi, "1006",4.0);
                                            
//  Now lets configure and operate the totalizer (note that we enabled the gate via the DAC above)
    //
    // Totalizer configuration
    //
	CHECK(ag34980a_ConfigureTotalizer34952A(vi,"1005",AG34980A_VAL_TOTALIZE_TYPE_READ,
		AG34980A_VAL_TOTALIZE_THRESHOLD_MODE_AC,
        AG34980A_VAL_TOTALIZE_SLOPE_POSITIVE));

// clear any counts that may be on the totalizer
	CHECK(ag34980a_ClearTotal34952A( vi, "1005"));

    //
    // Read totalizer, if you have nothing connected it will read 0
    //
    for (index=0; index<=5; index++)
    {
        CHECK(ag34980a_ReadTotalizerData34952A(vi, "1005", &real64Resp));
        printf ("Totalizer reading %d: %ld\n", index, (long)real64Resp);
    }
                              
errExit: //  Exit and close session
	viStatus = ag34980a_close(vi);

	printf("Done...Press Enter to exit: \n");
	getchar();

	return 0;
}

