PXIVisaSample

Agilent VISA.NET

// PXIVisaSample

//   An example of how to use Agilent VISA to get/set attributes

//   and access memory on a PXI device.

//

// Error checking should always be done when making VISA calls.

// To simplify this example and make it easier to read,

// most error checking has been eliminated.

 

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers

#include <stdio.h>

#include <windows.h>

#include "visa.h"

 

///////////////////////////////////////////////////////////////////////////////////

// Finding all VISA PXI resources

///////////////////////////////////////////////////////////////////////////////////

void VisaFindRsrc()

{

   ViStatus status;

   ViSession drm;

   printf("VISA viFindRsrc / viFindNext\n");

   printf("============================\n");

 

   // Open a session to the VISA default resource manager

   status = viOpenDefaultRM(&drm);

 

   // Find all PXI resources

   char *searchExpression = "PXI?*";

   ViSession viFindSession;

   ViUInt32 findCount = 0;

   ViChar findName[1024];

   status = viFindRsrc(drm, searchExpression, &viFindSession, &findCount, findName);

   if (status >= VI_SUCCESS)

   {

      printf("viFindRsrc of '%s' found %d device(s)\n", searchExpression, findCount);

      for (ViUInt32 index = 1; index <= findCount; index++)

      {

         printf("  %2d: %s\n", index, findName);

         if (index < findCount)

         {

            status = viFindNext(viFindSession, findName);

         }

      }

      status = viClose(viFindSession);

   }

   status = viClose(drm);

   printf("\n");

}

 

void VisaLowLevelMemoryAccess(char *visaName)

{

   ViStatus status;

   ViSession drm;

   printf("VISA Low Level Memory Access Example\n");

   printf("====================================\n");

   // Open a session to the VISA default resource manager

   status = viOpenDefaultRM(&drm);

 

   // Open a session to the PXI device

   ViSession vi;

   status = viOpen(drm, visaName, VI_NULL, VI_NULL, &vi);

   printf("Opening '%s' returned status=0x%x\n", visaName, status);

 

   // Get the manufacturer and model name

   ViChar manfName[1024];

   ViChar modelName[1024];

   status = viGetAttribute(vi, VI_ATTR_MANF_NAME, manfName);

   status = viGetAttribute(vi, VI_ATTR_MODEL_NAME, modelName);

   printf("   Manufacturer Name: '%s'\n", manfName);

   printf("   Model Name:        '%s'\n", modelName);

 

   // Get the size of the BAR0 memory

   ViAttr    mapAttr  = VI_ATTR_PXI_MEM_SIZE_BAR0;

   ViUInt16  mapSpace = VI_PXI_BAR0_SPACE;

   ViBusSize mapSize  = 0;

   status = viGetAttribute(vi, mapAttr, &mapSize);

 

   // Map BAR0 memory - Note: only one map at a time is allowed per VISA session

   ViBusAddress mapOffset  = 0;

   ViAddr       mapAddress = 0;

   status = viMapAddress(vi, mapSpace, mapOffset, mapSize, VI_NULL, VI_NULL, &mapAddress);

   if (sizeof(void*)==8)

   {

      // ViBusSize and viBusAddress are 64-bit values in 64-bit applications

      printf("   Size of BAR0 memory = %I64d bytes\n", mapSize);

      printf("   BAR0 Map Address    = 0x%016I64x\n", mapAddress);

   }

   else

   {

      // ViBusSize and viBusAddress are 32-bit values in 32-bit applications

      printf("   Size of BAR0 memory = %d bytes\n", mapSize);

      printf("   BAR0 Map Address    = 0x%08x\n", mapAddress);

   }

 

   // Find out if BAR0 memory can be dereferenced directly

   ViUInt16 winAccess;

   status = viGetAttribute(vi, VI_ATTR_WIN_ACCESS, &winAccess);

   if (winAccess == VI_DEREF_ADDR)

   {

      printf("   BAR0 memory can be directly dereferenced\n");

   }

   else if (winAccess == VI_USE_OPERS)

   {

      printf("   BAR0 memory cannot be directly dereferenced.\n");

   }

   else if (winAccess == VI_NMAPPED)

   {

      printf("   BAR0 memory is not currently mapped.\n");

   }

 

   if (winAccess == VI_DEREF_ADDR)

   {

      printf("   Dereferencing BAR0 memory\n");

      // Here are some examples of direct memory dereferencing:

      ViUInt32 memValue0 = *((ViUInt32 *)mapAddress);

      ViUInt32 memValue1 = *((ViUInt32 *)mapAddress+1);

      printf("      memValue0 = 0x%08x, memValue1 = 0x%08x\n", memValue0, memValue1);

      ViChar memValueArray[16];

      memcpy(memValueArray, mapAddress, sizeof(memValueArray));

      printf("      memValueArray = ");

      for (int index = 0; index < sizeof(memValueArray); index++)

      {

         printf("%02x", memValueArray[index]);

      }

      printf("\n");

   }

 

   if ((winAccess == VI_USE_OPERS) || (winAccess == VI_DEREF_ADDR))

   {

      printf("   Using viPeek on BAR0 memory\n");

      // Some examples of using viPeek for memory access:

      ViUInt32 memValue0;

      ViUInt32 memValue1;

      viPeek32(vi, mapAddress, &memValue0);

      viPeek32(vi, ((ViUInt32 *)mapAddress)+1, &memValue1);

      printf("      memValue0 = 0x%08x, memValue1 = 0x%08x\n", memValue0, memValue1);

   }

 

   status = viUnmapAddress(vi);

 

   status = viClose(vi);

   status = viClose(drm);

   printf("\n");

}

 

void VisaHighLevelMemoryAccess(char *visaName)

{

   ViStatus status;

   ViSession drm;

   printf("VISA High Level Memory Access Example\n");

   printf("=====================================\n");

   // Open a session to the VISA default resource manager

   status = viOpenDefaultRM(&drm);

 

   // Open a session to the PXI device

   ViSession vi;

   status = viOpen(drm, visaName, VI_NULL, VI_NULL, &vi);

   printf("Opening '%s' returned status=0x%x\n", visaName, status);

 

   // Get the manufacturer and model name

   ViChar manfName[1024];

   ViChar modelName[1024];

   status = viGetAttribute(vi, VI_ATTR_MANF_NAME, manfName);

   status = viGetAttribute(vi, VI_ATTR_MODEL_NAME, modelName);

   printf("   Manufacturer Name: '%s'\n", manfName);

   printf("   Model Name:        '%s'\n", modelName);

 

   ViUInt16  mapSpace = VI_PXI_BAR0_SPACE;

   ViUInt32 memValue0;

   ViUInt32 memValue1;

   ViUInt32 memValueArray[4];

 

   // viIn example

   status = viIn32(vi, mapSpace, 0, &memValue0);

   status = viIn32(vi, mapSpace, sizeof(ViUInt32) * 1, &memValue1);

   printf("   viIn32 from BAR0 memory\n");

   printf("      memValue0 = 0x%08x, memValue1 = 0x%08x\n", memValue0, memValue1);

 

   // Show the default DMA value and set it to VI_TRUE so viMoveIn32 will use DMA if

   // the device supports it.

   ViBoolean allowDma;

   status = viGetAttribute(vi, VI_ATTR_DMA_ALLOW_EN, &allowDma);

   printf("   Current value of VI_ATTR_DMA_ALLOW_EN is %s\n",

          (allowDma == VI_TRUE)? "VI_TRUE" : "VI_FALSE");

   status = viSetAttribute(vi, VI_ATTR_DMA_ALLOW_EN, VI_TRUE);

   status = viGetAttribute(vi, VI_ATTR_DMA_ALLOW_EN, &allowDma);

   printf("   After setting it VI_ATTR_DMA_ALLOW_EN is %s\n",

          (allowDma == VI_TRUE)? "VI_TRUE" : "VI_FALSE");

 

   // viMoveIn example

   status = viMoveIn32(vi, mapSpace, 0,

                       sizeof(memValueArray)/sizeof(memValueArray[0]),

                       memValueArray);

   printf("   viMoveIn32 from BAR0 memory\n");

   printf("      memValueArray = ");

   for (int index = 0; index < sizeof(memValueArray)/sizeof(memValueArray[0]); index++)

   {

      printf("0x%08x ", memValueArray[index]);

   }

   printf("\n");

 

   status = viClose(vi);

   status = viClose(drm);

   printf("\n");

}

 

void main()

{

   VisaFindRsrc();

 

   char *visaName = "pxi0::1-1.0::instr";

 

   VisaLowLevelMemoryAccess(visaName);

   VisaHighLevelMemoryAccess(visaName);

 

}