Consumer in C++ to read and write PLC variables

This sample explains how to create an ADS-WebService-Consumer to read and write PLC-Variables via ADS-HTTP.

 Unzip the sample 'WebServiceTestConsumer (C++)'.

1. Creating a WebService-Consumers within the Microsoft Visual C++

Create a new project with the help of the MFC-AppWizard.
Copy th files 'WebServiceConnector.cpp', 'WebServiceConnector.h', 'Base64.cpp' and 'Base64.h' (can be found in the 'WebServiceSampleCPP.zip'-file) to your project-folder and add them into the project
Insert the following line to the file in which you want to use the ADS Web Service:

#include "WebServiceConnector.h"

Before you can read and write you must create an object of the WebServiceConnector and connect to the WebService:

CWebServiceConnector webServiceConnector;

webServiceConnector.Connect("http://192.168.0.2.1.1/TcAdsWebService/TcAdsWebService.dll");

The address must be the URL of your TwinCAT ADS WebService-DLL-file.

2. Reading PLC -variables

To read a PLC-variable use the "Read"-method of the WebServiceConnector-object:

HRESULT Read(char* netId, int port, ULONG indexGroup, ULONG indexOffset, ULONG* pcbData, BYTE* pData);


The 3 methods

HRESULT ReadInt (char* netId, int port, ULONG indexGroup, ULONG indexOffset, ULONG cblen, int& ret);
HRESULT ReadBool (char* netId, int port, ULONG indexGroup, ULONG indexOffset, ULONG cblen, bool& ret);
HRESULT ReadString (char* netId, int port, ULONG indexGroup, ULONG indexOffset, ULONG cblen, CString& ret);

convert the variables to their corresponding type.

Example:

bool Boolean;
if (webServiceConnector.ReadBool("192.168.0.2.1.1", 801, 16416, 0, 1, Boolean)!=S_OK)
        MessageBox("Error");

3. Writing PLC-variables

HRESULT Write(char* netId, int port, ULONG indexGroup, ULONG indexOffset, ULONG cblen, BYTE* pData);


The 3 methods

HRESULT WriteInt(char* netId, int port, ULONG indexGroup, ULONG indexOffset, ULONG cblen, int Value);
HRESULT WriteBool(char* netId, int port, ULONG indexGroup, ULONG indexOffset, ULONG cblen, bool Value);
HRESULT WriteString(char* netId, int port, ULONG indexGroup, ULONG indexOffset, ULONG cblen, CString Value);

convert the variables to a byte-arrays.

Example:

if (webServiceConnector.WriteBool("192.168.0.2.1.1", 801, 16416, 0, 1, true)!=S_OK)
        MessageBox("Error");

4. Error codes

S_OK - No error
E_NO_CON - No connetion to the WebService
E_SEND - Unable to send data to the WebService
E_RESPONSE - Unknown response from the WebService
E_FAILED - Unknown error

5. PLC-program, IEC1131 daclaration of the variables:

PROGRAM MAIN
VAR
    PlcVarBool    AT %MX0.0  : BOOL := TRUE;
    PlcVarInt     AT %MW1    : INT := 1234;
    PlcVarString  AT %MD3    : STRING := 'Hello Automation';
END_VAR

6. C++ Application, the layout:

Consumer in C++ to read and write PLC variables 1:

7. The source-code:

void CWebServiceTestConsumerDlg::OnRead() 
{
    CWebServiceConnector webServiceConnector;
    webServiceConnector.Connect(WebServiceURL);
    if (webServiceConnector.IsConnected())
    {
        int Integer;
        bool Boolean;
        CString String;

        int port = 801;
        int group = 16416;
        unsigned long size;
        int offset;


        /* read bool */
        size = 1;
        offset = 0;
        if (webServiceConnector.ReadBool(WebServiceNetId,port,group,offset,size,Boolean) == S_OK)
        {
            m_boolean = Boolean;
        }
        else
        {
            MessageBox("Error");
            return;
        }

        /* read int */
        size = 2;
        offset = 1;
        if (webServiceConnector.ReadInt(WebServiceNetId,port,group,offset,size,Integer) == S_OK)
        {
            m_integer = Integer;
        }
        else
        {
            MessageBox("Error");
            return;
        }

        /* read string */
        size = 81;
        offset = 3;
        if (webServiceConnector.ReadString(WebServiceNetId,port,group,offset,size,String) == S_OK)
        {
            m_string = String;
        }
        else
        {
            MessageBox("Error");
            return;
        }
        UpdateData(false);
    }
    else MessageBox("Error");
}

void CWebServiceTestConsumerDlg::OnWrite() 
{
    CWebServiceConnector webServiceConnector;
    webServiceConnector.Connect(WebServiceURL);
    if (webServiceConnector.IsConnected())
    {
        UpdateData(true);

        int port = 801;
        int group = 16416;
        int offset;
        unsigned long size;

        /* write boolean */
        size = 1;
        offset = 0;
        bool boolean = false;
        if (m_boolean==1)
            boolean = true;
        if (webServiceConnector.WriteBool(WebServiceNetId,port,group,offset,size,boolean)!=S_OK)
            MessageBox("Error");

        /* write integer */
        size = 2;
        offset = 1;

        if (webServiceConnector.WriteInt(WebServiceNetId,port,group,offset,size,m_integer)!=S_OK)
            MessageBox("Error");

        /* write string */
        size = 81;
        offset = 3;

        if (webServiceConnector.WriteString(WebServiceNetId,port,group,offset,size,m_string)!=S_OK)
            MessageBox("Error");
    }
    else MessageBox("Error");
}

8. C++ Application, the download:

Complete sourcecode