Shared Memory Operations
- Updated2025-10-10
- 3 minute(s) read
A common configuration in a VXI system is to export memory to either the A24 or A32 space. The local controller usually can export such memory. This memory can then be used to buffer the data going to or from the instruments in the system. However, a common problem is preventing multiple devices from using the same memory. In other words, a memory manager is needed on this memory to prevent corruption of the data.
The VISA Shared Memory operations—viMemAlloc() and viMemFree()—provide the memory management for a specific device, namely, the local controller. Since these operations are part of the INSTR resource, they are associated with a single VXI device. In addition, because a VXI device can export memory in either A24 or A32 space (but not both), the memory pool available to these operations is defined at startup. You can determine whether the memory resides in A24 or A32 space by querying the attribute VI_ATTR_MEM_SPACE.
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.
C Example
#include "visa.h"
#define ADD_OFFSET (addr, offs) (((ViPByte)addr) + (offs))
int main(void)
{
ViStatus status; /* For checking errors */
ViSession defaultRM, /* Communication channels */
ViAddr self; /* User pointer */
ViBusAddress address; /* Shared memory offset */
ViUInt16 offset; /* Shared memory space */
ViUInt16 addrSpace; /* To store register value */
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;
}
Visual Basic Example
Private Sub vbMain()
Dim stat As ViStatus
Dim dfltRM As ViSession
Dim self As ViSession
Dim addr As ViAddr
Dim offs As Long
Dim mSpace As Integer
Dim Value As Integer
Rem Begin by initializing the system
stat = viOpenDefaultRM(dfltRM)
If (stat < VI_SUCCESS) Then
Rem Error initializing VISA...exiting
Exit Sub
End If
Rem Open communication with VXI Device at Logical Address 0
Rem NOTE: For simplicity, we will not show error checking
stat = viOpen(dfltRM, "VXI0::0::INSTR", VI_NULL, VI_NULL, self)
Rem Allocate a portion of the device's memory
stat = viMemAlloc(self, &H100, offs)
Rem Determine where the shared memory resides
stat = viGetAttribute(self, VI_ATTR_MEM_SPACE, mSpace)
stat = viMapAddress(self, mSpace, offs, &H100, VI_FALSE, VI_NULL, addr)
viPeek16 self, addr, Value
Rem Access a different register by manipulating the pointer.
viPeek16 self, addr + 2, Value
stat = viUnmapAddress(self)
stat = viMemFree(self, offs)
Rem Close down the system
stat = viClose(self)
stat = viClose(dfltRM)
End Sub