Write your first Tango client

Write your first Tango client#

audience:developers lang:all

Below is an example of how to write a client to connect to a Tango device and interact with it:

 1  /*
 2   * Example of a client using the Tango C++ api.
 3   */
 4  #include <tango.h>
 5
 6  using namespace Tango;
 7
 8  int main(unsigned int argc, char **argv)
 9  {
10      try
11      {
12        // Create a connection to a Tango device
13        DeviceProxy *device = new DeviceProxy("sys/tg_test/1");
14
15        // Ping the device
16        device->ping();
17
18        // Execute a command on the device and extract the reply as a string
19        string db_info;
20        DeviceData cmd_reply;
21        cmd_reply = device->command_inout("DbInfo");
22        cmd_reply >> db_info;
23        cout << "Command reply " << db_info << endl;
24
25        // Read a device attribute (string data type)
26        string spr;
27        DeviceAttribute att_reply;
28        att_reply = device->read_attribute("StoredProcedureRelease");
29        att_reply >> spr;
30        cout << "Database device stored procedure release: " << spr << endl;
31      }
32      catch (DevFailed &e)
33      {
34        Except::print_exception(e);
35        exit(-1);
36      }
37  }
 1'''
 2Example of a client using the Tango Python api (PyTango).
 3'''
 4import tango
 5
 6try:
 7    # Create a connection to a Tango device
 8    tango_test = tango.DeviceProxy("sys/tg_test/1")
 9
10    # Ping the device
11    print(f"Ping: {tango_test.ping()}")
12
13    # Execute a command on the device and extract the reply as a string
14    result = tango_test.command_inout("DbInfo", "First hello to device")
15    print(f"Result of execution of DbInfo command = {result}")
16
17    # Read a device attribute (string data type)
18    procedure_release = tango_test.read_attribute("StoredProcedureRelease")
19    print(f"Database device stored procedure release = {procedure_release.value}")
20
21except DevFailed as df:
22    print(f"Failure: {df}")