SRQ Event in Visual Basic .NET with VISA-C

Status
Not open for further replies.

pancho_hideboo

Advanced Member level 5
Joined
Oct 21, 2006
Messages
2,847
Helped
767
Reputation
1,536
Reaction score
733
Trophy points
1,393
Location
Real Homeless
Activity points
17,490
I would like to treat SRQ(Service Request) Event in "Visual Basic .NET".
I use VISA-C API function in Visual Basic .NET.

However I can not succeed in this task.

How can I implement this task ?

BTW, I don't use VISA-COM.

For example, the following code is written in C.
I would like to convert this code to Visual Basic .NET or C#.
Code:
/* srqhdlr.c
   This example program illustrates installing an event handler to
   be called when an SRQ interrupt occurs. Note that you must
   change the address. */

#include <visa.h>
#include <stdio.h>
#if defined (_WIN32)
   #include <windows.h> /* for Sleep() */
   #define YIELD   Sleep( 10 )
#elif defined (__BORLANDC__)
   #include <windows.h>  /* for Yield() */
   #define YIELD Yield()
#elif defined (_WINDOWS)
   #include <io.h>      /* for _wyield */
   #define YIELD   _wyield()
#else
   #include <unistd.h>
   #define YIELD sleep (1)
#endif

int srqOccurred;

/* trigger event handler */
ViStatus _VI_FUNCH mySrqHdlr(ViSession vi, ViEventType eventType,
                          ViEvent ctx, ViAddr userHdlr){

   ViUInt16 statusByte;
   
   /* make sure it is an SRQ event */
   if(eventType!=VI_EVENT_SERVICE_REQ){
      /* Stray event, so ignore */
      printf( "\nStray event of type 0x%lx\n", eventType );
      return VI_SUCCESS;
   }

   /* print the event information */
   printf("\nSRQ Event Occurred!\n");
   printf("...Original Device Session = %ld\n", vi);

   /* get the status byte */
   viReadSTB(vi, &statusByte);
   printf("...Status byte is 0x%x\n", statusByte);

   srqOccurred = 1;
   return VI_SUCCESS;
}

void main(){
   ViSession defaultRM,vi;
   long      count;

   /* open session to message based VXI device */
   viOpenDefaultRM(&defaultRM);
   viOpen(defaultRM, "GPIB-VXI0::24::INSTR", VI_NULL, VI_NULL, &vi);

   /* Enable command error events */
   viPrintf( vi, "*ESE 32\n" );

   /* Enable event register interrupts */
   viPrintf( vi, "*SRE 32\n" );

   /* install the handler and enable it */
   viInstallHandler(vi, VI_EVENT_SERVICE_REQ, mySrqHdlr, (ViAddr)10);
   viEnableEvent(vi, VI_EVENT_SERVICE_REQ, VI_HNDLR, VI_NULL);
   
   srqOccurred = 0;
   
   /* Send a bogus command to the message based device to cause an SRQ */
   /* Note: 'IDN' causes the error -- '*IDN?' is the correct syntax */
   viPrintf( vi, "IDN\n" );
   
   /* Wait a while for the SRQ to be generated and for the handler */
   /* to be called. Print something while we wait */
   printf( "Waiting for an SRQ to be generated ." );
   for ( count = 0 ; (count < 10) && (srqOccurred == 0) ; count++ ) {
      long count2 = 0;
      printf( "." );
      while ( (count2++ < 100) && (srqOccurred ==0) ){
         YIELD;
      }
   }
   printf( "\n" );
    
   /* disable and uninstall the handler */
   viDisableEvent(vi, VI_EVENT_SERVICE_REQ, VI_HNDLR);
   viUninstallHandler(vi, VI_EVENT_SERVICE_REQ, mySrqHdlr, (ViAddr)10);
   
   /* Clean up after ourselves - don't leave device in error state */
   viPrintf( vi, "*CLS\n" );
   
   /* close the sessions */
   viClose(vi);
   viClose(defaultRM);
   
   printf( "End of program\n" );
}
 

Self followup.
Code:
VISA cannot call back to a Visual Basic function.
Thus, you can only use the queuing mechanism in viEnableEvent.
There is no way to install a VISA event handler in Visual Basic.

So we can not convert followings to VB or VC#.
Code:
ViStatus _VI_FUNCH mySrqHdlr(ViSession vi, ViEventType eventType,
                          ViEvent ctx, ViAddr userHdlr)

viInstallHandler(vi, VI_EVENT_SERVICE_REQ, mySrqHdlr, (ViAddr)10);

We can implement SRQ by Using the Queuing Method.
Code:
viEnableEvent(vi, VI_EVENT_SERVICE_REQ, VI_QUEUE, VI_NULL);

viWaitOnEvent(vi, VI_EVENT_SERVICE_REQ, VI_TMO_INFINITE, &eventType, &event);
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…