Accessing the value of variable through variable name in ADS

In the C (c + +) interface of TWinCat ADS library provided by Beifu, there are two ways to access variables in PLC: access according to address, Access by variable name.
The codes accessed according to the address are as follows:

...
QString hostNetId = "192.168.12.51.1.1";
int hostPort = 851;
AmsAddr targetAddr = createAddr(hostNetId, hostPort); //This createAddr is written by myself to fill data into the structure.

//The following 512952 is the address of the variable I want to access
unsigned short data = 3;
qDebug() << AdsSyncWriteReq(&targetAddr, 0x00004020, 512952, 2, &data);
qDebug() << AdsSyncReadReq(&targetAddr, 0x00004020, 512952, 2, &data) << "D0:" << data;

The code accessed according to the variable name is as follows:

...
QString hostNetId = "192.168.12.51.1.1";
int hostPort = 851;
AmsAddr targetAddr = createAddr(hostNetId, hostPort); //This createAddr is written by myself to fill data into the structure.

  ulong lHdlVar;
  char szVar[]={"GVL_GuleImage.Glue_Site"};
  int nErr = AdsSyncReadWriteReq(&targetAddr,
                                  ADSIGRP_SYM_HNDBYNAME,
                                  0x0,
                                  sizeof(lHdlVar),
                                  &lHdlVar,
                                  sizeof(szVar),
                                  szVar);//Create handle

    //    short readInt = 0;
    //    short writeInt = 0;
    //    Nerr = adssyncwritereq (& targetaddr, adsigrp sym valbyhnd, lhdlvar, 2, & writeint); / / write

    //    Nerr = adssyncreadreq (& targetaddr, adsigrp 〝 sym 〝 valbyhnd, lhdlvar, 2, & readint); / / read
    
    //    Nerr = adssyncwritereq (& targetaddr, adsigrp sym releasehnd, 0, sizeof (lhdlvar), & lhdlvar); / / release handle

In addition, because of the way to get the handle according to the variable name and then access it, it seems to be relatively slow, and it is impossible to monitor a variable (register the callback function). So I think the better way is to read the address of the changed quantity through the variable name, and then operate according to the address. The code is as follows:

...
QString hostNetId = "192.168.12.51.1.1";
int hostPort = 851;
AmsAddr targetAddr = createAddr(hostNetId, hostPort); //This createAddr is written by myself to fill data into the structure.

char szVar[]={"GVL_GuleImage.Glue_Site"};
AdsSymbolInfoByName nameInfo = {0};
int nErr = AdsSyncReadWriteReq(&targetAddr,
                                ADSIGRP_SYM_INFOBYNAME,
                                0x0,
                                sizeof(nameInfo),
                                &nameInfo,
                                sizeof(szVar),
                                szVar);
qDebug() << nErr << nameInfo.indexGroup << nameInfo.indexOffset << nameInfo.cbLength;
        
unsigned short data = 3;
qDebug() << AdsSyncWriteReq(&targetAddr, nameInfo.indexGroup, nameInfo.indexOffset, 2, &data);
qDebug() << AdsSyncReadReq(&targetAddr, nameInfo.indexGroup, nameInfo.indexOffset, 2, &data) << "D0:" << data;

Posted by snascendi on Fri, 01 Nov 2019 09:43:29 -0700