def cntx(request, shared_broadcaster):
    cntx = Context(broadcaster=shared_broadcaster, log_level='DEBUG')
    cntx.register()

    def cleanup():
        cntx.disconnect()

    request.addfinalizer(cleanup)
    return cntx
def main(pv1="XF:31IDA-OP{Tbl-Ax:X1}Mtr.VAL",
         pv2="XF:31IDA-OP{Tbl-Ax:X2}Mtr.VAL"):
    '''Simple example which connects to two motorsim PVs (by default).

    It tests reading, writing, and subscriptions.
    '''

    shared_broadcaster = SharedBroadcaster()
    pv1 = "XF:31IDA-OP{Tbl-Ax:X1}Mtr.VAL"
    pv2 = "XF:31IDA-OP{Tbl-Ax:X2}Mtr.VAL"

    # Some user function to call when subscriptions receive data.
    called = []

    def user_callback(command):
        print("Subscription has received data: {}".format(command))
        called.append(True)

    ctx = Context(broadcaster=shared_broadcaster, log_level='DEBUG')
    ctx.register()
    ctx.search(pv1)
    ctx.search(pv2)
    # Send out connection requests without waiting for responses...
    chan1 = ctx.create_channel(pv1)
    chan2 = ctx.create_channel(pv2)
    # Set up a function to call when subscriptions are received.
    chan1.register_user_callback(user_callback)

    reading = chan1.read()
    print('reading:', reading)
    chan1.subscribe()
    chan2.read()
    chan1.unsubscribe(0)
    chan1.write((5, ))
    reading = chan1.read()
    assert reading.data == 5
    print('reading:', reading)
    chan1.write((6, ))
    reading = chan1.read()
    assert reading.data == 6
    print('reading:', reading)
    chan2.disconnect()
    chan1.disconnect()
    assert called
Beispiel #3
0
def test_thread_pv(threading_broadcaster):
    from caproto.threading.client import Context, PV

    pv1 = "XF:31IDA-OP{Tbl-Ax:X1}Mtr.VAL"
    # pv2 = "XF:31IDA-OP{Tbl-Ax:X2}Mtr.VAL"

    # Some user function to call when subscriptions receive data.
    called = []

    def user_callback(*, value, **kwargs):
        print()
        print('-- user callback', value)
        called.append(True)

    ctx = Context(threading_broadcaster, log_level='DEBUG')
    ctx.register()

    time_pv = PV(pv1, context=ctx, form='time')
    ctrl_pv = PV(pv1, context=ctx, form='ctrl')

    time_pv.wait_for_connection()
    time_pv.add_callback(user_callback)
    print('time read', time_pv.get())
    print('ctrl read', ctrl_pv.get())

    time_pv.put(3, wait=True)
    time_pv.put(6, wait=True)

    time.sleep(0.1)
    assert time_pv.get() == 6
    assert called

    print('read', time_pv.get())
    print('done')

    repr(time_pv)

    for k, v in PV.__dict__.items():
        if isinstance(v, property):
            getattr(time_pv, k)
            getattr(ctrl_pv, k)