Skip to content
Hlab edited this page May 30, 2013 · 8 revisions

How to emit a signal in a server

Import Signal class
from labrad.server import Signal

#Create Signal object.
onEvent = Signal( ID, signal name, data_type)
#i.e ID = 543617
#name = 'signal: interesting information acquired'
#info_type = 'i' for integer data
#Note this has to be done as a global class variable, i.e

class ourserver( LabradServer):
    onEvent = ...
    ...
def initServer(self):
    ...

Call that signal:

onEvent( data, listeners)
#i.e data = 34
#i.e listeners = None if message is sent to everyone listening
#or  listeners = set(context.ID) to send to a particular listener of that context
Even if signal requires no parameters, still need to do self.onEvent(None).

How to receive a singal in a simple client

import labrad
from labrad.wrappers import connectAsync
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.internet import reactor
@inlineCallbacks
def connect():
    def printSignal(x,y):
        print x,y
        reactor.stop()
cxn = yield connectAsync()
server = yield cxn.data_vault
#context = yield cxn.context()
yield dv.signal__new_dataset(99901)#, context = context)
yield dv.addListener(listener = printSignal, source = None, ID = 99901)#, context = context)
print 'now listening'
reactor.callWhenRunning(connect)
reactor.run()