Sample Program Listing

/***********************************************************************

 

      FILE        : GxSmuExampleC.cpp

 

      PURPOSE           : WIN32/LINUX example program for GX3104/GX3116e boards

                              using the GxSmu driver

 

      CREATED           : Sep 2017

 

      COPYRIGHT   : (c) Marvin Test Solutions, Inc.

 

      COMMENTS    :

 

      To compile the example:

 

      1. Microsoft VC++

            Load GxSmuExampleC.dsp, .vcproj or .mak, depends on

            the VC++ version from the Project\File/Open... menu

            Select Project/Rebuild all from the menu

 

      2. Linux (GCC for CPP and Make must be available)

            make -fGxSmuExampleC.mk [CFG=Release[64] | Debug[64]] [rebuild |

                  clean]

 

***********************************************************************/

 

#ifndef __GNUC__

#include "windows.h"

#endif

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#include "..\..\GxSmu.h"

 

//**********************************************************************

//          DisplayMsg

//**********************************************************************

void DisplayMsg(PCSTR lpszMsg)

{

#ifndef __GNUC__

      MessageBeep(0);

      MessageBoxA(0, lpszMsg, "GxSmu example program", MB_OK);

#else

      printf("\r\nGxSmu 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 setup and measure using the "

            "GX3104 or GX3116e:"

 

            "\r\n\r\nUsage:\r\n"

            "GxSmuExampleC <slot> <function> <range> <resolution>"

 

            "\r\n\r\nWhere :\r\n"

            "<slot> : PCI/PXI slot number as shown by the PXI "

            "explorer\r\n"

 

            "<channel> :\r\n"

            "\tGX3104: CH1-CH4 = set the Channel Output Enabled\r\n"

            "\tGX3116: CH1-CH16 = set the Channel Output Enabled\r\n"

 

            "<Mode> :\r\n"

            "\tCVol=Constant Voltage Mode\r\n"

            "\tCCur=Constant Current Mode\r\n"

 

            "<Settings (GX3104)> :\r\n"

            "<Voltage Setting|Limit> : (+/-20V)\r\n"

            "<Current Setting|Limit> : (+/-25mA)\r\n"

 

            "<Settings (GX3116e)> :\r\n"

            "<Voltage Setting|Limit> : (-2.0V to +10.5V)\r\n"

            "<Current Setting|Limit> : (+/-25.6mA)\r\n"

 

            "The example should be run from the Windows command\r\n"

            "prompt/Linux terminal.\r\n"

            "Examples :\r\n"

            "\t./GxSmuExampleC 8 CH1 CVol 5 10\r\n"

            "\t./GxSmuExampleC 8 CH3 CCur 10 25\r\n"

            "\t./GxSmuExampleC 7 CH4 CVol -1 20 "

            "(under Windows, slot 7)\r\n"

            "\t./GxSmuExampleC 0x60c CH4 CVol 5 20"

            "(under Linux, bus 6 device 12)\r\n"

      );

      getchar();

      exit(1);

}

 

//**********************************************************************

//          CheckStatus

//**********************************************************************

void CheckStatus(SHORT nStatus)

{

      CHAR  sz[512];

 

      if (!nStatus) return;

      GxSmuGetErrorString(nStatus, sz, sizeof sz, &nStatus);

      DisplayMsg(sz);

      DisplayMsg("Aborting the program...");

      exit(nStatus);

}

 

//**********************************************************************

//          MAIN

//

// This main function receives the following parameters:

//

//    GX3104/GX3116e GXSMU board slot number (e.g. 1)

//

//    <channel>: string specifying which channel to use

//    GX3104: CH1-CH4

//    GX3116e: CH1-CH16

//         

//    <Mode>: string specifying the mode of the selected channel

//          CVol=Constant Voltage Mode

//          CCur=Constant Current Mode

//

//    <Voltage>:

//    GX3104: value specifying the voltage setting or limit (-20V - 20V)

//    GX3164: value specifying the voltage setting or limit (-2.0V to +10.5V)

//

//    <Current>:

//    GX3104: value specifying the current setting or limit (+/-25mA)

//    GX3116e: value specifying the current setting or limit (+/-25.6mA)

//**********************************************************************

int main(int argc, char ** argv)

{

      short nSlotNum;

      short nHandle;

      short nStatus;

      short nBoardType;

      short nChannel;

      short n;

      char * szChannel;

      short nMode;

      char * szMode;

      double dCurrent;

      double dVoltage;

      double dMeasCurrent;

      double dMeasVoltage;

      char   sz[256];

 

      // Check minimum number of arguments received

      if (argc < 5) DisplayUsage();

 

      // Parse command line parameters

      nSlotNum = (SHORT)strtol(*(++argv), NULL, 0);

      if (nSlotNum < 0) DisplayUsage();

      GxSmuInitialize(nSlotNum, &nHandle, &nStatus);

      CheckStatus(nStatus);

 

      GxSmuGetBoardType(nHandle, &nBoardType, &nStatus);

      CheckStatus(nStatus);

      // Get all the info...

      szChannel = __strupr(*(++argv));

      szMode = __strupr(*(++argv));

 

      // GX3104

      if (nBoardType == GXSMU_BOARD_TYPE_GX3104)

      {     // Get Channel number

            for (n = GX3104_CHANNEL_1; n <= GX3104_CHANNEL_4; n++)

            {

                  sprintf_s(sz, "CH%i", n);

                  if (!strcmp(szChannel, sz))

                  {

                        nChannel = n;

                        break;

                  }

                  else if (n == GX3104_CHANNEL_4)

                        DisplayUsage();

            }

            // Get Channel mode

            if (!strcmp(szMode, "CVOL"))

            {

                  nMode = GXSMU_SOURCE_MODE_CONSTANT_VOLTAGE;

                  printf("Set channel %i to constant voltage\n", nChannel);

            }

            else if (!strcmp(szMode, "CCUR"))

            {

                  nMode = GXSMU_SOURCE_MODE_CONSTANT_CURRENT;

                  printf("Set channel %i to constant current\n", nChannel);

            }

            else

                  DisplayUsage();

 

            // Set Channel mode

            GxSmuSourceSetMode(nHandle, nChannel, nMode, &nStatus);

            CheckStatus(nStatus);

            // Set current range to +/-25mA

            GxSmuSourceSetCurrentRange(nHandle, nChannel,

                  GX3104_SOURCE_CURRENT_RANGE_25MA, &nStatus);

            CheckStatus(nStatus);

            printf("Set channel %i current range to 25mA\n", nChannel);

 

            // Set Channel Output State

            GxSmuSourceSetOutputState(nHandle, nChannel,

                  GXSMU_SOURCE_OUTPUT_ON, &nStatus);

            CheckStatus(nStatus);

            printf("Set channel %i output to on\n", nChannel);

 

            // Get Voltage setting

            dVoltage = strtod(*(++argv), NULL);

            // Get Current setting, current needs to <= +/-25mA

            dCurrent = strtod(*(++argv), NULL);

 

            if (nMode == GXSMU_SOURCE_MODE_CONSTANT_VOLTAGE)

            {

                  GxSmuSourceSetVoltage(nHandle, nChannel, dVoltage,

                        &nStatus);

                  CheckStatus(nStatus);

                  printf("Set channel %i voltage\n", nChannel);

 

                  GxSmuSourceSetCurrentLimit(nHandle, nChannel, dCurrent,

                        &nStatus);

                  CheckStatus(nStatus);

                  printf("Set channel %i current limit\n", nChannel);

            }

            else

            {

                  GxSmuSourceSetCurrent(nHandle, nChannel, dCurrent,

                        &nStatus);

                  CheckStatus(nStatus);

                  printf("Set channel %i current\n", nChannel);

 

                  GxSmuSourceSetVoltageLimit(nHandle, nChannel, dVoltage,

                        &nStatus);

                  CheckStatus(nStatus);

                  printf("Set channel %i voltage limit\n", nChannel);

            }

 

            //Measure voltage across channel

            GxSmuMeasure(nHandle, nChannel, GXSMU_MEASURE_VOLTAGE,

                  &dMeasVoltage, &nStatus);

            CheckStatus(nStatus);

            printf("Measured voltage across channel %i:\n", nChannel);

            printf("\t%f Volts\n", dMeasVoltage);

 

            //Measure current across channel

            GxSmuMeasure(nHandle, nChannel, GXSMU_MEASURE_CURRENT,

                  &dMeasCurrent, &nStatus);

            CheckStatus(nStatus);

            printf("Measured current across channel %i:\n", nChannel);

            printf("\t%f mAmps\n", dMeasCurrent);

      }

      else // GX3116e

      {     // Get Channel number

            for (n = GX3116_CHANNEL_1; n <= GX3116_CHANNEL_16; n++)

            {

                  sprintf_s(sz, "CH%i", n);

                  if (!strcmp(szChannel, sz))

                  {

                        nChannel = n;

                        break;

                  }

                  else if (n == GX3116_CHANNEL_16)

                        DisplayUsage();

            }

            // Get Channel mode

            if (!strcmp(szMode, "CVOL"))

            {

                  nMode = GXSMU_SOURCE_MODE_CONSTANT_VOLTAGE;

                  printf("Set channel %i to constant voltage\n", nChannel);

            }

            else if (!strcmp(szMode, "CCUR"))

            {

                  nMode = GXSMU_SOURCE_MODE_CONSTANT_CURRENT;

                  printf("Set channel %i to constant current\n", nChannel);

            }

            else

                  DisplayUsage();

 

            // Set Channel mode

            GxSmuSourceSetMode(nHandle, nChannel, nMode, &nStatus);

            CheckStatus(nStatus);

            // Set current range to 25.6mA

            GxSmuSourceSetCurrentRange(nHandle, nChannel,

                  GX3116_SOURCE_CURRENT_RANGE_25_POINT_6MA, &nStatus);

            CheckStatus(nStatus);

            printf("Set channel %i current range to 25.6mA\n", nChannel);

 

            // Set Channel Output State

            GxSmuSourceSetOutputState(nHandle, nChannel,

                  GXSMU_SOURCE_OUTPUT_ON, &nStatus);

            CheckStatus(nStatus);

            printf("Set channel %i output to on\n", nChannel);

 

            // Get Voltage setting

            dVoltage = strtod(*(++argv), NULL);

            // Get Current setting, currnet needs to <= +/-25.6mA

            dCurrent = strtod(*(++argv), NULL);

 

            if (nMode == GXSMU_SOURCE_MODE_CONSTANT_VOLTAGE)

            {

                  GxSmuSourceSetVoltage(nHandle, nChannel, dVoltage,

                        &nStatus);

                  CheckStatus(nStatus);

                  printf("Set channel %i voltage\n", nChannel);

 

                  // Set Current limits setting - I source = I sink

                  GxSmuSourceSetCurrentLimits(nHandle, nChannel,

                        dCurrent, dCurrent, &nStatus);

                  CheckStatus(nStatus);

                  printf("Set channel %i current limits\n", nChannel);

 

                  //Measure voltage across channel

                  GxSmuMeasure(nHandle, nChannel, GXSMU_MEASURE_VOLTAGE,

                        &dMeasVoltage, &nStatus);

                  CheckStatus(nStatus);

                  printf("Measured voltage across channel %i:\n", nChannel);

                  printf("\t%f Volts\n", dMeasVoltage);

 

                  //Measure current across channel

                  GxSmuMeasure(nHandle, nChannel, GXSMU_MEASURE_CURRENT,

                        &dMeasCurrent, &nStatus);

                  CheckStatus(nStatus);

                  printf("Measured current across channel %i:\n", nChannel);

                  printf("\t%f mAmps\n", dMeasCurrent);

            }

            else

            {

                  GxSmuSourceSetCurrent(nHandle, nChannel, dCurrent,

                        &nStatus);

                  CheckStatus(nStatus);

                  printf("Set channel %i current\n", nChannel);

                  // Set Channel Voltage Limits: Voltage high = Voltage,

                  // voltage low = 0V

                  GxSmuSourceSetVoltageLimits(nHandle, nChannel, dVoltage,

                        0, &nStatus);

                  CheckStatus(nStatus);

                  printf("Set channel %i voltage limits\n", nChannel);

 

                  // Measure voltage across channel: in constant current

                  // mode, we can only measure voltage

                  GxSmuMeasure(nHandle, nChannel, 0, &dMeasVoltage,

                        &nStatus);

                  CheckStatus(nStatus);

                  printf("Measured voltage across channel %i:\n", nChannel);

                  printf("\t%f Volts\n", dMeasVoltage);

            }

      }

      system("pause");

      return 0;

 

}

 

//**************************************************************************

//          End Of File

//**************************************************************************