Shared Memory Sample Code

NI-VISA

Shared Memory Sample Code

The following example shows how these shared memory operations work by incorporating them into the pointer manipulation example. Their main purpose is to allocate a block of memory from the pool that can then be accessed through the standard register-based access operations (high level or low level). The INSTR resource for this device ensures that no two sessions requesting memory receive overlapping blocks.

Example

Note  This example uses bold text to distinguish lines of code that are different from those in the pointer manipulation example.
Note  This example shows C source code. There is also an example in Visual Basic syntax.

#include "visa.h"

#define ADD_OFFSET (addr, offs) (((ViPByte)addr) + (offs))

int main(void)
{

ViStatus
ViSession
ViAddr
ViBusAddress
ViUInt16
ViUInt16
status;
defaultRM, self;
address;
offset;
addrSpace;
value;
/* For checking errors */
/* Communication channels */
/* User pointer */
/* Shared memory offset */
/* Shared memory space */
/* To store register value */
/* Begin by initializing the system */
status = viOpenDefaultRM(&defaultRM);
if (status < VI_SUCCESS) {

/* Error Initializing VISA...exiting */
return -1;

}

/* Open communication with VXI Device at Logical Address 0 */
/* NOTE: For simplicity, we will not show error checking */
status = viOpen(defaultRM, "VXI0::0::INSTR", VI_NULL, VI_NULL, &self);

/* Allocate a portion of the device's memory */
status = viMemAlloc(self, 0x100, &offset);

/* Determine where the shared memory resides */
status = viGetAttribute(self, VI_ATTR_MEM_SPACE, &addrSpace);

status = viMapAddress(self, addrSpace, offset, 0x100, VI_FALSE, VI_NULL, &address);

viPeek16(self, address, &value);
/* Access a different register by manipulating the pointer. */
viPeek16(self, ADD_OFFSET(address, 2), &value);

status = viUnmapAddress(self);
status = viMemFree(self, offset);

/* Close down the system */
status = viClose(self);
status = viClose(defaultRM);
return 0;

}