EPICS Controls Argonne National Laboratory

Experimental Physics and
Industrial Control System

1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  <20192020  2021  2022  2023  2024  Index 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  <20192020  2021  2022  2023  2024 
<== Date ==> <== Thread ==>

Subject: Re: Issue of PV callback of PyEpics
From: Matt Newville via Tech-talk <[email protected]>
To: Tong Zhang <[email protected]>
Cc: EPICS Tech Talk <[email protected]>
Date: Tue, 19 Feb 2019 15:31:03 -0600
Hi Tong,

On Tue, Feb 19, 2019 at 8:28 AM Tong Zhang via Tech-talk <[email protected]> wrote:
And the mbbi record used in this demo:

   1 record(mbbi, "status") {
   2     field("VAL", 3)

   3     field("ZRST", "Level0")
   4     field("ONST", "Level1")

   5     field("TWST", "Level2")

   6     field("THST", "Level3")

   7     field("PINI", "1")

   8 }

On 2/19/19 9:25 AM, Tong Zhang wrote:
> mypv.get(as_string=True) returns the string value as expected. Please
> see the detailed constructed fully functional demo below:
>
> Script works as expected with mbbi record:
>
>    7 import epics
>    8 import time
>    9
>   10
>   11 pv_name = 'INVALID_PV_NAME'
>   12
>   13 def pv_with_callback(pv_name):
>   14     pv = epics.PV(pv_name)
>   15     time.sleep(2)
>   16     def on_value_changed(char_value, **kws):
>   17         print("Char Value: ", char_value)
>   18     pv.add_callback(on_value_changed)
>   19
>   20
>   21 pv_with_callback(pv_name)
>   22
>   23 time.sleep(2)
>   24 pv_name = 'status'
>   25
>   26 pv_with_callback(pv_name)
>
> If commented out line 15th, and run it again, the behavior is changed,
> only value is returned in the callback rather than expected char_value.
>

Slight aside: the value-change callback will be called with a fixed signature with PV name as the first argument, and keyword arguments for everything else, including `char_value`.  So, that should be

    def on_value_change(pvname, char_value='?', **kws):
          print("Char Value ", pvname, char_value)

But I think that is not the real issue, and is not solved by adding a `time.sleep()`.    

I do think that the issue there -- and probably why the initial call to you callback is not seeing the char_values -- is that the PV needs to connect to the Control Values for the PV.  That takes a small (but non-zero)  amount of time.  You should not need to wait 2 seconds, but waiting 0.01 seconds might help. 

So, with no wait at alil, it seems sort of normal to me that the enum strings might not be **immediately available** upon creating a PV.  It should show up very soon, but that initial call of the callback might not yet have the control values like enumeration strings.

There are a few things you could do.  First, you could use the "control data type" as with:

     pv = epics.PV(pv_name, form='ctrl')
     pv.add_callback(on_value_change)

(the default value for "form" is "time" to give the Time-Status-Severity data type) or you could force retrieving the control types:
 
    pv = epics.PV(pv_name)
    pv.get_ctrlvars()
    pv.add_callback(on_value_change)

You can also wait a short amount of time -- you do not need to wait 2 seconds, but waiting 0.01 seconds might help:

    pv = epics.PV(pv_name)
    time.sleep(0.01)
    pv.add_callback(on_value_change)

If you're connecting to many PVs, I would suggest creating many of the PVs, then adding callbacks to all the PVs  That approach allows all the connections (including fetching the control values) to be completed.

Hope that helps,

--Matt


References:
Issue of PV callback of PyEpics Zhang, Tong via Tech-talk
Re: Issue of PV callback of PyEpics Matthew Newville via Tech-talk
Re: Issue of PV callback of PyEpics Tong Zhang via Tech-talk
Re: Issue of PV callback of PyEpics Tong Zhang via Tech-talk

Navigate by Date:
Prev: Re: Two EVR in one VME Konrad, Martin via Tech-talk
Next: help Earl Green via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  <20192020  2021  2022  2023  2024 
Navigate by Thread:
Prev: Re: Issue of PV callback of PyEpics Tong Zhang via Tech-talk
Next: EPICS support with Turbo UMAC fengwentian--- via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  <20192020  2021  2022  2023  2024 
ANJ, 19 Feb 2019 Valid HTML 4.01! · Home · News · About · Base · Modules · Extensions · Distributions · Download ·
· Search · EPICS V4 · IRMIS · Talk · Bugs · Documents · Links · Licensing ·