source: qtprobe-demo/traits_probe.py @ 856

Last change on this file since 856 was 856, checked in by jemian, 10 years ago

try to connect to PV on start, try to add PV name to GUI title bar on connection setup (not working yet)

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Rev Url
File size: 3.6 KB
Line 
1#!/usr/bin/env python
2
3########### SVN repository information ###################
4# $Date: 2012-05-31 17:37:51 +0000 (Thu, 31 May 2012) $
5# $Author: jemian $
6# $Revision: 856 $
7# $URL$
8# $Id: traits_probe.py 856 2012-05-31 17:37:51Z jemian $
9########### SVN repository information ###################
10
11'''
12EPICS PvProbe built using Enthought Traits
13
14*pvProbe* is one of the simplest EPICS user GUI tools.
15It is used to watch the value of an EPICS Process Variable.
16
17This version is based on the Enthought Traits user interface toolkit.
18
19USAGE::
20
21    [jemian@gov]$ ./pvProbe.py -h
22    usage: pvProbe.py [-h] [pv]
23   
24    pvProbe: EPICS Traits-based version of Probe
25   
26    positional arguments:
27      pv          EPICS PV name
28   
29    optional arguments:
30      -h, --help  show this help message and exit
31
32
33SEE ALSO:
34
35For more information about Python version of pvProbe, see
36this presentation from the 2012 EPICS Collaboration Meeting:
37https://subversion.xor.aps.anl.gov/bcdaext/qtprobe-demo/docs/build/html/index.html
38
39This code is the work entitled *traits_probe.py*.
40'''
41
42__description__ = "EPICS Traits-based version of pvProbe"
43
44
45import argparse
46import epics
47import sys                  #@UnusedImport
48
49
50choose_Qt4 = True
51choose_Qt4 = False
52from traits.etsconfig.api import ETSConfig
53if choose_Qt4:
54    ETSConfig.toolkit = 'qt4'
55else:
56    ETSConfig.toolkit = 'wx'
57
58from traits.api import *    #@UnusedWildImport
59from traitsui.api import *  #@UnusedWildImport
60from traitsui.key_bindings \
61    import KeyBinding, KeyBindings
62
63
64key_bindings = KeyBindings(
65    KeyBinding( binding1    = 'Enter',
66                description = 'Connect to new PV',
67                method_name = 'connect' ),
68    KeyBinding( binding1    = 'Return',
69                description = 'Connect to new PV',
70                method_name = 'connect' ),
71)
72
73
74#
75class CodeHandler ( Handler ):
76    '''TraitsUI Handler class for bound methods
77    '''
78   
79    def connect ( self, info ):
80        '''handle a request to connect with an EPICS PV
81        '''
82        info.object.do_connect()
83        if len(info.object.name) > 0:
84            # FIXME: This is not working
85            info.object.title = "pvProbe: " + str(info.object.name)
86
87
88class Probe( HasTraits ):
89    '''EPICS PvProbe built using Enthought Traits
90    '''
91    name = Str
92    value = Str
93    _chid = Instance( epics.PV, value = None )
94
95    def do_callback(self, value=None, **kwds):
96        """simple monitor callback"""
97        self.value = str(value)
98
99    traits_view = View(
100        Item('name'),
101        Readonly('value'),
102        resizable=True,
103        width = 250,
104        title='pvProbe',
105        key_bindings = key_bindings,
106        handler   = CodeHandler()
107    )
108   
109    def do_connect(self):
110        '''connect this GUI with an EPICS Process Variable
111           and set up a Channel Access monitor to update ``value``
112           whenever the PV changes.
113        '''
114        if len(self.name) > 0:
115            try:
116                if self._chid is not None:
117                    self._chid.cancel_callback()
118                    self._chid.disconnect()
119            except:
120                pass
121            self._chid = epics.PV(str(self.name), 
122                                  callback=self.do_callback)
123   
124    def __init__(self, name=""):
125        HasTraits.__init__(self)
126        self.name = name
127        self.do_connect()
128
129
130if __name__ == '__main__':
131    parser = argparse.ArgumentParser(description=__description__)
132    parser.add_argument('pv', 
133                        action='store', 
134                        nargs='?', 
135                        help="EPICS PV name", 
136                        default="EpicsDemo1")
137    results = parser.parse_args()
138
139    probe = Probe(name=results.pv)
140    probe.configure_traits()
Note: See TracBrowser for help on using the repository browser.