nspyre.instrument.gateway

This module provides an interface to control devices on an InstrumentServer.

Module Contents

Classes

InstrumentGateway

Create a connection to an InstrumentServer and access it's devices.

InstrumentGatewayDevice

Wrapper for a device residing in an InstrumentGateway.

Attributes

RPYC_CONN_TIMEOUT

RPyC connection timeout in seconds (None for no timeout).

nspyre.instrument.gateway.RPYC_CONN_TIMEOUT = 30

RPyC connection timeout in seconds (None for no timeout).

exception nspyre.instrument.gateway.InstrumentGatewayError(*args, **kwargs)

Raised for failures related to the InstrumentGateway.

Parameters:
  • args – Arguments to pass to super class Exception().

  • kwargs – Keyword arguments to pass to super class Exception().

class nspyre.instrument.gateway.InstrumentGateway(addr='localhost', port=INSTRUMENT_SERVER_DEFAULT_PORT, conn_timeout=0.0, sync_timeout=RPYC_SYNC_TIMEOUT)

Create a connection to an InstrumentServer and access it’s devices. When accessing a device through the gateway using gateway.my_device notation, an InstrumentGatewayDevice is returned.

Usage Example:

from nspyre import InstrumentGateway

with InstrumentGateway() as gw:
    # d is an InstrumentGatewayDevice object
    d = gw.dev1
    # run the set_something() method of dev1
    d.set_something(5)
    # run the get_something() method of dev1 and print its return value
    print(d.get_something())
Parameters:
  • addr (str) – Network address of the InstrumentServer.

  • port (int) – Port number of the InstrumentServer.

  • conn_timeout (float) – Lower bound on the time to wait for the connection to be established.

  • sync_timeout (float) – Time to wait for requests / function calls to finish.

Raises:

InstrumentGatewayError – Connection to the InstrumentServer failed.

connect()

Attempt connection to an InstrumentServer.

Raises:

InstrumentGatewayError – Connection to the InstrumentServer failed.

is_connected()

Return whether the gateway is connected.

Return type:

bool

disconnect()

Disconnect from the InstrumentServer.

reconnect()

Disconnect then connect to the InstrumentServer again.

Raises:

InstrumentGatewayError – Connection to the InstrumentServer failed.

class nspyre.instrument.gateway.InstrumentGatewayDevice(name, gateway)

Wrapper for a device residing in an InstrumentGateway. When we access an attribute of a device from an InstrumentGateway, it will return an rpyc netref object. This creates a problem when the gateway disconnects from the instrument server, then later reconnects. If we have an rpyc netref that pointed to a device attribute, it will be stale because it was linked to the original gateway. However, if we instead pass around this InstrumentGatewayDevice, we can always re-access the gateway device whenever we want to access an attribute of the device. This way, if the gateway disconnects then reconnects, we will always be accessing the attributes of the newly connected gateway, rather than a stale netref.

Accessing the “device” attribute will return (an rpyc netref to) the device object. Attributes of the device can be accessed directly from this object. E.g.:

from nspyre import InstrumentGateway

with InstrumentGateway() as gw:
    # let's assume "dev1" was created on the instrument server as an
    # instance of "MyDriver"

    # d is an InstrumentGatewayDevice object
    d = gw.dev1
    # run the get_something() method of dev1 and print its return value
    print(d.get_something())
    # does the same thing
    print(d.device.get_something())

    print(isinstance(gw.dev1, MyDriver)) # False
    print(isinstance(gw.dev1, InstrumentGatewayDevice)) # True
    print(isinstance(gw.dev1.device, MyDriver)) # True
Parameters: