Escolar Documentos
Profissional Documentos
Cultura Documentos
Qt/C++
Example of a client-server
application
http://support.automation.siemens.com/WW/view/en/90278134
We accept no liability for any damage or loss caused by the examples, information,
programs, planning data or performance data described in this application
example, irrespective of the legal basis for claims arising from such damage or
loss, unless liability is mandatory. For example, according to the product liability
law, in cases of malfeasance, gross negligence, due to endangerment of life, body
or 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 contractual
obligations. Any compensation for violation of basic contractual obligations,
however, shall be limited to the foreseeable damage or loss which is typically
envisaged in contracts unless there has been gross negligence or unless liability is
mandatory due to endangerment of life, body, or health. Any change to the burden
of proof to your disadvantage is not covered hereby.
Copyright
Caution
The functions and solutions described in this article are restricted primarily to the
implementation of the automation task. Please also observe that in case of
networking your plant/system with other plant units, the company network or the
Internet, appropriate protective measures within the framework of industrial security
must be adopted. For more information, see the Article ID 50203404.
http://support.automation.siemens.com/WW/view/de/50203404
Table of contents
Table of contents
Warranty and liability................................................................................................... 2
1
Preparation ......................................................................................... 17
Creating a project ............................................................................... 17
Copy server files into the project folder .............................................. 17
Insert NcPlcServer.h into a project ..................................................... 17
Linking the header file of the form with NcPlcServer.h and
NCPlcServer.lib .................................................................................. 17
Execution ............................................................................................ 19
Set up hotlink to variable in constructor ............................................. 19
Address hotlink ................................................................................... 19
Disconnect client from server and disconnect hotlink ........................ 19
Result ................................................................................................. 19
History............................................................................................................... 20
Copyright
1 Client-server structure
Client-server structure
1.1
Copyright
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 provided
by the server, assuming that the OEM has installed an HMI project on the
server. The benefit is reduced complexity.
Complex machines, with robotic loading for example, have a client-server
application. This allows the server to control the communication with the robots
and to coordinate the loading of the machine tools.
1.2
1 Client-server structure
Schematic diagram
The following figure shows the principle of a client-server architecture as an
example for the data handling of the SINUMERIK.
Copyright
Required knowledge
The following knowledge is required:
Programming language C++
Programming Manual for SINUMERIK 840D sl
SINUMERIK Operate programming package for Qt/C++
Development environment Qt
1 Client-server structure
1.3
Software components
1.3.1
Used components
The application was created using the following components:
Copyright
Table 1-1
Component
Number
Order number
Note
External
Qt license from
digia
External
SINUMERIK 840D
SL SINUMERIK
INTEGRATE
CREATE MYHMI
/3GL SOFTWARE
VERSION 4.5 SP1
6FC5861-1YC411YA0
Creation license
SINUMERIK 840D
SL SINUMERIK
INTEGRATE RUN
MYHMI / 3GL
6FC5800-0AP600YB0
Runtime License
"Hotlink.cpp"
2.1
Project settings
When the Win32 project is opened, the application type "Windows application" and
the additional option "Empty project" must be observed. The Win32 project is
opened with Visual Studio 2008. The project properties have the following settings:
Configuration properties
General
Output directory
General
Intermediate directory
Copyright
.\Debug\output
Configuration properties
.\Debug
Configuration properties
Project standards
Configuration type
Debugging
Command
(leave blank)
Configuration properties
directories
Linker
General
Additional library
"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_CONFIGTYPE_DEBUG)\siemen
s\sinumerik\hmi\osal\ace\lib";"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_C
ONFIGTYPE_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.lib
slmd.lib sltrp.lib slhmiutilitieslib.lib slaesvcadapter.lib
slfsfilesvcadapter.lib sltraceadapter.lib slarchiveadapter.lib
Configuration properties
Linker
Extended
Import library
.\Debug\output\$(ProjectName).lib
Configuration properties
directories:
C/C++
General
Additional Include
"$(HMI_SL_PP_SUBST)include";"$(HMI_SL_PP_SUBST)gui\include";
"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_CONFIGTYPE_DEBUG)\siemen
s\sinumerik\hmi\osal\qt\include";"$(HMI_SL_PP_SUBST)$(HMI_SL_P
P_CONFIGTYPE_DEBUG)\siemens\sinumerik\hmi\osal\qt\include\qt";
"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_CONFIGTYPE_DEBUG)\siemen
s\sinumerik\hmi\osal\ace";"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_CON
FIGTYPE_DEBUG)\siemens\sinumerik\hmi\osal\ace\TAO";.\generatedf
iles
Configuration properties
C/C++
General
Preprocessor
WIN32;_DEBUG;_WINDOWS;_USRDLL;NCPLCSERVER_EXPORTS
After the header files are added, the following configuration properties must be set:
Configuration properties
line
General
Command
"$(HMI_SL_PP_SUBST)$(HMI_SL_PP_CONFIGTYPE_DEBUG)\siemen
s\sinumerik\hmi\osal\qt\bin\moc.exe" "$(InputPath)" -o
"$(InputDir)generatedfiles/moc_$(InputName).cpp"
Configuration properties
General
Description
General
Outputs
MOCing $(InputFileName)...
Configuration properties
Copyright
".\GeneratedFiles\moc_$(InputName).cpp"
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:
Copyright
#ifndef _H_NCPLCSERVER_INCLUDED
#define _H_NCPLCSERVER_INCLUDED
#include
#include
#include
#include
#include
<QtCore/QObject>
<QtCore/QMap>
"qlist.h"
"slqcap.h"
"Hotlink.h" // Integrate later own class of hotlink
private:
// Methods for removing and adding clients
void addClient(QString sClient);
void removeClient(QString sClient);
// Variables
static NcPlcServer* p_serverObject;
static long clientReferenceCounter;
// List of all clients
QList<QString> listClients;
};
#endif _H_NCPLCSERVER_INCLUDED
//____________________________________________________________________________
//____________________________________________________________________________
//____________________________________________________________________________
Copyright
//__________________________________________________________________
// NcPlcServer.cpp --> Description of the implementation file
//__________________________________________________________________
#include "NcPlcServer.h"
// Pointer to the server object
NcPlcServer* NcPlcServer::p_serverObject = 0;
// Count connections of clients
long 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 yet
if ( 0 == p_serverObject )
{
// Create new server object
p_serverObject = new NcPlcServer();
}
// If server object exists
if ( p_serverObject )
{
// Add name of the new client
p_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 server
if ( 0 < clientReferenceCounter )
{
// If server object still exists, disconnect client
10
if ( p_serverObject )
p_serverObject->removeClient(sClientName);
// Decrement reference of the clients
clientReferenceCounter--;
// If no more clients exist
if ( 0 == clientReferenceCounter )
{
// Destroy server object
delete p_serverObject;
}
}
}
Copyright
11
Copyright
12
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
Copyright
#include
#include
#include
#include
<QtCore/QObject>
<QtCore/QMap>
"qlist.h"
"slqcap.h"
13
//__________________________________________________________________
// Hotlink.h --> Description of the implementation file
//__________________________________________________________________
#include "Hotlink.h"
// Destructor
//__________________________________________________
Hotlink::~Hotlink(void)
{
}
// Create hotlink to cap server
//__________________________________________________
bool Hotlink::createHotlink(QString sAddress)
{
// Save path to address
m_sAddress = sAddress.toUpper();
// Status variable
bool bStatus = false;
Copyright
// Constructor
//__________________________________________________
Hotlink::Hotlink(void)
{
// Reference is set to zero
m_iReferenceCounter= 0;
}
14
QString Hotlink::getAddress(void)
{
return m_sAddress;
}
// Increment access reference
//__________________________________________________
void Hotlink::incrementReferenceCounter(void)
{
m_iReferenceCounter++;
}
// Decrement access reference
//__________________________________________________
bool Hotlink::decrementReferenceCounter(void)
{
Copyright
m_iReferenceCounter--;
// Reference must never be less than zero
if(0 >= m_iReferenceCounter)
m_iReferenceCounter = 0;
// If reference <= 0 then delete hotlink object
if(m_iReferenceCounter == 0)
// return = true -> delete object
return true;
// return = false -> do NOT delete object
return false;
}
// Slot for machine data from cap server
//__________________________________________________
void Hotlink::receiveCapDataSlot(SlCapErrorEnum cError, const
QVariant& vData, const SlCapSupplementInfoType& cSuplInfo)
{
if(SL_CAP_OK == cError)
//Data is forwarded to all interested parties via hotlink
signal
emit hotlinkSignal(vData);
}
15
2.4
2. Add a new "GeneratedFiles" filter in the project and insert moc files as the
existing item.
3. Project
Create new.
Copyright
The library and the .dll file of the project are now located in the "Debug" folder and
the "output" subfolder in the project folder.
16
3.1
Preparation
3.1.1
Creating a project
A new HMI project is created in the following. The settings to be made are identical
to the settings in the documentation for the programming packet
"SINUMERIK_Operate_Schnelleinstieg_neuesProjekt.pdf". In addition, a
SlGfwLabel must be set up in the constructor of the form for the display of the
hotlink. The class of the SlGfwLabel must be called up and a variable must be
generated in the header file of the form.
Header file:
#include "SlGfwLabel.h
3.1.2
Copyright
private:
SlGfwLabel* pItemLabel;
NcPlcServer.h
NcPlcServer.lib
The NcPlcServer.dll file must be copied to the controller in the subfolder "appl"
during the implementation.
3.1.3
3.1.4
Add
Existing item
Create slot for hotlink, pointer to NcPlcServer and hotlink, and client name.
17
Copyright
private slots:
void hotlinkSlot(QVariant vData); //Slot for hotlink
18
3.2
Execution
3.2.1
Copyright
3.2.2
Address hotlink
Get the results from the server in the hotlinkSlot.
3.2.3
3.2.4
Result
After the project folder is created and implementation in the SINUMERIK
environment, the SlGfwLabel pItemLabel also gets a change of the R1 parameter
and displays the value.
This example is simplified and only represents the basic architecture of a possible
client-server application.
19
4 Contact persons
Contact persons
Siemens AG
Industry Sector
I DT MC MTS APC2
Frauenauracher Strasse 80
D - 91056 Erlangen, Germany
E-mail: MC-MTS-APC-Tech-Team.i-dt@siemens.com
History
Table 5-1
Date
V1.0
06/2014
Revision
First Edition
Copyright
Version
20