Building EPICS Environment on Windows and Example Usage of CA.DLL

Takashi OBINA Takashi NOGAMI

Photon Factory, KEK Outline

● Time Table ● 30min : Talk ● 30min : Hands-on ● bulid example Application on Windows ● 30min : Demo with Visual Basic

● Table of Contents ● CA Client/Server on Windows? ● Requirement, Prebuild setup ● Getting EPICS source code ● Build EPICS base ● How to create EPICS application on Windows ● How to use CA.DLL from Visual Basic TOC

● CA Client/Server on Windows? ● Requirement, Prebuild setup ● Getting EPICS source code ● Build EPICS base ● How to create EPICS application on Windows ● How to use CA.DLL from Visual Basic CA Client/Server on Windows

● CA Client ● Advantage ● Rapid application development tool ● Debugger ● Disadvantage ● Cost? ● Stability?

● CA Server ● How to control from remote host? ● Stability? ● Windows update? ● Security? CA Client/Server on Windows (cont.)

● This is my opinion: ● LabView? ● I don't want to relay upon 1 Product / 1 Company ● VC?, Version, security, long term stability of PC ● If you use 1 PC for control, LabView is a good soluti on, however ...... If you want to compare the data to another parameters, such as beam charge, beam p osition, vacuum pressure, etc... ● If you are a good programmer and can do everythin g by youself, and no need to communicate with othe r person/groups, there are less reason to adopt EPI CS TOC

● CA Client/Server on Windows? ● Requirement, Prebuild setup ● Getting EPICS source code ● Build EPICS base ● How to create EPICS application on Windows ● How to use CA.DLL from Visual Basic What is your favorite environment?

● cygwin ● Unix like environment, GNU tools ● Easy-to-use installation tool is available ● X-window emulator included ● http://www.cygwin.com/

● Microsoft ● Visual C++6.0, Visual C .NET, Visual Studio 2005

● MinGW ● Minimalist GNU for Win32 ● Free GNU C compiler + ● http://www.mingw.org/

● Borland C++ ● http://www.borland.com/ Microsoft Visual C++ selection

● 3 versions ● Visual C++ 6.0 : VC6 ● Visual C++ .NET : VC7 ● Visual Studio .NET : VC8

● Default compiler for EPICS : VC7 ● VC6 / VC8 also works. ● VC8 not tested well, however

● Today's talk ● Visual Studio .NET "Express Edition" ● Download / Install from Microsoft Web site ● FREE

● In your PC, "VC6" is already installed Environment Setup -- Prebuild(1)

● Install "Visual Studio 2005 Express Edition" ● Visual C++ 2005 Express Edition ● Visual Basic 2005 Express Edition ● Windows Platform SDK ● GNU make ● GNU http://www.gnu.org/ ● cygwin gmake ● ● CPAN ● Active Perl (http://www.activestate.com/) ● cygwin perl It is possible to use cygwin, however, I tried to build EPI CS without cygwin environenet today. (Because if you alread y installed cygwin, you can build EPICS inside cygwin envir onment. It is relatively easier than VC6.) Today, I build CA.DLL and CA.LIB using MSVC. Setup – Prebuild (2)

● Check PATH ● perl, make must be included in your PATH.

C:\obina>perl -v This is perl, v5.8.8 built for MSWin32-x86-multi-thread (with 25 registered patches, see perl -V for more detail) ...

C:\obina>make -v GNU Make 3.80 Copyright (C) 2002 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Setup -- Prebuild (3) : Optional

● Directory Structure Example ● EPICS --> C:\VECC\base3.14.8.2 ● Create .BAT file for VC and EPICS (only for co nvenience) ● %SystemRoot%\system32\cmd.exe /K "C:\vecc\epic s\DevEnv.bat" set EPICS_HOST_ARCH=win32-x86 set EPICS_BASE=C:\VECC\base-3.14.8.2 set EPICS_BIN=%EPICS_BASE%\bin\%EPICS_HOST_ARCH% set EPICS_CA_ADDR_LIST=172.xxx.yyy.255 set EPICS_CA_AUTO_ADDR_LIST=NO set PATH=%EPICS_BIN%;%PATH%

"%VS80COMNTOOLS%vsvars32.bat"

After the successful build of EPICS: You should set the environment variable (EPICS_CA..., etc) with Control Panel -> System -> Detail -> Environment Variables TOC

● CA Client/Server on Windows? ● Requirement, Prebuild setup ● Getting EPICS source code ● Build EPICS base ● How to create EPICS application on Windows ● How to use CA.DLL from Visual Basic Building EPICS base (1)

● Download from http://www.aps.anl.gov/epics/ ● At first, please read documentation carefully. ● Using the "Latest" version is important !!! ● Many number of bug fixes are exist in Windows port. ● I personally felt that.... port was relatively stable and easy...but now, things are getting better. ● Expand to some directory ● For example C:\VEEC\base-3.14.8

● Edit : CONFIG_SITE.win32-x86.win32-x86 ● in %EPICS_BASE%\configure\os ● Edit : CONFIG_SITE, CONFIG_SITE_ENV ● in %EPICS_BASE%\configure\ make! for VC6

● you must modify some files ● I will explain the detail procedure in the last pa rt of this file...

● please forget for a while. ● EPICS base are already build and installed in your PC. EPICS base Build (2)

● Check ● Binary file, Environment Variables ● EPICS_BASE = C:\pf\epics\base-3.14.8.2cvs ● %EPICS_BASE%\bin\win32-x86 ● %EPICS_BASE%\lib\win32-x86 ● %PATH% include "bin" directory? ● Execute "excas" in bin directory ● example record : jane, fred, freddy, etc ● caget ● camonitor ● If you've got an error: ● security setting (Firewall of Windows, Norton, etc) ● You MUST be connected to network ● check environment variable ● EPICS_CA_ADDR_LIST ● EPICS_CA_AUTO_ADDR_LIST Useful command-line Tool (General)

● Show network port statistics ● netstat -a ● netstat -o : Windows XP ● netstat -b : WinXP SP2 ● tasklist , taskkill : Win XP Pro ● Use "grep" on Linux, "findstr" on Windows ● Linux: netstat -a | grep 5064 ● Win: netstat -a | findstr 5064 set | findstr EPICS findstr /i <---- ignore case findstr /s <---- search subdir ● Find a file in sub-directory ● example: dir /s makeBase* ● Command line suppliment ● Cursor, , TOC

● CA Client/Server on Windows? ● Requirement, Prebuild setup ● Getting EPICS source code ● Build EPICS base ● How to create EPICS application on Windows ● How to use CA.DLL from Visual Basic EPICS Application

● Create EPICS Application ● makeBaseApp.pl makeBaseApp.pl -h :show help makeBaseApp.pl -l : list application types

Example 1 makeBaseApp.pl -t example example makeBaseApp.pl -i -t example example make cd iocBoot\iocexample ..\..\bin\win32-x86\example st.cmd

Example 2 makeBaseApp.pl -t caClient caex make EPICS Application (2)

● Execute "example.exe st.cmd" (prev. page) ● Open a new cmd window ● test caget/caput ● caget fooHost:aiExample ● camonitor fooHost:aiExample ● caput fooHost:xxxExample

● In order to create command-line program, use "makeBaseApp.pl" and check the source code is easy way. ● To create GUI, IDE of Visual C is possible ● Visual Basic / Delphi / C++ Builder is also OK Hands-on 1

● Open a command window ● Check environment C:\> cd \ C:\> set | more C:\> set | findstr /i epics C:\> make -v C:\> perl -v C:\> cl Hands-on 2

● Create an Example Application

C:\> cd \ C:\> mkdir group1 <--- replace by your groupname C:\> cd group1 C:\group1> makeBaseApp.pl -t example myexample C:\group1> makeBaseApp.pl -i -t example myexample C:\group1> make ..... wait for a while ..... C:\group1> cd iocBoot\iocmyexample C:\group1\iocBoot\iocmyexample> notepad st.cmd ..... edit "st.cmd" ..... dbLoadRecords("db/dbExample1.db","user=lenovoHost") replace dbLoadRecords("db/dbExample1.db","user=group1")

C:\group1\**\> ..\..\bin\win32-x86\example.exe st.cmd ..... open another cmd window, try caget, camonitor TOC

● CA Client/Server on Windows? ● Requirement, Prebuild setup ● Getting EPICS source code ● Build EPICS base ● How to create EPICS application on Windows ● How to use CA.DLL from Visual Basic How to use CA.DLL

● Dynamic-Link Library (DLL) ● Microsoft's implementation of the shared library conc ept in the operating systems ● In Linux or other UNIX-like operating systems, .so o r .sl file extensions are used

● Demo: caClient by Visual Basic ● Procedure ● create an interface file ● design form (place Button, Label, Timer, etc) ● assign event ● execute ● add some functionalities ? Interface file for CA.DLL / Com.DLL

● EXCUSE: not full-functional..... ● Convenient Tool : "dependency walker" ● Interface file "modCA.vb"

Example ( written in 1 line in the program ) Public Declare Function ca_array_get : Declare "ca_array_get" Lib "ca.dll" : in "CA.DLLt" Alias "_ca_array_get@16" : exported as _ca_array.... (ByVal chtype As Integer, ByVal count As Integer, ByVal chanId As Integer, : To check file type, args, ByRef pValue As Any) As Integer : please read "cadef.h"

"As Any" is NOT allowed in Visual Studio 2005 CA client example : Visual Basic 2005

● Example : caget

● This example is not good! Only an example. ● Please use CA monitor for REAL application.... CA client in C

● caExample.c (simplified) ● Please refer to "How to write CA client" doc. ● makeBaseApp.pl -t caClient clientex

#include "cadef.h" int main(int argc,char **argv) { double data; chid mychid;

ca_context_create(ca_disable_preemptive_callback); ca_create_channel("recname",NULL,NULL,10,&mychid); ca_pend_io(5.0);

ca_get(DBR_DOUBLE,mychid,(void *)&data); ca_pend_io(5.0);

printf("%s %f\n",argv[1],data); return(0); } CA client example : Visual Basic 2005

● Execute IDE ● Create New Project -> Windows Application ● Create New Form ● Place Button, Labe, Timer ● Add "modCA.vb" in the project ● Assign Event ● Form1_Load status = ca_context_create (ca_preemptive_callback_select.ca_disable_preemptive_callback) status = ca_create_channel("recn", 0, 0, CA_PRIORITY_DEFAULT, pCh) status = ca_pend_io(5.0) ● Timer1_Tick status = ca_array_get(DBR_DOUBLE, 1, pCh, recval) status = ca_pend_io(5.0) lblValue.Text = Format(recval, "f") ● Form1_FormClosing status = ca_clear_channel(pCh) Call ca_context_destroy() Need more?

● Add Sound in the application (catest4.exe) ● Use Win32API ● winmm.dll ● PlaySound (ASYNC mode) ● please refer to MSDN document

● StripTool (catest5.exe) Application

● Voice Message ● It is possible to use Microsoft API

● LabVIEW / Matlab ● ActiveX, LabCA, etc ● Please ask to Tech-talk

● Scripting languate : Python, Perl, etc ● you can use CA.DLL, COM.DLL But please think....

● If IOC is disconnected....

● MEDM --> PVs on the panel went to BLANK ● you should write this feture in your application

● I must emphasize again: ● Polling is not a good method ● use CA monitor!!!! Comment on VC6

● You many need to modify some source codes or setting files for now. ● These error will be fixed in the future release o f EPICS base. ● Some of them are already fixed in CVS reposit ory.

● C:\VECC\base-3.14.8.2\configure\RULES_BUILD ● C:\VECC\base-3.14.8.2\src\gdd\dbMapper.cc ● C:\VECC\base-3.14.8.2\src\makeBaseApp\makeBaseApp.pl ● C:\VECC\base-3.14.8.2\src\catools\camonitor.c

● For detail, please refer to the following pages VC6

C:\VECC\base-3.14.8.2\configure\RULES_BUILD ● in perl script, * does not interpreted correctlly.

before correction ifdef WIN32 ifdef BORLAND @$(INSTALL_PRODUCT) -d -m 555 $< $(INSTALL_BIN) else @$(INSTALL_PRODUCT) -d -m 555 $<* $(INSTALL_BIN) endif endif after correction ifdef WIN32 ifdef BORLAND @$(INSTALL_PRODUCT) -d -m 555 $<* $(INSTALL_BIN) else @$(INSTALL_PRODUCT) -d -m 555 $< $(INSTALL_BIN) endif endif VC6

● C:\VECC\base-3.14.8.2\src\gdd\dbMapper.cc

line 1138 before correction

for ( int i = db->no_str; i < MAX_ENUM_STATES; i++ ) { db->strs[i][0] = '\0'; }

after correction { for ( int i = db->no_str; i < MAX_ENUM_STATES; i++ ) { db->strs[i][0] = '\0'; } } VC6

● C:\VECC\base-3.14.8.2\src\makeBaseApp\makeBaseApp.pl before correction sub UnixPath { my ($newpath) = @_; if ($^O eq "cygwin") { $newpath =~ s|\\|/|go; $newpath =~ s%^([a-zA-Z]):/%/cygdrive/$1/%; } elsif ($^O eq 'sunos') { $newpath =~ s(^\/tmp_mnt/)(/); } return $newpath; }

after correction sub UnixPath { my ($newpath) = @_; if ($^O eq "cygwin") { $newpath =~ s|\\|/|go; $newpath =~ s%^([a-zA-Z]):/%/cygdrive/$1/%; } elsif ($^O eq 'MSWin32') { $newpath =~ s|\\|/|go; } elsif ($^O eq 'sunos') { $newpath =~ s(^\/tmp_mnt/)(/); } return $newpath; VC6

● C:\VECC\base-3.14.8.2\src\catools\camonitor.c

before correction setvbuf(stdout,NULL,_IOLBF,BUFSIZ); /* Set stdout to line buffering */

after correction comment this line or #define BUFSIZ 2