next up previous
Next: Compiling Up: APS runControl Library Previous: Introduction

Sample Application

The runControl library is a set of three C functions for communicating with an EPICS runcontrol record. Here is a simple program depicting proper use of the library.

#include <stdio.h>
#include <string.h>
#include <cadef.h>
#include <libruncontrol.h>
int main(int argc, char *argv[]) {
  char pv[255];
  char handle[255];
  int status;
  RUNCONTROL_INFO rcInfo;

  if (argc < 2) {
     fprintf(stderr,"usage: %s <description-string>\n\n",argv[0]);
     return(1);
  }
  
  /* NULL means the next available runcontrol record should be used.
     argv[1] is the unique application description string.
     A ping timeout of 2000 milliseconds will be used. The timeout is
     used by the IOC to check if the client has stopped responding.
     The handle is used for subsequent calls.
     rcInfo is a RUNCONTROL_INFO structure.
     A pend IO timeout of 10 seconds will be used. This timeout is
     used by the client to check if the IOC has stopped responding.
  */

  sprintf(pv, ``oag:ControlLawRC'');
  status = runControlInit(pv, argv[1], 2000.0, handle, &rcInfo, 10);
  if (status != RUNCONTROL_OK) {
    fprintf(stderr,"ERROR initializing run control\n");
    return(1);
  }
  while (1) {
    /* Note: application may suspend inside the runControlPing() call */
    status = runControlPing(handle, &rcInfo);
    switch (status) {
    case RUNCONTROL_ABORT:
      fprintf(stderr,"Application aborted\n");
      return(1);
    case RUNCONTROL_TIMEOUT:
      fprintf(stderr,"Application timed out\n");
      return(1);
    case RUNCONTROL_OK:
      /* do your application work here, but don't take more than 2000 ms */
      sleep(1);
      /* Send some useful status message and set alarm severity if you wish. */
      status = runControlLogMessage(handle, "informative message", NO_ALARM, &rcInfo);
      if (status != RUNCONTROL_OK) {
         fprintf(stderr,"Unable to write status message and alarm severity\n");
         return(1);
      }
      break;
    case RUNCONTROL_ERROR:
      fprintf(stderr,"Communications error with runcontrol record\n");
      return(1);
    default:
      fprintf(stderr,"Unknown error code\n");
      return(1);
    }
  }
  /* When loop above is done, exit gracefully. May have to do this as part
     of a kill signal handler if that is how you stop your application.
  */
  status = runControlExit(handle, &rcInfo);
  if (status != RUNCONTROL_OK) {
    fprintf(stderr,"ERROR during exit run control\n");
    return(1);
  }
  return(0);
}



Robert Soliday 2005-04-29