application description y 06/2014 qt/c++ example of a ......application description y 06/2014 qt/c++...

20
http://support.automation.siemens.com/WW/view/en/90278134 Application description 06/2014 Qt/C++ Example of a client-server application

Upload: others

Post on 23-Mar-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

http://support.automation.siemens.com/WW/view/en/90278134

Application description 06/2014

Qt/C++

Example of a client-serverapplication

Page 2: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

Warranty and liability

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 2

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

Warranty and liability

Note The application examples are non-binding and do not claim to be complete interms of configuration and equipment or to take account of any othercontingencies. The application examples do not represent specific customersolutions; they are intended only as support for typical tasks. The user has soleresponsibility for ensuring correct operation of the products described. Theseapplication examples do not exempt the user from their due diligence obligationwith regard to application, installation, operation and maintenance. By usingthese application examples, you agree that Siemens cannot be made liable forpossible damage beyond the mentioned liability clause. We reserve the right tomake changes to these application examples at any time and without priornotice. If there are any differences between the suggestions made in theseapplication examples and other Siemens publications such as catalogs, thecontents of the other document(s) take priority.

We do not provide a warranty for any of the information contained in this document.We accept no liability for any damage or loss caused by the examples, information,programs, planning data or performance data described in this applicationexample, irrespective of the legal basis for claims arising from such damage orloss, unless liability is mandatory. For example, according to the product liabilitylaw, in cases of malfeasance, gross negligence, due to endangerment of life, bodyor health, due to assumption of a guarantee for a product's characteristics of state,due to malicious concealment of a defect or due to violation of basic contractualobligations. Any compensation for violation of basic contractual obligations,however, shall be limited to the foreseeable damage or loss which is typicallyenvisaged in contracts unless there has been gross negligence or unless liability ismandatory due to endangerment of life, body, or health. Any change to the burdenof proof to your disadvantage is not covered hereby.Any form of duplication of these application examples or excerpts hereof is notpermitted without the express consent of Siemens Industry Sector.

CautionThe functions and solutions described in this article are restricted primarily to theimplementation of the automation task. Please also observe that in case ofnetworking your plant/system with other plant units, the company network or theInternet, appropriate protective measures within the framework of industrial securitymust be adopted. For more information, see the Article ID 50203404.http://support.automation.siemens.com/WW/view/de/50203404

Page 3: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

Table of contents

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 3

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

Table of contentsWarranty and liability ................................................................................................... 2

1 Client-server structure ...................................................................................... 4

1.1 Tasks and possible applications .......................................................... 41.2 Example for a Client-server architecture .............................................. 41.3 Software components ........................................................................... 61.3.1 Used components ................................................................................ 6

2 Project engineering of the server application ................................................ 7

2.1 Project settings ..................................................................................... 72.2 NcPlcServer ......................................................................................... 92.3 Hotlink................................................................................................. 132.4 Output of the file and creation of library ............................................. 16

3 Integrating the server application into a client ............................................. 17

3.1 Preparation ......................................................................................... 173.1.1 Creating a project ............................................................................... 173.1.2 Copy server files into the project folder .............................................. 173.1.3 Insert NcPlcServer.h into a project ..................................................... 173.1.4 Linking the header file of the form with NcPlcServer.h and

NCPlcServer.lib .................................................................................. 173.2 Execution ............................................................................................ 193.2.1 Set up hotlink to variable in constructor ............................................. 193.2.2 Address hotlink ................................................................................... 193.2.3 Disconnect client from server and disconnect hotlink ........................ 193.2.4 Result ................................................................................................. 19

4 Contact persons .............................................................................................. 20

5 History............................................................................................................... 20

Page 4: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

1 Client-server structure

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 4

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

1 Client-server structure1.1 Tasks and possible applications

A client-server architecture is used to manage and edit requests from the input ofthe HMI (client) in an orderly fashion via a server application.Several clients appear for multi-channel machines, for example. One channel(HMI) is used to process a workpiece. The second channel controls a backgroundmagazine for tool sorting on a separate HMI

The reasons for a client-server application are as follows: The server controls the communication between clients and NC, PLC and drive

and machine data. An orderly structure facilitates the HMI service (especially if there are different

machine series). If several clients access one data unit, the processing is done in successive

order. This prevents failed access attempts. Due to the installation of certain functions, such as a log service, the server

can give conclusions about software malfunctions. The server could be started upon HMI ramp up and already provide valuable

information for its clients in advance. The server can be configured in such a way that the machine design and

series can be detected by the server. Relevant HMI data can also be providedby the server, assuming that the OEM has installed an HMI project on theserver. The benefit is reduced complexity.

Complex machines, with robotic loading for example, have a client-serverapplication. This allows the server to control the communication with the robotsand to coordinate the loading of the machine tools.

1.2 Example for a Client-server architectureThe layout of a client-server structure should be explained in the following. Callingup a hotlink to an NC variable is limited for this. Mirroring this structure, additionalfunctions such as a log service can be integrated. The program can run on theSINUMERIK PCU solution.Project engineering in Visual Studio 2008 requires that a unique .dll file (dynamiclibrary) be written for the server application. In the solution listed below, thecreation, management and deletion of one or more hotlinks and the adding anddeleting of clients are controlled in this .dll file in a targeted manner. The library ofthe server application must also be integrated in the respective client project sothat communication can take place.The more tasks an individual HMI must fulfill, the more important the serverapplication is, because higher-level functions and management become moreimportant. The querying of an NC variable takes place in the current example (Fig.2-1). This takes place starting with Client_1 via the server application by means ofa Qt-proprietary signal/slot process (yellow arrow). The server applicationmaintains a connection to the SlQCap service via a hotlink. If the value of the NCvariable changes, Client_1 receives a feedback message about the changed valueby means of signal/slot. This also applies to all additional clients.

Page 5: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

1 Client-server structure

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 5

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

Schematic diagramThe following figure shows the principle of a client-server architecture as anexample for the data handling of the SINUMERIK.

Fig. 2-1 Principle of client-server architecture

Required knowledgeThe following knowledge is required: Programming language C++ Programming Manual for SINUMERIK 840D sl SINUMERIK Operate programming package for Qt/C++ Development environment Qt

Page 6: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

1 Client-server structure

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 6

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

1.3 Software components

1.3.1 Used components

The application was created using the following components:

Sample files and projectsThe list below contains all the files and projects used in this example.Table 1-1

Component Number Order number Note

Visual Studio 2008Version9.0.21022.8 RTM

1 - External

Qt license fromdigia

1 - External

SINUMERIK 840DSL SINUMERIKINTEGRATECREATE MYHMI/3GL SOFTWAREVERSION 4.5 SP1

1 6FC5861-1YC41-1YA0

Creation license

SINUMERIK 840DSL SINUMERIKINTEGRATE RUNMYHMI / 3GL

1 6FC5800-0AP60-0YB0

Runtime License

Page 7: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

2 Project engineering of the server application

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 7

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

2 Project engineering of the serverapplicationFirst, the server application code must be written. Since a hotlink with ServiceSlQCap is used in the current example, two source files are recommended: "NcPlcServer.cpp"

– The connection to the client is controlled– Preparations are made for a hotlink access

"Hotlink.cpp"– Automatic creation and counting up of the hotlinks

2.1 Project settings

When the Win32 project is opened, the application type "Windows application" andthe additional option "Empty project" must be observed. The Win32 project isopened with Visual Studio 2008. The project properties have the following settings:

Configuration properties General Output directory.\Debug\outputConfiguration properties General Intermediate directory.\DebugConfiguration properties Project standards Configuration typeDynamic library (.dll)Configuration properties Debugging Command(leave blank)Configuration properties Linker General Additional librarydirectories"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_CONFIGTYPE_DEBUG)\siemens\sinumerik\hmi\osal\ace\lib";"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_CONFIGTYPE_DEBUG)\siemens\sinumerik\hmi\osal\qt\lib";"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_CONFIGTYPE_DEBUG)\lib"Configuration properties Linker Input Additional dependencies$(LIBQT_CORE_RELEASE) $(LIBQT_GUI_RELEASE)$(LIBQT_XML_RELEASE) slgfw.lib slgfwwidget.lib slcap.lib sltrc.libslmd.lib sltrp.lib slhmiutilitieslib.lib slaesvcadapter.libslfsfilesvcadapter.lib sltraceadapter.lib slarchiveadapter.libConfiguration properties Linker Extended Import library.\Debug\output\$(ProjectName).libConfiguration properties C/C++ General Additional Includedirectories:"$(HMI_SL_PP_SUBST)include";"$(HMI_SL_PP_SUBST)gui\include";"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_CONFIGTYPE_DEBUG)\siemens\sinumerik\hmi\osal\qt\include";"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_CONFIGTYPE_DEBUG)\siemens\sinumerik\hmi\osal\qt\include\qt";"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_CONFIGTYPE_DEBUG)\siemens\sinumerik\hmi\osal\ace";"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_CONFIGTYPE_DEBUG)\siemens\sinumerik\hmi\osal\ace\TAO";.\generatedfiles

Page 8: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

2 Project engineering of the server application

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 8

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

Configuration properties C/C++ General PreprocessorWIN32;_DEBUG;_WINDOWS;_USRDLL;NCPLCSERVER_EXPORTS

After the header files are added, the following configuration properties must be set:Configuration properties User-def. build step General Commandline"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_CONFIGTYPE_DEBUG)\siemens\sinumerik\hmi\osal\qt\bin\moc.exe" "$(InputPath)" -o"$(InputDir)generatedfiles/moc_$(InputName).cpp"Configuration properties User-def. build step General DescriptionMOCing $(InputFileName)...Configuration properties User-def. build step General Outputs".\GeneratedFiles\moc_$(InputName).cpp"

Page 9: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

2 Project engineering of the server application

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 9

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

2.2 NcPlcServer

The tasks of the NcPlcServer are: Add clients to server upon request Count the number of clients Establish client connection to server object:

– Creation of server object if none is available yet– Disconnect if no more clients exist

Management of hotlink requests

The header file of the NcPlcServer and the implementation file are describedbelow.//__________________________________________________________________// NcPlcServer.h --> Description of the header file//__________________________________________________________________

#ifndef _H_NCPLCSERVER_INCLUDED#define _H_NCPLCSERVER_INCLUDED

#include <QtCore/QObject>#include <QtCore/QMap>#include "qlist.h"#include "slqcap.h"#include "Hotlink.h" // Integrate later own class of hotlink

// Creation of a .dll for later, easier integration in client.#ifdef NCPLCSERVER_EXPORTS

#define NCPLCSERVER_API __declspec(dllexport)#else

#define NCPLCSERVER_API __declspec(dllimport)#endif

class NCPLCSERVER_API NcPlcServer : public QObject{ Q_OBJECT

public:

// The following declarations are for client/server//______________________________________________________________

//Create/close connection to serverstatic NcPlcServer* connectToServer(QString sClientName);static void disconnectFromServer(QString sClientName);

// The following declarations are for dynamic hotlinks//______________________________________________________________

//Functions for connecting/creating a hotlink Hotlink* attachToHotlink(QString sAddress);

void detachFromHotlink(Hotlink* p_hotlink);

//List of all hotlink objects QList<Hotlink*> listHotlinkObjects;

// The following declarations are for client/server//______________________________________________________________

Page 10: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

2 Project engineering of the server application

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 10

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

private:// Methods for removing and adding clientsvoid addClient(QString sClient);void removeClient(QString sClient);

// Variablesstatic NcPlcServer* p_serverObject;static long clientReferenceCounter;

// List of all clients QList<QString> listClients;

};

#endif _H_NCPLCSERVER_INCLUDED

//____________________________________________________________________________//____________________________________________________________________________//____________________________________________________________________________

//__________________________________________________________________// NcPlcServer.cpp --> Description of the implementation file//__________________________________________________________________#include "NcPlcServer.h"

// Pointer to the server objectNcPlcServer* NcPlcServer::p_serverObject = 0;

// Count connections of clientslong NcPlcServer::clientReferenceCounter = 0;

// Create client connection to server object// or create server object if none is available.//__________________________________________________NcPlcServer* NcPlcServer::connectToServer(QString sClientName){

// If there is no server object yetif ( 0 == p_serverObject )

{// Create new server objectp_serverObject = new NcPlcServer();

}

// If server object existsif ( p_serverObject )

{// Add name of the new clientp_serverObject->addClient(sClientName);

}

// Increment reference of the clients clientReferenceCounter++;

return p_serverObject;}

// Disconnect from server//__________________________________________________void NcPlcServer::disconnectFromServer(QString sClientName){

// If clients are still connected to the serverif ( 0 < clientReferenceCounter )

{// If server object still exists, disconnect client

Page 11: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

2 Project engineering of the server application

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 11

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

if ( p_serverObject )p_serverObject->removeClient(sClientName);

// Decrement reference of the clientsclientReferenceCounter--;

// If no more clients existif ( 0 == clientReferenceCounter ){

// Destroy server objectdelete p_serverObject;

} }}

// Insert new client in the list//__________________________________________________void NcPlcServer::addClient(QString sClient){

// Append new client at end of the list listClients.append(sClient);}

// Remove client from list//__________________________________________________void NcPlcServer::removeClient(QString sClient){

// Search for index of client and remove from list listClients.removeAt(listClients.indexOf(sClient));}

// Create hotlink or attach to hotlink//__________________________________________________Hotlink* NcPlcServer::attachToHotlink(QString sAddress){

// Status of the connection is returnedbool bStatus = false;// Auxiliary variables, whether hotlink already existsbool bAlreadyAttached = false;

// Iterator via the list of hotlinks QList<Hotlink*>::iterator it;

// Iterate via the list of hotlinksfor (it = listHotlinkObjects.begin(); it != listHotlinkObjects.end(); it++)

{// If path already exists, the following instruction existsif ( 0 == QString::compare((*it)->getAddress(),

sAddress.toUpper()) ){

// Set auxiliary variablesbAlreadyAttached = true;bStatus = true;// Increment reference(*it)->incrementReferenceCounter();// Exit loopbreak;

} }

// If hotlink does not yet existif ( !bAlreadyAttached )

{

Page 12: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

2 Project engineering of the server application

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 12

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

// New hotlink objectHotlink* p_hotlink = new Hotlink();// Create hotlink to pathbStatus = p_hotlink->createHotlink(sAddress);// Increment referencep_hotlink->incrementReferenceCounter();// Append new hotlink object to listlistHotlinkObjects.append(p_hotlink);// Return reference to new hotlink objectreturn p_hotlink;

}

// Return reference to existing hotlink objectreturn *it;

}

// Destroy hotlink or detach from hotlink//__________________________________________________void NcPlcServer::detachFromHotlink(Hotlink* p_hotlink){

// Decrement reference if 0...if ( (p_hotlink)->decrementReferenceCounter() )

{//...then remove hotlinklistHotlinkObjects.removeAt(listHotlinkObjects.indexOf(p_hotlink));

}}

Page 13: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

2 Project engineering of the server application

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 13

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

2.3 Hotlink

The tasks of the hotlink are: Creation of a hotlink connection via SlQCap service Counting up of the hotlinks Forwarding the results to interested parties

The header file and the implementation file are described below.

//__________________________________________________________________// Hotlink.h --> Description of the header file//__________________________________________________________________#ifndef _H_HOTLINK_INCLUDED#define _H_HOTLINK_INCLUDED

#include <QtCore/QObject>#include <QtCore/QMap>#include "qlist.h"#include "slqcap.h"

class Hotlink : public QObject{ Q_OBJECT

public:

// c'tor & d'tor Hotlink(void); ~Hotlink(void);

// Method for creating hotlinkbool createHotlink(QString sAddress);

// Getter for path of the machine data QString getAddress();

// Methods for editing the referencevoid incrementReferenceCounter();bool decrementReferenceCounter();

private:

// Cap server objects for communication SlQCap m_capServerHotlink; SlQCapHandle m_capHandleHotlink;

// Name of the object, or the path// e.g. /Channel/Parameter/R[U1,1]

QString m_sAddress;

// Variable in order to count the accessesint m_iReferenceCounter;

signals:

// Forward data to clientsvoid hotlinkSignal(QVariant vValue);

private slots:

Page 14: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

2 Project engineering of the server application

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 14

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

// Data from cap servervoid receiveCapDataSlot(SlCapErrorEnum, const QVariant&, constSlCapSupplementInfoType&);

};

#endif //_H_HOTLINK_INCLUDED

//__________________________________________________________________// Hotlink.h --> Description of the implementation file//__________________________________________________________________#include "Hotlink.h"

// Constructor//__________________________________________________Hotlink::Hotlink(void){

// Reference is set to zero m_iReferenceCounter= 0;}

// Destructor//__________________________________________________Hotlink::~Hotlink(void){}

// Create hotlink to cap server//__________________________________________________bool Hotlink::createHotlink(QString sAddress){

// Save path to address m_sAddress = sAddress.toUpper();

// Status variablebool bStatus = false;

// Create signal/slot connection to cap server bStatus = QObject::connect(&m_capServerHotlink,

SIGNAL(adviseData(SlCapErrorEnum, const QVariant&, constSlCapSupplementInfoType&)),this,SLOT(receiveCapDataSlot(SlCapErrorEnum, const QVariant&,const SlCapSupplementInfoType&)));

//If connection successful...if(true == bStatus)

{//...assign handler and pathSlCapErrorEnum eError = m_capServerHotlink.advise(sAddress,m_capHandleHotlink);

// If Hotlink fails, connection not successfulif(SL_CAP_OK != eError)

bStatus = false; }

// Return status of the hotlink connectionreturn bStatus;

}

// Getter for path of the machine data//__________________________________________________

Page 15: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

2 Project engineering of the server application

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 15

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

QString Hotlink::getAddress(void){

return m_sAddress;}

// Increment access reference//__________________________________________________void Hotlink::incrementReferenceCounter(void){ m_iReferenceCounter++;}

// Decrement access reference//__________________________________________________bool Hotlink::decrementReferenceCounter(void){

m_iReferenceCounter--;// Reference must never be less than zeroif(0 >= m_iReferenceCounter)

m_iReferenceCounter = 0;

// If reference <= 0 then delete hotlink objectif(m_iReferenceCounter == 0)

// return = true -> delete objectreturn true;

// return = false -> do NOT delete objectreturn false;

}

// Slot for machine data from cap server//__________________________________________________void Hotlink::receiveCapDataSlot(SlCapErrorEnum cError, constQVariant& vData, const SlCapSupplementInfoType& cSuplInfo){

if(SL_CAP_OK == cError)//Data is forwarded to all interested parties via hotlinksignalemit hotlinkSignal(vData);

}

Page 16: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

2 Project engineering of the server application

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 16

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

2.4 Output of the file and creation of libraryThe following steps must be carried out in order to receive the output files,including the library:1. Project Create new. The moc files are created.2. Add a new "GeneratedFiles" filter in the project and insert moc files as the

existing item.3. Project Create new.The library and the .dll file of the project are now located in the "Debug" folder andthe "output" subfolder in the project folder.

Page 17: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

3 Integrating the server application into a client

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 17

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

3 Integrating the server application into aclient

3.1 Preparation

3.1.1 Creating a project

A new HMI project is created in the following. The settings to be made are identicalto the settings in the documentation for the programming packet"SINUMERIK_Operate_Schnelleinstieg_neuesProjekt.pdf". In addition, aSlGfwLabel must be set up in the constructor of the form for the display of thehotlink. The class of the SlGfwLabel must be called up and a variable must begenerated in the header file of the form.

Header file:#include "SlGfwLabel.h”

private:SlGfwLabel* pItemLabel;

Constructor source code file:// Create a new SlGfwLabel

SlGfwLabel* pItemLabel = new SlGfwLabel(QString::null, this);// Move to a suitable position in the formpItemLabel->setGeometry(100,100,50,20);

3.1.2 Copy server files into the project folder

The following files from the server project must be copied into the project folder: Hotlink.h NcPlcServer.h NcPlcServer.lib

The NcPlcServer.dll file must be copied to the controller in the subfolder "appl"during the implementation.

3.1.3 Insert NcPlcServer.h into a project

The NcPlcServer.h files must be inserted into the project:Project folder Header Files Add Existing item NcPlcServer.h (from thesame project folder).

3.1.4 Linking the header file of the form with NcPlcServer.h andNCPlcServer.lib

NcPlcServer.h and NcPlcServer.lib must be linked in the header file of the form. Todo this, the following must be added in the definition of the header file:

#include "NcPlcServer.h“//Name of the lib bears the project name of the server application#pragma comment (lib, “NcPlcServer.lib")

Create slot for hotlink, pointer to NcPlcServer and hotlink, and client name.

Page 18: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

3 Integrating the server application into a client

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 18

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

Preparations for a server call must be made in the header file.private: NcPlcServer* m_myServer; //Pointer to NcPlcServer QString m_myName; //Variable for name of the client Hotlink* m_myHotlink; // Pointer to hotlink in NcPlcServer

private slots:void hotlinkSlot(QVariant vData); //Slot for hotlink

Page 19: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

3 Integrating the server application into a client

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 19

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

3.2 Execution

3.2.1 Set up hotlink to variable in constructor

The following code must be added in the constructor. First, the name of the form issaved as a client name.// Save the name of the formm_myName = rszName;

Then the connection to the server is created.// Connection to server is createdm_myServer = Server::connectToServer(m_myName);

Then the hotlink is set up.// Currently, hotlink to R-parameter 1 in channel 1m_myHotlink = m_myServer->attachToHotlink(„channel/parameter/r[u1,1]“)

// Establish the signal/slot connectionbool bResult = QObject::connect(m_myHotlink,

SIGNAL(hotlinkSignal(QVariant)),this,SLOT(hotlinkSlot(QVariant)));

3.2.2 Address hotlink

Get the results from the server in the hotlinkSlot.//Slot for receiving data/* NameOfForm must correspond to the name of the created form, in

this caseonly as an example */void NameOfForm::hotlinkSlot(QVariant vData){

pItemLabel->setValue(vData.toString());

}

3.2.3 Disconnect client from server and disconnect hotlink

Write the following code in the destructor of the form.// Disconnect client from serverNcPlcServer::disconnectFromServer(m_myName);// Detach hotlink of serverm_myServer->detachFromHotlink(m_myHotlink);// Disconnect hotlink of the formbool bResult = QObject::disconnect(m_myHotlink,

SIGNAL(hotlinkSignal(QVariant)),this,SLOT(hotlinkSlot(QVariant)));

3.2.4 Result

After the project folder is created and implementation in the SINUMERIKenvironment, the SlGfwLabel pItemLabel also gets a change of the R1 parameterand displays the value.

This example is simplified and only represents the basic architecture of a possibleclient-server application.

Page 20: Application description y 06/2014 Qt/C++ Example of a ......Application description y 06/2014 Qt/C++ Example of a client-server application. Warranty and liability Qt/C++ Client Server

4 Contact persons

Qt/C++ Client Server ApplicationEntry ID: 90278134, V1.0, 06/2014 20

Cop

yrig

htSi

emen

sAG

2014

Allr

ight

sre

serv

ed

4 Contact personsSiemens AGIndustry SectorI DT MC MTS APC2Frauenauracher Strasse 80D - 91056 Erlangen, GermanyE-mail: [email protected]

5 History

Table 5-1

Version Date Revision

V1.0 06/2014 First Edition