EPICS Primer

Introduction

This document is intended to serve as a starting point for anyone starting to use EPICS for the first time. It is in no way self contained and you will need to refer to more complete documentation for many things. A good source of documentation is at Cebaf:
"http://www.cebaf.gov/accel/documents/epics_doc.html"
Follow the links under The EPICS System Documents . Before attempting anything I would suggest you read the following: The rest of this document is based on what I have been doing in Rutherford, i.e.
  1. Make a small database of EPICS records.
  2. Get the EPICS code loaded and running on a VxWorks micro.
  3. Do some operations on the records from the VxWorks Shell.
  4. Access the records from a UNIX station (Channel Access).
  5. (Use Tcl/Tk to do channel access with a graphical interface.)

Makeing a Database

A database containing a few linked records is relatively easy to make. I suspect, however, that you will have to read the Record Reference Manual and the Application Developer's Guide quite carefully to make a useful (and functional) database.

To make a database (or just a set of records ??) use gdct (documentation available at the Cebaf site). This program produces two files: myDbase and myDbase.db. It is the latter file that is loaded into the VxWorks station. For example: myDbase.db


grecord(stringin,"inMessage")
grecord(ai,"dummyADC")
grecord(ai,"dummyDAC")
grecord(calc,"dummySum")
{
        field(CALC,"A+B")
        field(INPA,"dummyDAC.VAL  PP MS")
        field(INPB,"dummyADC.VAL  PP MS")
}

See the gdct display for this file.

Minimal EPICS

To get EPICS running in an IOC, with a small database (myDBase) put the line:
< myEpics.cmd
in the startup.cmd script. The file myEpics.cmd is:

#
# Startup script for "minimal" EPICS
#

# Location of EPICS code
cd "  "

# Load the object code
ld < targetmv167/iocCore
ld < drv/O.mv167/drvSup
ld < dev/O.mv167/devSup
ld < rec/O.mv167/recSup
ld < targetmv167/initHooks.o
ld < targetmv167/seq 

# Load the dbase records
dbLoad("db/default.dctsdr")
dbLoadRecords("db/myDBase.db")

# Kick-start EPICS 
iocLogDisable 1
iocInit "resource.def"

 
The file resource.def contains some global variable definitions (See the Application Developer's Guide, chapter 11):

EPICS_IOC_LOG_INET DBF_STRING 155.198.211.64
EPICS_CA_CONN_TMO DBF_STRING 4.0
EPICS_CA_BEACON_PERIOD DBF_STRING 2.0
EPICS_TS_MIN_WEST DBF_STRING 0
EPICS_CA_AUTO_ADDR_LIST DBF_STRING YES
EPICS_AR_PORT DBF_STRING 7002

Accessing the database from VxWorks

There are a number of functions executable from the VxWorks shell providing database access. To test whether you have successfully loaded down your EPICS database, try: The available functions are detailed in the Application Developers's Guide .

Channel Access

Communication between OPIs and IOCs on the same LAN probably works "automatically". However if you want to access an IOC from somewhere else you will need to inform EPICS of the IOC's IP address. You can do this by by setting one of the EPICS environment variables, e.g.
export EPICS_CA_ADDR_LIST="xx.xx.xx.xx"
The following program will read the VAL field of the dummySum record. Consult the Channel Access Reference Manual for the function definitions. Note in particular that channel access messages are buffered.

#include 
#include "cadef.h"

main()
{
  int status;
  chid dummySum;
  double sum;

  /* Search for the channel */
  status = ca_search("dummySum.VAL", &dummySum);
  printf("ca_search: %s\n", ca_message(status));

  /* Flush the message buffer, i.e. force ca_search to complete */
  status = ca_pend_io(0);
  printf("ca_pend_io: %s\n", ca_message(status));

  /* Read the VAL field */
  status = ca_get(DBR_DOUBLE, dummySum, &sum);
  printf("ca_get: %s\n", ca_message(status));

  /* Flush the message buffer, i.e. force ca_get to complete */
  status = ca_pend_io(0);
  printf("ca_pend_io: %s\n", ca_message(status));

  printf("dummySum.VAL: %f\n", sum);
  exit(0);
}

To compile and link it use the following makefile:

#
# Makefile for testCA
#

# Where to look for EPICS header files
IFLAGS=-I/data/babar/devel/epics/R3.12.2/base/include

# Where to look for EPICS libraries
LFLAGS=-L/data/babar/devel/epics/R3.12.2/base/lib/solaris

# EPICS libraries to link
EPICSLIBS= -lca -lCom -lDb

# Other libraries (libm.a is the mathematics library)
OTHERLIBS= -lsocket -lnsl -lelf -laio -lm

# Link
testCA: testCA.o
        gcc $(LFLAGS) -o testCA testCA.o $(EPICSLIBS) $(OTHERLIBS)

# Compile
testCA.o: testCA.c
        gcc -c $(IFLAGS) -o testCA.o testCA.c

Bits and Pieces


Paul Maley pdm@aloha.cern.ch