ibnotify Usage Example

NI-488.2

ibnotify Usage Example


int __stdcall MyCallback (int LocalUd, unsigned long LocalIbsta, unsigned long LocalIberr, unsigned long LocalIbcnt, void *RefData);
int ReadingsTaken = 0;
float Readings[1000];
BOOL DeviceError = FALSE;
char expectedResponse = 0x43;

int main() 
{
   int ud;

   // Assign a unique identifier to the device and store it in the 
   // variable ud. ibdev opens an available device and assigns it to 
   // access GPIB0 with a primary address of 1, a secondary address of 0,
   // a timeout of 10 seconds, the END message enabled, and the EOS mode 
   // disabled. If ud is less than zero, then print an error message 
   // that the call failed and exit the program.
   ud = ibdev   (0,   // connect board
                 1,   // primary address of GPIB device
                 0,   // secondary address of GPIB device
                 T10s,   // 10 second I/O timeout
                 1,   // EOT mode turned on
                 0);   // EOS mode disabled

   if (ud < 0)  {
      printf ("ibdev failed.\n");
      return 0;
   }

   // Issue a request to the device to send the data. If the ERR bit 
   // is set in Ibsta, then print an error message that the call failed 
   // and exit the program.
   ibwrt (ud, "SEND DATA", 9L);
   if (Ibsta() & ERR)  {
      printf ("unable to write to device.\n");
      return 0;
   }
   // set up the asynchronous event notification on RQS
   ibnotify (ud, RQS, MyCallback, NULL);
   if (Ibsta() & ERR)  {
      printf ("ibnotify call failed.\n");
      return 0;
   }

   while ((ReadingsTaken < 1000) && !(DeviceError))  {
      // Your application does useful work here. For example, it
      // might process the device readings or do any other useful work.
   }
   
   // disable notification
   ibnotify (ud, 0, NULL, NULL);

   // Call the ibonl function to disable the hardware and software.
   ibonl (ud, 0);
   return 1;

}

int __stdcall MyCallback (int LocalUd, unsigned long LocalIbsta, unsigned long LocalIberr, 
      unsigned long LocalIbcnt, void *RefData)
{
   char SpollByte;
   char ReadBuffer[40];
   // If the ERR bit is set in LocalIbsta, then print an error message 
   // and return.
   if (LocalIbsta & ERR)  {
      printf ("GPIB error %d has occurred. No more callbacks.\n", 
         LocalIberr);
      DeviceError = TRUE;
      return 0;
   }
   
   // Read the serial poll byte from the device. If the ERR bit is set
   // in LocalIbsta, then print an error message and return.
   LocalIbsta = ibrsp (LocalUd, &SpollByte);
   if (LocalIbsta & ERR)  {
      printf ("ibrsp failed. No more callbacks.\n");
      DeviceError = TRUE;
      return 0;
   }

   // If the returned status byte equals the expected response, then 
   // the device has valid data to send; otherwise it has a fault 
   // condition to report.
   if (SpollByte != expectedResponse)   {
      printf ("Device returned invalid response. Status byte = 0x%x\n", 
            SpollByte);
      DeviceError = TRUE;
      return 0;
   }

   // Read the data from the device. If the ERR bit is set in LocalIbsta, 
   // then print an error message and return.
   LocalIbsta = ibrd (LocalUd, ReadBuffer, 40L);
   if (LocalIbsta & ERR)  {
      printf ("ibrd failed. No more callbacks.\n");
      DeviceError = TRUE;
      return 0;
   }

   // The string returned by ibrd is a binary string whose length is 
   // specified by the byte count in Ibcnt. However, many GPIB
   // instruments return ASCII data strings and this example makes this
   // assumption. Because of this, it is possible to add a NULL
   // character to the end of the data received and use the printf()
   // function to display the ASCII data. The following code
   // illustrates that.
   ReadBuffer[LocalIbcnt()] = '\0';

   // Convert the data into a numeric value.
   sscanf (ReadBuffer, "%f", &Readings[ReadingsTaken]);

   // Display the data.
   Printf ("Reading : %f\n", Readings [ReadingsTaken]);

   ReadingsTaken += 1;
   if (ReadingsTaken >= 1000)  {
      return 0;
   }
   else  {

      // Issue a request to the device to send the data and rearm
      // callback on RQS.
      LocalIbsta = ibwrt (LocalUd, "SEND DATA", 9L);
      if (LocalIbsta & ERR)  {
         printf ("ibwrt failed. No more callbacks.\n");
         DeviceError = TRUE;
         return 0;
      }
      else  {
         return RQS;
      }
   }
}


Related Topics:

ibnotify Usage

ibnotify

GpibNotify

GpibNotify Usage