FILE : GxCntExampleC.cpp
PURPOSE : WIN32/LINUX example program for GX2200 boards
using the GXCNT driver.
CREATED : Mar 2002-2011
COPYRIGHT : Copyright 2002-2011 MARVIN TEST SOLUTIONS
COMMENTS :
To compile the example:
1. Microsoft VC++
Load GxCntExampleC.dsp, .vcproj or .mak, depends on
the VC++ version from the Project\File/Open... menu
Select Project/Rebuild all from the menu
2. Borland C++ Builder
Load GxCntExampleC.bpr from the Project/Open
Project... menu
Select Project/Build all from the menu
3. Linux (GCC for CPP and Make must be available)
make -fGxCntExampleC.mk [CFG=Release[64] | Debug[64]] [rebuild | clean]
**********************************************************/
#ifndef __GNUC__
#include "windows.h"
#endif
#include "GxCnt.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#if defined(__BORLANDC__)
#pragma hdrstop
#include <condefs.h>
USELIB("GxCntBC.lib");
USERC("GxCntExampleC.rc");
#endif
//*********************************************************
// DisplayMsg
//*********************************************************
void DisplayMsg(PCSTR lpszMsg)
{
#ifndef __GNUC__
MessageBeep(0);
MessageBox(0, lpszMsg, "GxCnt example program", MB_OK);
#else
printf("\r\nGxCnt example program: %s\r\n", lpszMsg);
#endif
return;
}
//*********************************************************
//__strupr
//*********************************************************
char * __strupr(char * sz)
{
int i;
for (i=0; sz[i]; i++)
sz[i] = toupper(sz[i]);
return sz;
}
//*********************************************************
// DisplayUsage
//*********************************************************
void DisplayUsage(void)
{
DisplayMsg(
"This example shows how to use the GX2200:\r\n"
"Usage:\r\n"
"GxCntExampleC <slot|address> <channel|measure|summary>
<operation> <mode|level>"
"\r\n\r\nWhere :"
"<slot> - Under Windows: PCI/PXI slot number as shown by the PXI
explorer\r\n"
"<address> - Under Linux: Bus/device as displayed with lspci
utility\r\n"
"<channel|measure|summary>:\r\n"
"\tSCHA=set channel A function, trigger level or trigger
mode\r\n"
"\tSCHB=set channel B function, trigger level or trigger
mode\r\n"
"\tMEASURE=Read single measurment\r\n"
"\tSUM=Print board summary\r\n"
"<operation> - one of the followings:\r\n"
"\tFREQ=Sets measurment function to frequency\r\n"
"\tPERIOD=Sets measurment function to period\r\n"
"\tTRIG_MODE=Sets the trigger level mode\r\n"
"\tTRIG_LEVEL=Sets the trigger level\r\n"
"<mode|level>- trigger mode or level:\r\n"
"\t<mode>:\t0=trigger level set by user\r\n"
"\t\t1=trigger level set automatically\r\n"
"\t\t2=trigger level uses the last value\r\n"
"\t<level>:\tvoltage level (-5.0v to +5.0v)\r\n"
"\r\nThe example should be run from the Windows command
prompt/Linux terminal.\r\n"
"\tExamples:\r\n"
"\t./GxCntExampleC 7 SCHA FREQ 1\r\n"
"\t./GxCntExampleC 7 MEASURE (under Windows, slot 7)\r\n"
"\t./GxCntExampleC 0x60c MEASURE (under Linux, bus 6 device 12)\r\n"
);
exit(1);
}
//*********************************************************
// CheckStatus
//*********************************************************
void CheckStatus(SHORT nStatus)
{
char sz[512];
if (!nStatus) return;
GxCntGetErrorString(nStatus, sz, sizeof sz, &nStatus);
DisplayMsg(sz);
DisplayMsg("Aborting the program...");
exit(nStatus);
}
//*********************************************************
// MAIN
//
// This main function receives the following parameters
//
// GX2200 GXCNT board slot number (e.g. 1)
// <channel|measure>:
// SCHA=set channel A function, trigger level or mode
// SCHB=set channel B function, trigger level or mode
// MEASURE=Read single measurment
// SUM=print board summary
//
// <operation> - one of the followings:
// FREQ=Sets measurment function to frequency
// PERIOD=Sets measurment function to period
// TRIG_MODE=Sets the trigger level mode
// TRIG_LEVEL=Sets the trigger level
//
// <mode|level>- trigger mode or level:\r\n"
// <mode>: 0=trigger level set by user\r\n"
// 1=trigger level set automatically\r\n"
// 2=trigger level uses the last value\r\n"
// <level>: voltage level (-5.0v to +5.0v)\r\n"
//
//*********************************************************
int main(int argc, char ** argv)
{
short nSlotNum; // Board slot number
char * szChannelOrMeasure; // Channel or Measure operation
char * szOperation; // Board Operation
short nHandle; // Board handle
short nStatus; // Returned status
short nChannel; // Channel number
short nTriggerMode; // Trigger Mode
short nReadTriggerMode; // Read Trigger Mode
double dTriggerLevel; // Trigger Level Voltage
double dMeasure; // Measurement
char sz[512]; // Board Summary
// Check minimum number of arguments recived
if (argc<3) DisplayUsage();
// Parse command line parameters
nSlotNum=(SHORT)strtol(*(++argv), NULL, 0);
if (nSlotNum<0) DisplayUsage();
GxCntInitialize(nSlotNum, &nHandle, &nStatus);
CheckStatus(nStatus);
szChannelOrMeasure=__strupr(*(++argv));
if (!strcmp(szChannelOrMeasure, "SCHA") || !strcmp(szChannelOrMeasure,
"SCHB"))
{
// Check number of required arguments recived
if (argc<4)
DisplayUsage();
// Get Channel number
nChannel=strcmp(szChannelOrMeasure, "SCHA")==0 ? GXCNT_CHANNEL_A :
GXCNT_CHANNEL_B;
szOperation=__strupr(*(++argv));
if (!strcmp(szOperation, "FREQ"))
{ GxCntSetFunctionFrequency(nHandle, nChannel, &nStatus);
CheckStatus(nStatus);
printf("Set channel %c function to freqeuncy\n", nChannel+65);
}
else if (!strcmp(szOperation, "PERIOD"))
{ GxCntSetFunctionPeriod(nHandle, nChannel, &nStatus);
CheckStatus(nStatus);
printf("Set channel %c function to period\n", nChannel+65);
}
else if (!strcmp(szOperation, "TRIG_MODE"))
{
if (argc<5) DisplayUsage();
nTriggerMode=(SHORT)strtol(*(++argv), NULL, 0);
if (nTriggerMode<0 || nTriggerMode>2)
DisplayUsage();
GxCntSetChannelTriggerLevelMode(nHandle, nChannel, nTriggerMode,
&nStatus);
CheckStatus(nStatus);
// Verify settings
GxCntGetChannelTriggerLevelMode(nHandle, nChannel,
&nReadTriggerMode, &nStatus);
CheckStatus(nStatus);
printf("successfully set channel %c Trigger Level Mode\n",
nChannel+65);
}
else if (!strcmp(szOperation, "TRIG_LEVEL"))
{
dTriggerLevel=strtod(*(++argv), NULL);
GxCntSetChannelTriggerLevel(nHandle, nChannel, dTriggerLevel,
&nStatus);
CheckStatus(nStatus);
// verify settings
GxCntGetChannelTriggerLevel(nHandle, nChannel, &dTriggerLevel,
&nStatus);
CheckStatus(nStatus);
printf("Channel %c Trigger Level voltage is %f\n", nChannel+65,
dTriggerLevel);
}
else
DisplayUsage();
}
else if (!strcmp(szChannelOrMeasure, "MEASURE"))
{ // read set channel rail
GxCntReadMeasurement(nHandle, &dMeasure, &nStatus);
CheckStatus(nStatus);
printf("Measurement: %f\n", dMeasure);
}
else if (!strcmp(szChannelOrMeasure, "SUM"))
{ // print board summary
GxCntGetBoardSummary(nHandle, sz, sizeof sz, &nStatus);
CheckStatus(nStatus);
printf("Board Summary: %s.\n", sz);
}
else
DisplayUsage();
return 0;
}
//*********************************************************
// End Of File
//*********************************************************