Personal tools
You are here: Home Members cpascual Documents Recipes of PyTango

Recipes of PyTango

This is a recipes collection for simple control of TANGO devices from the command prompt with iPython. The examples are done for a Power Supply Device server. We assume Tango and PyTango versions >= 7.0.0

Important: you can find more comprehensive information in the official PyTango  page:

http://www.tango-controls.org/static/PyTango/latest/doc/html/index.html

Specifically, you can find more examples here.

 

Note: All this commands can be run from a normal python console, but your life will be easier using "ipython"  instead. In ipython you can use help(command) or simply comand? to get documentation. You can also check available autocompletion options using the TAB key.

 

Importing PyTango and checking its version

>>> import PyTango
>>> print PyTango.__version__
7.0.0

 

Get a TANGO device object and check if it is online

>>> dev = PyTango.DeviceProxy('lt01/pc/bend-01')
>>> dev.ping()
349

 

Execute commands on this device...

... using the "classic" syntax

>>> print dev.command_inout('state')
ON
>>> dev.command_inout('off')
>>> print dev.command_inout('State')
OFF
>>> dev.command_inout('on')
>>> print dev.command_inout('State')
ON

Alternatively, the commands can be executed directly as Python methods of the device:

>>> print dev.state()
ON
>>> dev.off()
>>> print dev.state()
OFF
>>> dev.on()
>>> print dev.state()
ON

 

Reading Attributes

When you read a Tango Attribute, you get an object that contains not only the attribute value but also information about quality, dimensions, type, timestamp,...)

The classical way:

>>> print dev.read_attribute('current')
DeviceAttribute[
data_format = PyTango._PyTango.AttrDataFormat.SCALAR
  dim_x = 1
  dim_y = 0
has_failed = False
is_empty = False
   name = 'Current'
nb_read = 1
nb_written = 0
quality = PyTango._PyTango.AttrQuality.ATTR_VALID
r_dimension = AttributeDimension(dim_x = 1, dim_y = 0)
   time = TimeVal(tv_nsec = 0, tv_sec = 1249979113, tv_usec = 985183)
   type = PyTango._PyTango.CmdArgType.DevDouble
  value = 1228.6609907159213
w_dim_x = 0
w_dim_y = 0
w_dimension = AttributeDimension(dim_x = 0, dim_y = 0)
w_value = None]

The "Pythonic" way (we read the CurrentSetpoint this time):

>>> print dev['currentsetpoint']
DeviceAttribute[
data_format = PyTango._PyTango.AttrDataFormat.SCALAR
  dim_x = 1
  dim_y = 0
has_failed = False
is_empty = False
   name = 'CurrentSetpoint'
nb_read = 1
nb_written = 1
quality = PyTango._PyTango.AttrQuality.ATTR_VALID
r_dimension = AttributeDimension(dim_x = 1, dim_y = 0)
   time = TimeVal(tv_nsec = 0, tv_sec = 1249979457, tv_usec = 261332)
   type = PyTango._PyTango.CmdArgType.DevDouble
  value = 1234.0
w_dim_x = 1
w_dim_y = 0
w_dimension = AttributeDimension(dim_x = 1, dim_y = 0)
w_value = 1234.0]

If we only want the value:

>>> print dev['current'].value
1235.83187539

Reading a list of attributes simultaneously (the return value is a list of attribute objects):

>>> result = dev.read_attributes(['current','currentsetpoint'])
>>> print result[0].value
1233.8343894675204
>>> print result[1].value
1234.0

 

Writing to Attributes

The classic way:

>>> dev.write_attribute('currentsetpoint',11)
>>> print dev.read_attribute('currentsetpoint').value
11.0

The "Pythonic" way:

>>> dev['currentsetpoint'] = 13
>>> print dev['currentsetpoint'].value
13

 

Document Actions