Getting Started¶
In This Section
Creating an Instance¶
SNAPconnect Futures requires some minimal setup. There are two primary ways to achieve this: creating and configuring
the class manually or using the helper function, SnapConnectFuturesAuto()
.
Manually¶
If you need to set up your SNAPconnect Futures instance manually, you will also need to manually create and start your IO loops, in addition to creating the SNAPconnect Futures instance.
from snapconnect_futures import SnapConnectFutures
from snapconnect import snap
from tornado.gen import coroutine
import apy
import tornado
# Create an apy IOLoopScheduler
scheduler = apy.ioloop_scheduler.IOLoopScheduler.instance()
# Create a SNAPconnect Instance that uses the apy scheduler
comm = snap.Snap(scheduler=scheduler, funcs={})
# Setup tornado's IO loop by scheduling calls to SNAPconnect's poll_internals method
tornado.ioloop.PeriodicCallback(comm.poll_internals, 5).start()
# Create an SCF instance using an existing SNAPconnect instance which registers all
# of our callbacks automatically
scf = SnapConnectFutures(comm)
Using SnapConnectFuturesAuto¶
If you are unfamiliar with SNAPconnect Futures, you may want to use SnapConnectFuturesAuto()
to simplify creating
the SNAPconnect Futures instance.
from snapconnect_futures import SnapConnectFuturesAuto
from snapconnect import snap
from tornado.gen import coroutine
import tornado
scf = SnapConnectFuturesAuto()
Running an Application¶
With everything set up correctly, you can start creating your own coroutine functions and kick off the loop:
SERIAL_TYPE = snap.SERIAL_TYPE_RS232
# Since Windows counts from COM1, 0 is COM1, 1 is COM2, etc.
# On Linux/OS X, use something like "/dev/ttyS1"
SERIAL_PORT = 0
DEVICE_ADDR = "aabbcc"
@coroutine
def setup_serial():
"""This function won't return until the serial connection is established."""
print "Connecting to serial..."
bridge_addr = yield scf.open_serial(SERIAL_TYPE, SERIAL_PORT)
print "Connected to: ", bridge_addr
@coroutine
def get_module_info():
"""Each yielded callback_rpc() function here will not return until it gets a
response back from the node."""
print "Probing the module..."
hw_type = yield scf.callback_rpc(DEVICE_ADDR, "get_hardware_type", hardware_id)
print "Hardware type: ", hw_type
num_devices = yield scf.callback_rpc(DEVICE_ADDR, "get_connected_devices", arg1, arg2)
print "Connected devices: ", num_devices
measurement = yield scf.callback_rpc(DEVICE_ADDR, "get_measurement")
print "Current measurement: ", measurement
@coroutine
def main():
# Will wait here until serial connection completes
yield setup_serial()
# Will wait here until all module information is returned
yield get_module_info()
print "Done!"
# Kick off the loop to run the main() function:
tornado.ioloop.IOLoop.current().run_sync(main)
To run this program, you should first change the SERIAL_TYPE
and SERIAL_PORT
to those of your bridge device
(see the snapconnect-futures-serial-params section for more information). You will also need to update DEVICE_ADDR
to be the SNAP address for a device in your local network.