1 Programming EPICS with PERL
Total Page:16
File Type:pdf, Size:1020Kb
Outline • History Programming EPICS with • Quick Overview of Perl PERL • EPICS Overview • Channel Access (EZCA) Mohan Ramanathan • Perl interface to EZCA October 12, 2004 • Perl Applications • Current and Future Part of the EPICS “Getting Started” Lecture Series • Conclusions • Acknowledgments Argonne National Laboratory A U.S. Department of Energy 2 Office of Science Laboratory Office of Science Pioneering Office of Science U.S. Department of Energy Operated by The University of Chicago Science and U.S. Department Technology of Energy History History • Why Perl? • Perl is an easy language for WEB programming (CGI) Back in 1995/96 was looking for a language to use for various Needed an easy to program , yet powerful language to write applications code to interface to CA, SDDS and for writing CGI interface C was too cumbersome Needed a quick and easy to use, yet a powerful language • Object oriented Tcl/Tk was used at APS but was not properly suited for the • Easy to interface to other languages like C, C++, java etc.. applications I was interested in (mainly WEB interface) Easy to learn !! Perl has simple constructs and looks similar to C • Preexisting objects modules are available in abundance Numerous modules available in CPAN for graphics and GUI http://www.cpan.org pTK was already available for Perl in 1996 Interpreter based language means no compilation • Perl is free! Full programs can be generated easily with Perl • Great support and free! • Perl EZCA module was born in 1996 3 4 Pioneering Office of Science Pioneering Office of Science Science and U.S. Department Science and U.S. Department Technology of Energy Technology of Energy 1 Quick Overview of Perl Quick Overview of Perl • Data types • A high-level interpreted programming language Scalars need no assignment. Internally all scalars are assigned to • Modern programming language double. However for users it takes the form of the data it holds Data Structures Arrays can be multi-dimensional. However internally it is only one Control Structures dimension Regular Expressions • Variable Names Object Oriented All scalars start with $ • Perl handles both strings and numbers elegantly All arrays start with @ Excellent for manipulation of both strings and numbers All hash reference start with % Most string and arithmetic operators are similar to C All operators and most functions work on scalar or array data of • Has special variables and arrays like $_, @_, @ARGV, %ENV, etc.. any type • User has a choice of (not) using special cryptic variables !! • Multi-platform support • Memory allocation is dynamic – will use up all memory when Unix, Linux, Windows, etc.. needed ! 5 6 Pioneering Office of Science Pioneering Office of Science Science and U.S. Department Science and U.S. Department Technology of Energy Technology of Energy Quick Overview of Perl - Operators Quick Overview of Perl - Control • Binary and String Operators • if and unless statements $a = 123; $b = 456; if ($level < 0) { $c = 3; print "Level is below Zero \n"; print $a + $b; # prints 579 (addition) } print $a . $b; # prints 123456 (concatenation) print "City is not New York \n" unless ($city eq "New York"); print $a x $c; # prints 123123123 (repeat) • while and until statements • Logical Operators while ($level < 100) { $a && $b # $a if $a is false, $b otherwise $level++; ! $a # True if $a is not true print "level value is $level\n"; } • Numeric and String Comparison Operators until ($level == 0) { $a == $b # True if $a is equal to $b (numeric) $level--; $a eq $b # True if $a is equal to $b (string) print "level value is $level\n"; } • File Test Operators -e $a # True if file named in $a exists 7 8 Pioneering Office of Science Pioneering Office of Science Science and U.S. Department Science and U.S. Department Technology of Energy Technology of Energy 2 Quick Overview of Perl - Control EPICS Overview • for statements for ($level =0; $level <100; $level++) { print "level value is $level\n"; } MEDM MEDM Client Client Perl Client • foreach statements foreach $user (@users) { Channel Access print "$user is ok\n"; Server } •PV Gateway Client • next foreach $user (@users) { next unless ($user eq "jack"); } Server IOC IOC IOC • last foreach $user (@users) { Meter Power Supply Camera last if ($user eq "jack"); } 9 10 Pioneering Office of Science Pioneering Office of Science Science and U.S. Department Science and U.S. Department Technology of Energy Technology of Energy Channel Access (EZCA) Perl Interface to EZCA EPICS extension for “E-Z (Easy) Channel Access” Description • EZCA.pm is a Perl interface to EPICS EZCA library • EZCA provides a simplified interface to Channel Access, which • The routine names are based on EZCA nomenclature is useful for some C programs which do not need all the • Each routine name in EZCA has an equivalent in Perl module capabilities of the full API • All the commands are similar to what is used in EZCA • Data types as described in EZCA (ezcatype) are used • Don’t need to handle chids, just use PV name strings – hash ezcaByte table ezcaString ezcaShort • Synchronous APIs - applications don’t have to handle ezcaLong callbacks ezcaFloat ezcaDouble • Has only 25 routines to handle synchronous groups, individual requests and easy error handling 11 12 Pioneering Office of Science Pioneering Office of Science Science and U.S. Department Science and U.S. Department Technology of Energy Technology of Energy 3 Perl Interface to EZCA Perl Interface to EZCA Return Codes Work in Progress • Most function calls return status codes indicating the success/failure of the call • Most functions with the exception of the Group function have • Following are the return codes been implemented and tested EZCA_OK 21 of the 25 functions are working EZCA_INVALIDARG EZCA_FAILEDMALLOC • All functions involving arrays have a limitation of 2000 EZCA_CAFAILURE elements EZCA_UDFREG EZCA_NOTCONNECTED • In the case of PV Put at this time only one element is possible EZCA_NOTIMELYRESPONSE EZCA_INGROUP EZCA_NOTINGROUP 13 14 Pioneering Office of Science Pioneering Office of Science Science and U.S. Department Science and U.S. Department Technology of Energy Technology of Energy Perl Interface to EZCA Perl Interface to EZCA Main Functions Associated Functions • @data = EZCA::Get($pvname,"ezcatype",$no_elem) Retrieves values from $pvname and the data type is specified as ezcatype • $status = EZCA::GetNelem($pvname,$nelem) The return status code is stored in $data[0] and the retrieved values are in the The number of elements in the $pvname is returned in $nelem. rest of the array starting at elements $data[1] At this time the no of elements $no_elem is limited to 2000 • $status = EZCA::GetPrecision($pvname,$prec) The precision for the $pvname is returned in $prec. • $status = EZCA::Put($pvname,"ezcatype",$no_elem,$data) Write values into $pvname. • $status = EZCA::GetUnits($pvname,$unit) The value is send in $data and the data type is specified as ezcatype. This returns the Engineering Unit field of the $pvname in $unit. The return status code is $status At this time the no of elements $no_elem is limited to 1 • $status = EZCA::GetControlLimits($pvname, $low, $high) Retrieve the Control Limits values in $low and $high. • EZCA::GetStatus($pvname,$tstamp,$tnstamp,$status,$severity) Retrieves various information about the said $pvname. • $status = EZCA::GetGraphicLimits($pvname, $low, $high) The time in seconds is in $tstamp and in nanoseconds is in $tnstamp. Retrieve the Graphics Limits values in $low and $high. The $status and $severity returns status and the severity of the process variable 15 16 Pioneering Office of Science Pioneering Office of Science Science and U.S. Department Science and U.S. Department Technology of Energy Technology of Energy 4 Perl Interface to EZCA Perl Interface to EZCA Monitors Error Handling • $status = EZCA::SetMonitor($pvname,"ezcatype") • EZCA::AutoErrorMessageOn() Used to set a monitor on the $pvname. Turn ON the EZCA error messages to be printed on the stdout. • $status = EZCA::ClearMonitor($pvname,"ezcatype") • EZCA::AutoErrorMessageOff() Used to clear any monitors placed on the $pvname. Turn OFF the EZCA error messages to be printed on the stdout. • EZCA::Perror($prefix) • $mon = EZCA::NewMonitorValue($pvname,"ezcatype") Use this to optionally add a prefix message to the EZCA error message. Use this function to poll for new values on a previously set monitor. A non zero return value in $mon indicates a new value since the last time the value • was read via a Get EZCA::GetErrorString(NULL, $stringBuff) Retrieve the EZCA error message in the $stringBuff instead of stdout. • $status = EZCA::Delay($seconds) • Use to introduce delays between calls. EZCA::Free($stringBuff) The time $seconds is in seconds. Fractional time are allowed. Use this to free up the $stringBuff used in the above call. 17 18 Pioneering Office of Science Pioneering Office of Science Science and U.S. Department Science and U.S. Department Technology of Energy Technology of Energy Perl Interface to EZCA Perl Applications Tuning EZCA •Use EZCA and GD modules to generate images every minute for WEB #!/usr/bin/perl ################################################################## # Program to retrieve the data from EPICS and generate • $seconds = EZCA::GetTimeout() # png files of Storage ring history. Retrieve the current CA timeout value in seconds ################################################################## use Time::CTime; # load Time module for manipulating time use EZCA;