Sample Program Listing

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

FILE : GxPIOExample.cpp

PURPOSE : WIN32 sample program for GX5731, GX5732 and GX5733

          boards using the GXPIO driver.

CREATED : March 2003

COPYRIGHT : Copyright 2007 Marvin Test Solutions, Inc.

COMMENTS :

To compile the WIN32 example:

1. Microsoft VC++

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

Select Project/Build all from the menu

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

#include "windows.h"

#include "GxPIO.h"

#include "stdio.h"

 

// Borland C++ Builder compat. block

#if defined(__BORLANDC__)

#pragma hdrstop

#include <condefs.h>

USELIB("GxPIOBC.lib");

USERC("GxPioExampleC.rc");

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

#endif // defined(__BORLANDC__)

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

//          DisplayMsg

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

void DisplayMsg(PSTR lpszMsg)

{

MessageBeep(0);

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

return;

}

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

//          DisplayUsage

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

void DisplayUsage(void)

{

DisplayMsg(

"This example shows how to use the GX5731 or GX5732\r\n"

"Usage:\r\n"

"GxPIOExample <board_type> <slot_number> <port|counter numbers> <operation> <direction|value|count>\r\n\n"

"Where: \r\n"

"<board_type> - Board type: 5731=GX5731, 5732=GX5732, 5733=GX5733\r\n\n"

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

"<port|counter numbers> - depends on the operation:\r\n"

"\tport number:    \t0-6 for WD/RD/WP/RP operations\r\n"

"\tcounter number: \t0-7 for WC/RC operations\r\n\n"

"<operation> - one of the followings :\r\n"

"\tWD=Write port direction\t\tRD=Read port direction\r\n"

"\tWP=Write port value    \t\tRP=Read port value\r\n"

"\tWC=Write counter       \t\tRC=Read counter\r\n"

"\tRB=read buffer  \tWB=write buffer\r\n"

"\tRBF=read buffer empty/full flag\r\n"

"\tSCLK_SRC=set internal clock source  GCLK_SRC=get internal clock source\r\n"

"\tSCLK_DIV=set internal clock divider GCLK_DIV=Get internal clock divider\r\n\n"

"<direction|value|counter> - depends on the operation:\r\n"

"\tWD : 0-15        \t\t4 bits direction. each bit controls a byte direction\r\n"

"\t                 \t\t in the port, 0 for input and 1 for output\r\n"

"\tWP : 0-0xFFFFFFFF\t\tPort value\r\n"

"\tWC : 0-255       \t\tCounter value to load\r\n\n"

"To change command line under Windows:\r\n"

"\tRight click on the example shortcut from the start menu\r\n"

"\tand type the new command line"

);

exit(1);

}

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

//            CheckStatus

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

void CheckStatus(SHORT nStatus)

{

    CHAR sz[128];

if (!nStatus) return;

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

DisplayMsg(sz);

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

exit(nStatus);

}

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

//     MAIN

//

// This main functin receives three to four parameters

//

// Board type: 5731 - GX5731

//             5732 - GX5732

// PIO board slot number (e.g. 1)

// Port/counter number (depends on operation):

//             0 - 6 for port

//             0 - 7 for counter

// Operation:

//             RD=read port direction

//             WD=write port direction

//             RP=read port value

//             WP=write port value

//             RC=read counter

//             WC=write counter

//             RB=read buffer

//             WB=write buffer

//             RBF=read buffer empty/full flag

//             SCLK_SRC=set internal clock source and divider

//             GCLK_SRC=get internal clock source

//             SCLK_DIV=set internal clock divider

//             GCLK_DIV=Get internal clock divider

// Direction/value/counter/clock number (depends on operation):

//             0 - 15 for direction. This is 4-bits direction.

//             Each bit controls the direction of a byte of

//             the port. 1 for output, 0 for input.

//             0 - 0xFFFFFFFF for value

//             0 - 255 for counter

//             0 - 1 for Internal clock 0 or 1

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

int main(int argc, char **argv)

{

SHORT nSlotNum;           // Board slot number

SHORT nBoardType;         // Board type

char* sOperation;         // Board Operation

SHORT nPortOrCount;       // Port or counter number

DWORD dwValue;            // Direction, port value or count

DWORD dwSize;             // Write/Read buffer data Size

BOOL bTerminalCountState; // returned terminal count state

SHORT nHandle;            // Board handle

SHORT nStatus;            // Returned status

DWORD dwDivider;          // Read Clock Divider

SHORT nSource;            // Read Clock Source

 

// Check number of arguments rcvd

if (argc<4) DisplayUsage();

 

// Parse command line parameters

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

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

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

#if _MSC_VER < 1400

      sOperation = strupr(*(++argv));

#else

      sOperation=*(++argv);

      _strupr_s(sOperation, sizeof sOperation);

#endif

 

// Check parameters

if (nPortOrCount<0 || nPortOrCount>7) DisplayUsage();

 

// Board type is GX5731

if (nBoardType==5731)

{   Gx5731Initialize(nSlotNum, &nHandle, &nStatus);

    CheckStatus(nStatus);

    if(!strcmp(sOperation, "WD")) // Write port direction

    {     if (nPortOrCount>6) DisplayUsage();

    if (argc<5) DisplayUsage();

    dwValue=(DWORD)strtol(*(++argv), NULL, 0);

    Gx5731SetPortDirection(nHandle, nPortOrCount, SHORT(dwValue),

&nStatus);

    CheckStatus(nStatus);

    printf("Writes port direction OK\n");

    }

    else if (!strcmp(sOperation, "RD")) // read port direction

    {     if (nPortOrCount>6) DisplayUsage();

          Gx5731GetPortDirection(nHandle, nPortOrCount, ((PSHORT)&dwValue),

&nStatus);

          CheckStatus(nStatus);

          printf("Reads port direction: %d\n", SHORT(dwValue));

      }

      else if (!strcmp(sOperation, "WP")) // write port data

      {     if (nPortOrCount>6) DisplayUsage();

            if (argc<5) DisplayUsage();

dwValue=(DWORD)strtol(*(++argv), NULL, 0);

Gx5731SetPort(nHandle, nPortOrCount, dwValue, &nStatus);

CheckStatus(nStatus);

printf("Writes port value OK\n");

      }

      else if (!strcmp(sOperation, "RP")) // read port data

 {     if (nPortOrCount>6) DisplayUsage();

Gx5731GetPort(nHandle, nPortOrCount, &dwValue, &nStatus);

CheckStatus(nStatus);

printf("Reads port value: 0x%X\n", dwValue);

}

else if (!strcmp(sOperation, "WB")) // write buffer

{     if (argc<5 || (nPortOrCount<0 || nPortOrCount>2)) DisplayUsage();

 dwValue=(DWORD)strtol(*(++argv), NULL, 0);

 dwSize=1;

 Gx5731ModuleBufferWriteData(nHandle, nPortOrCount, &dwValue,

&dwSize, &nStatus);

CheckStatus(nStatus);

printf("Writes data to buffer OK\n");

}

else if (!strcmp(sOperation, "RB")) // read buffer

{     if (nPortOrCount<0 || nPortOrCount>2) DisplayUsage();

 dwSize=1;

 Gx5731ModuleBufferReadData(nHandle, nPortOrCount, &dwValue,

&dwSize, &nStatus);

CheckStatus(nStatus);

printf("Reads data to buffer: %d\n", dwValue);

}

else if (!strcmp(sOperation, "RBF"))// read buffer empty/full flag

 if (argc<5 || (nPortOrCount<0 || nPortOrCount>2)) DisplayUsage();

dwValue=(DWORD)strtol(*(++argv), NULL, 0);

Gx5731ModuleBufferGetState(nHandle, nPortOrCount,

((PSHORT)&dwValue),&nStatus);

CheckStatus(nStatus);

printf("Module Buffer Flag state is: %d\n", dwValue);

}

else if (!strcmp(sOperation, "SCLK_SRC")) // set internal clock source

{     if (argc<5 || (nPortOrCount<0 || nPortOrCount>2)) DisplayUsage();

dwValue=(DWORD)strtol(*(++argv), NULL, 0);

Gx5731GetInternalClock(nHandle, nPortOrCount, &nSource,

&dwDivider, &nStatus);

Gx5731SetInternalClock(nHandle, nPortOrCount, SHORT(dwValue),

dwDivider, &nStatus);

CheckStatus(nStatus);

printf("Set internal clock source OK\n");

}

else if (!strcmp(sOperation, "GCLK_SRC")) // get internal clock source

{     if (nPortOrCount<0 || nPortOrCount>2) DisplayUsage();

Gx5731GetInternalClock(nHandle, nPortOrCount, ((PSHORT)&dwValue),

&dwDivider, &nStatus);

CheckStatus(nStatus);

printf("Internal clock source is: %d\n", dwValue);

}

else if (!strcmp(sOperation, "SCLK_DIV")) // set internal clock divider

{     if (argc<5 || (nPortOrCount<0 || nPortOrCount>2)) DisplayUsage();

 dwValue=(DWORD)strtol(*(++argv), NULL, 0);

 Gx5731GetInternalClock(nHandle, nPortOrCount, &nSource,

&dwDivider, &nStatus);

 Gx5731SetInternalClock(nHandle, nPortOrCount, nSource, dwValue, &nStatus);

 CheckStatus(nStatus);

 printf("Set internal clock divider OK\n");

}

else if (!strcmp(sOperation, "GCLK_DIV")) // get internal clock divider

{     if (nPortOrCount<0 || nPortOrCount>2) DisplayUsage();

 Gx5731GetInternalClock(nHandle, nPortOrCount, &nSource, &dwDivider, &nStatus);

 CheckStatus(nStatus);

 printf("Internal clock divider is: %d\n", dwDivider);

}

else

 DisplayUsage();

}

// Borad type is GX5732

else

{    Gx5732Initialize(nSlotNum, &nHandle, &nStatus);

CheckStatus(nStatus);

if(!strcmp(sOperation, "WD")) // Write port direction

{    if (nPortOrCount>6) DisplayUsage();

if (argc<5) DisplayUsage();

dwValue=(DWORD)strtol(*(++argv), NULL, 0);

Gx5732SetPortDirection(nHandle, nPortOrCount, SHORT(dwValue),

&nStatus);

CheckStatus(nStatus);

printf("Writes port direction OK\n");

}

else if (!strcmp(sOperation, "RD")) // read port direction

{     if (nPortOrCount>6) DisplayUsage();

 Gx5732GetPortDirection(nHandle, nPortOrCount, ((PSHORT)&dwValue), &nStatus);

 CheckStatus(nStatus);

 printf("Reads port direction: %d\n", SHORT(dwValue));

}

else if (!strcmp(sOperation, "WP")) // write port data

{     if (nPortOrCount>6) DisplayUsage();

 if (argc<5) DisplayUsage();

 dwValue=(DWORD)strtol(*(++argv), NULL, 0);

 Gx5732SetPort(nHandle, nPortOrCount, dwValue,&nStatus);

 CheckStatus(nStatus);

 printf("Writes port value OK\n");

}

else if (!strcmp(sOperation, "RP")) // read port data

{     if (nPortOrCount>6) DisplayUsage();

 Gx5732GetPort(nHandle, nPortOrCount, &dwValue, &nStatus);

 CheckStatus(nStatus);

 printf("Reads port value: 0x%X\n", dwValue);

}

else if (!strcmp(sOperation, "WC")) // write counter

{     if (argc<5) DisplayUsage();

 dwValue=(DWORD)strtol(*(++argv), NULL, 0);

 Gx5732SetCounterValue(nHandle, nPortOrCount, (BYTE)dwValue, &nStatus);

 CheckStatus(nStatus);

 printf("Writes counter OK\n");

}

else if (!strcmp(sOperation, "RC")) // read counter

{     Gx5732GetCounterValue(nHandle, nPortOrCount, (BYTE*)&dwValue,

&bTerminalCountState, &nStatus);

 CheckStatus(nStatus);

 printf("Reads counter: %d, terminal count state: %d\n",

(BYTE)dwValue, bTerminalCountState);

}

else

 DisplayUsage();

}

return 0;

}

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

// End Of File

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