# Presentation: 727577

## Python ChannelAccess Interface(CaChannel)

**Geoff Savage**
**EPICS Collaboration Meeting**
**16 November 2000**

## Why Python?

**“****Python is an *****interpreted, interactive, object-oriented***** programming language”**
**Fermilab experience**
**Reduced development time**
**Portable (Linux, NT, OSF1)**
**Graphics (Tcl/Tk)**
**Powerful extension library**
**Easy to extend (SWIG)**
**Short learning curve**
**www.python.org**

- Python/CA

- 11/16/00

## Introduction

**Implement channel access in a Python class for use in control system applications**
**First the channel access library must be wrapped**
**Each Python function wraps a CA “C” function**
**Additional C functions are needed to complete the wrap**
**Internal functions**
**callbacks**
**file descriptor manager**
**Helper functions**
**ca macros**
**db macros**

- Python/CA

- 11/16/00

## Software Layers

**EPICS channel access C library**
** ****C functions**
** ****Macros**
** ****Data structures**
** ****Constants**

**caPython**
** ****Python wrapper around the C library**
** ****Collection of python functions**
** ****Software wrapper and interface****generator (SWIG)**

**CaChannel**
** ****Python class**
** ****Implemented using caPython****functions and SWIG pointer library**

- Python/CA

- 11/16/00

## Examples

**Interactive**
**>>> from CaChannel import ***
**>>> ch = CaChannel()**
**>>> ch.searchw('catest')**
**>>> ch.putw(123.456)**
**>>> ch.getw()**
**123.456**

**Script**
**from CaChannel import ***
**def main():**
**  ****try:**
**    ****catest = CaChannel()**
**    ****catest.searchw('catest')**
**    ****catest.putw(123.456)**
**    ****print catest.getw()**
**  ****except CaChannelException, status:**
**    ****print ca.message(status)**
**main()**

- Python/CA

- 11/16/00

## CaChannel Methods 

**Connection to a process variable (PV)**
**search_and_connect**
**clear_channel**
**Write to a PV**
**array_put**
**array_put_callback**
**Read from a PV**
**array_get**
**array_get_callback**
**Monitoring a PV**
**add_masked_array_event**
**clear_event**

- Python/CA

- 11/16/00

## CaChannel Methods

**Send the requests (search, get, put) to an IOC (server)**
**pend_io, pend_event, flush_io**
**PV information**
**field_type, element_count, name, state, host_name, read_access, write_access**
**Convenient PV access**
**Added by us to make PV access easier**
**searchw = search + pend_io**
**putw = array_put + pend_io**
**getw = array_get + pend_io**

- Python/CA

- 11/16/00

## Callbacks

**Users write callback functions in Python**
**The standard set of EPICS callbacks is supported**
**Connection**
**Put**
**Get**
**Value, Status, Time, Graphic, Control**
**Monitors**
**Value, Log, Alarm**
**Internal functions move data from C to Python**

- Python/CA

- 11/16/00

## Monitor Callback

**def eventCb(epics_args, user_args):**
**  ****print ca.message(epics_args['status'])**
**  ****print "new value = ",epics_args['pv_value']**
**  ****print ca.alarmSeverityString(**
**                   ****epics_args['pv_severity'])**
**  ****print ca.alarmStatusString(**
**                   ****epics_args['pv_status'])**
**try:**
**  ****ch = CaChannel()**
**  ****ch.searchw('catest')**
**  ****ch.add_masked_array_event(**
**    ****ca.dbf_type_to_DBR_STS(ch.field_type()),**
**    ****None,**
**    ****ca.DBE_VALUE | ca.DBE_ALARM,**
**    ****eventCb)**
**  ****ch.pend_io()**
**except CaChannelException, status:**
**  ****print ca.message(status)**

- Python/CA

- 11/16/00

## CA FunctionsNot Wrapped

**ca_change_connection_event**
**use ca_search_and_connect**
**ca_add_exception_event**
**ca_replace_printf_handler**
**ca_replace_access_rights_event**
**ca_puser macro **
**not used**
**ca_test_event**

- Python/CA

- 11/16/00

## Issues and Plans

**Issues**
**Scan groups included but not tested**
**fd manager implementation appears to have a memory leak (Janousch Markus)**
**Plans**
**Complete the wrap**
**Suitability of next CA version for use in Python**

- Python/CA

- 11/16/00

## Summary

**Extensive experience using CaChannel**
**CaChannel used in all control and monitoring GUIs at DZero**
**Easy for novices to use Python and CaChannel**

- Python/CA

- 11/16/00

## Downloading CaChannel

**www.aps.anl.gov/epics/****->host software->CaPython**

- Python/CA

- 11/16/00