Sample Programs

The following example demonstrates how to program the board using the C programming language under Windows. The example shows how to initialize the DIO board for a specific slot, set it up for measurement or trigger settings and get the reading.

The example bellow works with several DIO boards. Check the Examples folder for additional examples for other boards.

To run, enter the following command line:

GtDioExampleC  <Slot> <function> <value >

Where:

<Slot>         PXI Explorer slot number where the board resides.

Sample Program Listing

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

FILE               : GtDioExampleC.cpp

PURPOSE      : WIN32 sample program for GX2470 board using the GtDio driver.

CREATED       : Feb. 2008

COPYRIGHT    : Copyright 2008 Marvin Test Solutions, Inc.

COMMENTS :

To compile the WIN32 example:

  1. Microsoft VC++

Load GtDioExampleC.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 GtDioExampleC.bpr from the Project/Open

Project... menu

Select Project/Build all from the menu

 

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

#include "windows.h"

#include "GtDio.h"

#include <time.h>

#include "stdio.h"

// Borland C++ Builder compat. block

#if defined(__BORLANDC__)

#pragma hdrstop

#include <condefs.h>

USELIB("GTDIO32B.lib");

USERC("GtDioExampleC.rc");

//---------------------------------------------------------

#endif // defined(__BORLANDC__)

 

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

//             DisplayMsg

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

void DisplayMsg(PSTR lpszMsg)

{           MessageBeep(0);

            MessageBox(0, lpszMsg,"DIO", MB_OK);

            return;

}

 

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

//             DisplayUsage

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

void DisplayUsage(void)

{

DisplayMsg(

"This example shows the following:\r\n"

"\tInitialize a DIO board using the commands line parameters.\r\n"

"\tcreating a DIO file and fill it with a ramp patern.\r\n"

"\tFill the file control data with NOPs values and HALT command at step 1020\r\n"

"\tSet the file D, T and P events & mask registers.\r\n"

"\tSet the frequency to 2MHz and close and save the file.\r\n"

"\tLoad the DIO file, Arm, Trig Halt and save the result vector to another DIO file (.DI)\r\n\r\n"

 

"Usage:\r\n"

"GtDioExampleC <slot>"

 

"\r\n\r\nWhere :"

"<slot> - PCI/PXI slot number as shown by the PXI explorer\r\n"

 

"\r\nExample:\r\n"

"GtDioExampleC 0x106\r\n"

"\r\nTo change command line under Windows Select File/Properties from Program\r\n"

"Manager Menu and change the command line edit box as shown above."

);

exit(1);

}

 

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

//             CheckStatus

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

void CheckStatus(SHORT nStatus)

{

CHAR sz[512];

 

if (!nStatus) return;

DioGetErrorString(nStatus, sz, sizeof sz);

DisplayMsg(sz);

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

exit(nStatus);

}

 

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

//        MAIN

// This main function receives one parameters - slot number

//

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

int main(int argc, char **argv)

{

short nSlotNum;          // Board slot number

short nStatus;             // Returned status

short nHandle;             // DIO Master handle

short nDensity;            // Board density

short nBanks;              // Board density

clock_t ctStartTime;     // For timeout

WORD wData;             // Status register value

short hFile;

DWORD dw, dwSize;

DWORD dwData[1024]; // data array

DWORD dwCtrl[1024];   // control array

BYTE ucCtrl[1024];       // control array(GX5150)

short nType;                 // board type

char szFileNmae[128];

 

// Check number of arguments received

if   (argc<3) DisplayUsage();

 

// Parse command line parameters

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

 

if   (nSlotNum<0)

        DisplayUsage();

 

// Initialize Master DIO board

DioSetupInitialization(0, 1, nSlotNum, &nDensity, &nBanks, &nHandle, &nStatus);

CheckStatus(nStatus);

 

DioGetBoardType (nHandle, &nType, &nStatus);

//creat DIO file

switch(nType)

{case DIO_BOARD_TYPE_GC5050:

         strcpy (szFileNmae, "GC5050.DIO");

         break;

case DIO_BOARD_TYPE_GX5050:

         strcpy (szFileNmae, "GX5050.DIO");

         break;

case DIO_BOARD_TYPE_GX5150:

         strcpy (szFileNmae, "GX5150.DIO");

         break;

case DIO_BOARD_TYPE_GX5280:

         strcpy (szFileNmae, "GX5280.DIO");

         break;

case DIO_BOARD_TYPE_GX5290:

         strcpy (szFileNmae, "GX5290.DIO");

         break;

default:

MessageBox(0, "The board not supported", "GtDioExample", MB_OK);

return 0;

}

 

// Create new file

DioFileOpen (szFileNmae, DIO_FILE_CREATE, &nType, &hFile, &nStatus);

CheckStatus(nStatus);

 

// Set number of steps to 1024

DioFileSetNumberOfSteps(hFile, 1024, &nStatus);

CheckStatus(nStatus);

 

// Create a ramp pattern and write it to the ouput data

for (dw=0; dw<1024; dw++)

       dwData[dw]=dw;

 

DioWriteOutMemory (hFile, dwData, 0, 1024, &nStatus);

CheckStatus(nStatus);

 

if   (nType==DIO_BOARD_TYPE_GC5050 || nType==DIO_BOARD_TYPE_GX5050)

{

// Zero the control array (all groups are set to output)

for (dw=0; dw<1024; dw++)

        dwCtrl[dw]=0x0;

 

// Add commands to the control memory

dwCtrl[2]=0x01000064; // Set regiaster A to 100

dwCtrl[9]=0x00C00005; // Loop steps 5 to 9 for 100  (register A value)

         dwCtrl[1020]=0x01CE0000; // HALT to step 1020

                    // Write Ctrl Memory

          DioWriteCtrlMemory (hFile, dwCtrl, 0, 1024, &nStatus);

          CheckStatus(nStatus);

}

else if   (nType==DIO_BOARD_TYPE_GX5150)

{        // control array ( all outputs are enabled)

         for (dw=0; dw<1024; dw++)

                 ucCtrl[dw]=0xF0;

                 // Add HALT command to the control memory

         ucCtrl[1020]=0xFF; // HALT to step 1020

         // Write Ctrl Memory

         DioWriteCtrlMemory (hFile, ucCtrl, 1020, 1, &nStatus);

         CheckStatus(nStatus);

}

else if  (nType==DIO_BOARD_TYPE_GX5280)

{       // Set channesl directions as follows:

        // group 0 (channels 0-7) to out

        // group 1 (channels 8-15) to in

        // group 2 (channels 16-23) to out

        // group 3 (channels 24-31) to in

        DioSetupIOConfiguration(hFile, 0, 0x5, &nStatus);

         // Insert HALT command at step 1020

        DioWriteCtrlCommand(hFile, 1020, DIO_COMMAND_HALT, 0, 0, 0, 0, &nStatus);

 

}

else if (nType==DIO_BOARD_TYPE_GX5290)

{

// Fill an array to set the direction memory as follows:

// Set all odd channels numbers (1, 3 etc.) to be set to input on even steps and output on odd steps numbers.

// Set all even channels numbers (0, 2 etc.) to be set to output on odd steps and output on even steps numbers.

for  (dw=0; dw<1024; dw++)

        dwCtrl[dw]= dw & 1? 0xAAAAAAAA: 0x55555555;

// Write to the direction Memory starting at step 0, 25000 steps

DioWriteDirectionMemory(hFile, dwCtrl, 0, 1024, &nStatus);

CheckStatus(nStatus);

         // Insert HALT command at step 1020

         DioWriteCtrlCommand(hFile, 1020, DIO_COMMAND_HALT, 0, 0, 0, 0, &nStatus);

 

}

 

// Set D Events and Mask

DioSetupTriggerDEvent (hFile, 0x1234, 0xFF55, &nStatus);

CheckStatus(nStatus);

 

// Set T Events and Mask

DioSetupTriggerTEvent (hFile, 0xAAAA, 0xFFFF, &nStatus);

CheckStatus(nStatus);

 

// Set P Events and Mask

DioSetupTriggerPEvent (hFile, 0xABCD, 0xFFFF, &nStatus);

CheckStatus(nStatus);

 

// Set frequency to 2MHz

DioSetupFrequency (hFile, 2000000, &nStatus);

DioFileClose (hFile, &nStatus);

 

// Load data to the DIO board

DioLoadFile(nHandle, szFileNmae, 0, 0, &dwSize, &nStatus);

CheckStatus(nStatus);

// Arm the DIO card

DioArm(nHandle, &nStatus);

CheckStatus(nStatus);

// Trigger the DIO card

DioTrig(nHandle, &nStatus);

CheckStatus(nStatus);

// Wait up to 1000ms or until Halt state to complete

ctStartTime=clock();

do

{         DioReadStatusRegister(nHandle, &wData, &nStatus);}

while ((wData & 0x1C)!=0 && (clock()- ctStartTime)*CLOCKS_PER_SEC<1000000);

 

// Halt the DIO card

DioHalt(nHandle, &nStatus);

CheckStatus(nStatus);

// Read data from DIO card and save it into a file

switch(nType)

{         case DIO_BOARD_TYPE_GC5050:

                  strcpy (szFileNmae, "GC5050.DI");

                  break;

          case DIO_BOARD_TYPE_GX5050:

                  strcpy (szFileNmae, "GX5050.DI");

                  break;

          case DIO_BOARD_TYPE_GX5150:

                  strcpy (szFileNmae, "GX5150.DI");

                  break;

          case DIO_BOARD_TYPE_GX5280:

                  strcpy (szFileNmae, "GX5280.DI");

                  break;

          case DIO_BOARD_TYPE_GX5290:

                  strcpy (szFileNmae, "GX5290.DI");

                  break;

}

DioSaveFile(nHandle, szFileNmae, 0, &dwSize, &nStatus);

CheckStatus(nStatus);

MessageBox(0, "DIO example program was completed successfully", "GtDioExample", MB_OK);

return 0;

}

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

// End Of File

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