Tango Client在QT中的測試案例

把Tango安裝文件夾(D:\Program Files\tango\win64)下的lib和Include文件夾複製到項目所在目錄,在.pro文件中添加頭文件及其路徑和相應的庫:

# For tango config
INCLUDEPATH += $$PWD/include/vc12/
INCLUDEPATH += $$PWD/lib/vc12_dll/
DEPENDPATH  += $$PWD/lib/vc12_dll/

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/vc12_dll -lCOS4_rt
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/vc12_dll -lCOS421_rtd
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/vc12_dll -lomniDynamic4_rt
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/vc12_dll -lomniDynamic4_rtd
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/vc12_dll -lomnithread40_rt
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/vc12_dll -lomnithread40_rtd
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/vc12_dll -lomniORB421_rt
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/vc12_dll -lomniORB421_rtd
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/vc12_dll -ltango
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/vc12_dll -ltangod
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/vc12_dll -lzmq
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/vc12_dll -lzmqd
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/vc12_dll -llog4tango
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/vc12_dll -llog4tangod
else:win32:CONFIG(debug, debug|release): LIBS += -LD:/tango/win64/lib/vc12_dll/ -llog4tangod

頭文件添加:

#include <tango.h>
using namespace Tango;

DeviceProxy *device;//device
DeviceAttribute att_reply;//get Attribute from server

首先連接設備:

    device = new DeviceProxy("sys/tg_test/1");
    //
    // Ping the device
    //
    device->ping();

讀取設備屬性Attributes:

    try
    {
        //
        // Read a device attribute
        //

        //**********Attribute ampli**********/
        att_reply = device->read_attribute("ampli");
        qDebug()<<att_reply.DoubleSeq[0];

        //**********Attribute boolean_image**********/
        att_reply = device->read_attribute("boolean_image");
        for(int i=0;i<(att_reply.dim_x)*(att_reply.dim_y);i++)
        {
            qDebug()<<att_reply.BooleanSeq[i];
        }

        //**********Attribute boolean_scalar**********/
        att_reply = device->read_attribute("boolean_scalar");
        qDebug()<<att_reply.BooleanSeq[0];

        //**********Attribute boolean_spectrum**********/
        att_reply = device->read_attribute("boolean_spectrum");
        for(int i=0;i<att_reply.dim_x;i++)
        {
            qDebug()<<att_reply.BooleanSeq[i];
        }

        //**********Attribute double_image**********/
        att_reply = device->read_attribute("double_image");
        for(int i=0;i<(att_reply.dim_x)*(att_reply.dim_y);i++)
        {
            qDebug()<<att_reply.DoubleSeq[i];
        }

        //**********Attribute double_scalar**********/
        att_reply = device->read_attribute("double_scalar");
        qDebug()<<att_reply.DoubleSeq[0];

        //**********Attribute double_spectrum**********/
        att_reply = device->read_attribute("double_spectrum");
        for(int i=0;i<att_reply.dim_x;i++)
        {
            qDebug()<<att_reply.DoubleSeq[i];
        }

        //**********Attribute float_scalar**********/
        att_reply = device->read_attribute("float_scalar");
        qDebug()<<att_reply.FloatSeq[0];

        //**********Attribute long64_scalar**********/
        att_reply = device->read_attribute("long64_scalar");
        qDebug()<<att_reply.Long64Seq[0];


        //**********Attribute short_scalar**********/
        att_reply = device->read_attribute("short_scalar");
        qDebug()<<att_reply.ShortSeq[0];

        //**********Attribute State**********/
        vector<DeviceAttribute> *devattr;
        vector<string> attr_names;
        attr_names.push_back("State");
        devattr = device->read_attributes(attr_names);
        DevState state;
        (*devattr)[0] >> state;
        qDebug() << "my_attribute value " << state;
        delete devattr;

        //**********Attribute Status**********/
        att_reply = device->read_attribute("Status");
        qDebug()<<att_reply.StringSeq[0];

        //**********Attribute string_scalar**********/
        att_reply = device->read_attribute("string_scalar");
        qDebug()<<att_reply.StringSeq[0];

        //**********Attribute uchar_scalar**********/
        att_reply = device->read_attribute("uchar_scalar");
        qDebug()<<att_reply.UCharSeq[0];

        //**********Attribute wave**********/
        att_reply = device->read_attribute("wave");
        for(int i=0;i<att_reply.dim_x;i++)
        {
            qDebug()<<att_reply.DoubleSeq[i];
        }

        qDebug()<<'\n';
    }
    catch (DevFailed &e)
    {
        Except::print_exception(e);
        exit(-1);
    }

寫入設備屬性Attributes:

    try
    {
        //
        // Write a device attribute
        //

        /**********Write Attribute boolean_scalar**********/
        vector<DeviceAttribute> attr_boolean_scalar;
        string boolean_scalar_name("boolean_scalar");
        bool bool_attr = false;
        attr_boolean_scalar.push_back(DeviceAttribute(boolean_scalar_name,bool_attr));
        device->write_attributes(attr_boolean_scalar);

        /**********Write Attribute ampli**********/
        vector<DeviceAttribute> attr_ampli;
        string ampli_name("ampli");
        double ampli_attr = 2.2 ;
        attr_ampli.push_back(DeviceAttribute(ampli_name,ampli_attr));
        device->write_attributes(attr_ampli);

        /**********Write Attribute double_scalar**********/
        vector<DeviceAttribute> attr_double_scalar;
        string double_scalar_name("double_scalar");
        double double_scalar_attr=1.12;
        attr_double_scalar.push_back(DeviceAttribute(double_scalar_name,double_scalar_attr));
        device->write_attributes(attr_double_scalar);

    }
    catch (DevFailed &e)
    {
        Except::print_exception(e);
        exit(-1);
    }

寫入命令Command:

    try
    {
        //
        // Execute a command on the device and extract the reply as a string
        //

        //**********State Command**********/
        string db_info;
        DeviceData cmd_reply;
        cmd_reply = device->command_inout("State");
        cmd_reply >> db_info;
        cout << "Command reply " << db_info << endl;

        //**********Status Command**********/
        cmd_reply = device->command_inout("Status");
        cmd_reply >> db_info;
        cout << "Command reply " << db_info << endl;

        //**********DevVarLongArray Command**********/
        DeviceData din_LongArray,dout_LongArray;
        DevVarLongArray *in_LongArray = new DevVarLongArray();
        in_LongArray->length(256);
        (*in_LongArray)[0] = 2;
        (*in_LongArray)[1] = 4;
        din_LongArray << in_LongArray;
        try
        {
           dout_LongArray = device->command_inout("DevVarLongArray",din_LongArray);
           cout << "Command DevVarLongArray reply \n" << dout_LongArray << endl;
        }
        catch(DevFailed &e)
        {
            Except::print_exception(e);
            exit(-1);
        }

        /**********DevBoolean Command**********/
        DeviceData din_Boolean,dout_Boolean;
        DevBoolean *in_bool = new DevBoolean();
        *in_bool = true;
        din_Boolean << in_bool;
        try
        {
           dout_Boolean = device->command_inout("DevBoolean",din_Boolean);
           cout << "Command DevBoolean reply " << dout_Boolean << endl;
        }
        catch(DevFailed &e)
        {
            Except::print_exception(e);
            exit(-1);
        }
    }
    catch (DevFailed &e)
    {
        Except::print_exception(e);
        exit(-1);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章