SIMATIC NET S7 Programming Interface
SIMATIC NET S7 Programming Interface
S7 Programming Interface
Volume 1 of 1
5 Configuration
7 Appendix
Glossary
Index
C79000-G8976-C077 Release 7
We have checked the contents of this manual for The reproduction, transmission or use of this
agreement with the hardware described. Since document or its contents is not permitted without
deviations cannot be precluded entirely, we cannot express written authority. Offenders will be liable for
guarantee full agreement. However, the data in this damages. All rights, including rights created by patent
manual are reviewed regularly and any necessary grant or registration of a utility or design, are reserved.
corrections included in subsequent editions.
Suggestions for improvement are welcome. C79000-G8976-C077
Technical data subject to change. Copyright © Siemens AG 1995 to 2001
All Rights Reserved
Description C79000-B8976-C077/07
3
Note
We would point out that the contents of this product documentation shall not become a part of or modify any prior or existing
agreement, commitment or legal relationship. The Purchase Agreement contains the complete and exclusive obligations of Siemens.
Any statements contained in this documentation do not create new warranties or restrict the existing warranty.
We would further point out that, for reasons of clarity, these operating instructions cannot deal with every possible problem arising
from the use of this device. Should you require further information or if any special problems arise which are not sufficiently dealt with
in the operating instructions, please contact your local Siemens representative.
General
This device is electrically operated. In operation, certain parts of this device carry a dangerously high voltage.
WARNING !
Failure to heed warnings may result in serious physical injury and/or material damage.
!
Only appropriately qualified personnel may operate this equipment or work in its vicinity. Personnel must be
familiar with all warnings and maintenance measures in accordance with these operating instructions.
Correct and safe operation of this equipment requires proper transport, storage and assembly as well as careful
operator control and maintenance.
Qualified personnel as referred to in the operating instructions or in the warning notes are defined as persons who are familiar with
the installation, assembly, startup and operation of this product and who possess the relevant qualifications for their work, e.g.:
− Training in or authorization for connecting up, grounding or labeling circuits and devices or systems in accordance with current
standards in safety technology;
− Training in or authorization for the maintenance and use of suitable safety equipment in accordance with current standards in
safety technology;
S7 Programming Interface
SIMATIC S7 system components communicate with each other using the S7 communications
protocol. To allow programming device/PC application programs access to SIMATIC S7 system
components, the SAPI-S7 programming interface was developed. With its C programming
interface, you have flexible and simple access to the data of SIMATIC S7 system components.
This volume describes the S7 protocol and how to program it.
5
S7 Programming Interface C79000-G8976-C077-07
Notes
6
C79000-G8976-C077-07 S7 Programming Interface
7
S7 Programming Interface C79000-G8976-C077-07
8
C79000-G8976-C077-07 S7 Programming Interface
4.9 s7_discard_msg..................................................................................................184
5 Configuration ...................................................................................................................185
5.1 Significance of Configuration ..............................................................................186
5.2 Services With Configuration Data .......................................................................187
5.3 Configuring with STEP 7 V5................................................................................188
7 Appendix ..........................................................................................................................203
7.1 Range of Functions of SAPI-S7 ..........................................................................204
7.2 Special Notes......................................................................................................206
7.3 Formulas for Calculating Data Lengths for the Variable Services ........................207
7.4 Representation of S7 Variables...........................................................................209
7.5 Representation of the Standard Data Types........................................................210
7.5.1 Representation of the 'Boolean' Data Type.......................................................210
7.5.2 Representation of the Data Type 'Integer'.........................................................211
7.5.3 Representation of the 'Unsigned' Data Type.....................................................214
7.5.4 Representation of the 'Floating Point' Data Type..............................................216
7.5.5 Representation of the 'Visible String' Data Type...............................................218
7.5.6 Representation of the 'Octet String' Data Type.................................................219
7.5.7 Representation of the 'Bit String' Data Type .....................................................220
Glossary ..............................................................................................................................221
Index ....................................................................................................................................223
9
S7 Programming Interface C79000-G8976-C077-07
10
C79000-G8976-C077-07 S7 Programming Interface
11
S7 Programming Interface C79000-G8976-C077-07
12
C79000-G8976-C077-07 S7 Programming Interface
What Does SAPI-S7 The acronym SAPI-S7 stands for the following:
Mean?
➢ SAPI - Simple Application Programmers Interface,
➢ S7 - the layer 7 communications protocol of SIMATIC S7 sy-
stems.
13
S7 Programming Interface C79000-G8976-C077-07
Advantages of the The SAPI-S7 programming interface has the following advantages:
SAPI-S7 Pro-
➢ SAPI-S7 is a simple but nevertheless flexible and high-
gramming Interface performance interface that requires only one transferred parame-
ter field per job and allows simple buffer management. SAPI-S7
is easy to learn.
➢ The SAPI-S7 programming interface is designed for asynchro-
nous operation since asynchronous calls are more efficient
(more jobs can be processed at the same time). It allows status
messages to be received at any time and is ideally suited for
creating event-driven programs, for example under Windows.
➢ SAPI-S7 handles sequential services automatically, such as
active or passive connection establishment that consists of se-
veral individual steps. The user does not need to worry about
sequences and error handling in the individual steps. Writing a
program is therefore made much simpler.
➢ SAPI-S7 supports troubleshooting with an integrated trace
function that writes the most important transfer parameters and
return values to a file. The trace can be activated and deactiva-
ted for each individual service class.
➢ The SAPI-S7 programming interface is compatible with other
SAPI programming interfaces, in other words porting SAPI-S7
applications to applications of other SAPI programming inter-
faces (for example SAPI-FMS) requires little effort.
➢ The structures of the SAPI-S7 programming interface are de-
signed so that user programs translated with byte or word align-
ment can access the individual components without problems.
➢ Consistent configuration of SIMATIC S7 and SAPI-S7 from
STEP 7 V5 and higher.
➢ Diagnostic interface for fail-safe communication so that the
application is informed at all times of the state of the redundant
connection.
14
C79000-G8976-C077-07 S7 Programming Interface
This chapter introduces you to the principles of the SAPI-S7 programming interface, as
follows:
➢ Implementation of asynchronous calls for improved performance and the creati-
on of event-driven applications.
➢ Separation of receive calls and event-specific processing functions to simplify
the SAPI-S7 programming interface.
➢ Library-internal processing of sequential services to simplify the user programs.
➢ Introduction of a logon and logoff function allowing multi-CP and multi-user ope-
ration.
➢ Implementation of a trace for debugging the program and to allow the functions
of the library and user programs to be checked.
When you have worked through this chapter, you will be familiar with the principles of
the S7 programming interface and will be in a position to understand the interface for
creating programs.
15
S7 Programming Interface C79000-G8976-C077-07
Synchronous Job When jobs are handled synchronously (Figure 2.1), the user program is
Handling blocked from the time of the request to the reception of the confirmati-
on. Events occurring in the meantime can only be processed if the
program is interrupt-driven. This mode is not implemented in SAPI-S7.
1. Call request
indication
2. Program
waits
response
3. Resume
confirmation
program
Asynchronous Job In asynchronous job handling (Figure 2.2) a user program is not blok-
Handling ked following a request and can receive any event and react to it. The
confirmation is fetched explicitly with a receive call.
request
1. Call
indication
2. Continue
program
3. Receive
call
response
4. Continue
program
confirmation
16
C79000-G8976-C077-07 S7 Programming Interface
From the point of view of the application, you must decide whether or
not the services used are dependent on each other. If they are interde-
pendent, the execution of a job must be confirmed before the next job
can be sent. For example, a connection must be established before
data can be transferred on the connection.
17
S7 Programming Interface C79000-G8976-C077-07
How is a Received A message received from the lower protocol layers is processed by the
Message Proces- user program in the two following steps (Figure 2.3):
sed? ➢ The 's7_receive()' function first fetches an existing message.
The return value provides information about the received event.
Per event, a constant is defined in the header file 'SAPI_S7.H',
such as 'S7_INITIATE_IND' that shows that the partner station
wants to establish an S7 connection. If 'S7_NO_MSG' is entered,
no message was received.
➢ In the next step, the user must call the appropriate processing
function for the received message: an event identified by
'S7_INITIATE_IND' requires the processing function
's7_get_initiate_ind()' to be called.
Using the receive function, the application program can poll an event.
However, between the reception of the event and calling the proces-
sing function, no further event can be fetched from the communicati-
ons system.
s7_receive()
= S7_INITIATE_IND
Process indication
s7_get_initiate_ind()
= S7_OK
18
C79000-G8976-C077-07 S7 Programming Interface
Advantages of Separating the event processing into a receive function and an event-
Separating the specific processing function achieves the following:
Receive Call and ➢ Allows a simpler and clearer programming interface. Only the
Processing data and parameters relevant to the received event are transfer-
Function red to the processing functions.
➢ Allows the SAPI-S7 programming interface to be extended by
additional events by making appropriate processing functions
available.
➢ Separates the indication (receive call) and response (processing
function). This allows user data to be prepared for the response.
19
S7 Programming Interface C79000-G8976-C077-07
Support by the S7 The SAPI-S7 library supports you during active and passive connection
Library establishment that consists of several individual steps. Each step is
checked for success or failure. If an error occurs, the library makes
sure that steps that have already been executed are corrected. After
sending a request, you only need to call the receive function repeatedly
until the confirmation of completion is received (see Figure 2.4).
Initiate connection
s7_initiate_req()
= S7_OK
Repeat n times
s7_receive()
= S7_NO_MSG
s7_receive()
Fetch confirmation
s7_get_initiate_cnf()
= S7_OK
20
C79000-G8976-C077-07 S7 Programming Interface
The Three-Stage The SAPI-S7 programming interface supports a three-stage error mes-
Error Message sage concept:
Concept of the ➢ Success or failure is only indicated by the return value of the call
SAPI-S7 Pro- to simplify error handling.
gramming Interface
➢ The causes of errors are standardized in compliance with IEC
1131 (International Electrotechnical Commission) and the num-
ber of error identifiers has been reduced to a more practical size
➢ For more precise error analyses, more detailed error codes and
messages are available.
The Return Values Defines are located in the header file 'SAPI_S7.H' for the return values
of the SAPI-S7 of each SAPI-S7 call, as follows:
Programming Inter- ➢ The value 'S7_OK' indicates error-free execution of a job.
face
➢ The entry 'S7_ERR_RETRY' indicates that an error occurred
when executing a required service. This is a temporary problem
such as a brief memory shortage. The call can be repeated
without modifying the transferred parameters.
➢ The entry 'S7_ERR' also indicates an error in the execution of
the required service. In this case, however, the error does not
allow the service to be repeated. Here, steps must be taken to
eliminate the error such as assigning new parameters for the
call.
The IEC Error The S7 library provides the functions 's7_last_iec_err_no()' and
Identifiers 's7_last_iec_err_msg()' for reading out an error number and an error
message. The causes of the errors are standardized complying with
IEC 1131 (International Electrotechnical Commission) and reduce the
number of error identifiers necessary.
The Detailed Error For more detailed error analyses, the functions
Codes 's7_last_detailed_err_no()' and 's7_last_detailed_err_msg()' functions
are implemented on the SAPI-S7 programming interface. These
describe the error sources in greater detail and also provide informati-
on about eliminating the errors.
21
S7 Programming Interface C79000-G8976-C077-07
What Does Trace The trace is a simple and yet effective aid to debugging for the S7
Mean? library. This function enters data and events in a ring buffer and a trace
file.
The trace can be adapted to a wide range of applications, as follows:
➢ The trace file can be given any name to suit your application.
➢ The service classes for which the trace will be activated can be
selected.
➢ The trace depth (trace off, trace only for negative events etc.)
can be selected.
➢ The target of the trace indicates whether a trace file should be
created, an old trace file used or whether the information should
be written to the internal ring buffer.
Uses of the Trace During operation, the S7 library writes important data to the trace file.
The content of the file
➢ is used as a log of all actions performed by the S7 library,
➢ permits the sequence of events to be checked both in the appli-
cation and the library itself,
➢ allows simple debugging and checking of the data and parame-
ters on the SAPI-S7 programming interface.
You can also write to the trace file yourself. With such entries, it is
possible to save important data for test purposes, to check the se-
quence of the program or to synchronize it with the entries by the
library.
Time Required The trace and saving the data in the trace file slows down the user
program. For this reason, it is also possible to write the ring buffer to a
file as a “snapshot”.
22
C79000-G8976-C077-07 S7 Programming Interface
Purpose of the Using the mini-DB (mini-database), settings different from the standard
Mini-DB operating situation can be made and detailed information read out. The
mini-DB is a data area within the S7 library
➢ in which repeatedly required data (normally unchanged data) can
be stored,
➢ in which protocol-specific data can be stored or saved during
operation,
➢ in which identifiers of the last error to occur are saved,
➢ that can be read out and modified using functions such as
's7_mini_db_get()', 's7_mini_db_set()' etc.
Advantages of the The S7 library allows operation with default setting for the trace, the
Mini-DB establishment of S7 connections (active and passive), for uploading an
OD from the partner station etc. These default settings are adequate
for the majority of applications but can be modified and adapted to
special requirements at any time by calling the mini-DB.
The use of a mini-DB in the S7 library has the following advantages for
the user:
➢ The programming interface can be simplified to a few transfer
parameters. Values required (possibly always exactly the same
values) can be read from the mini-DB. This increases the per-
formance and simplifies the handling of the SAPI-S7 program-
ming interface.
➢ The high degree of flexibility provided by the S7 protocol is re-
tained. Changes in the settings and adapting them to the user’s
requirements are possible at any time by calling a mini-DB.
23
S7 Programming Interface C79000-G8976-C077-07
What Does Multi- In multi-user operation, the services and resources of a computer can
User Operation be used by several applications without interfering with each other.
Mean?
Requirements The main condition for using multi-CP and multi-user operation is that
the requests and confirmations as well as indications and responses
must be unambiguously assigned to each other and to the correspon-
ding application.
This condition is achieved by a logon function implemented on the
SAPI-S7 programming interface. There is a logon for
➢ a CP and
➢ for a VFD of the CP.
The VFD provides VFD-specific S7 connections for the application
(Figure 2.5). A further logon for the VFD cannot be made either by the
same application or by a different application although multiple logons
for a CP are possible.
24
C79000-G8976-C077-07 S7 Programming Interface
Relationship bet- An application can log on at several VFDs on one or more CPs. multi-
ween VFDs and the CP and multi-user operation is, however, only possible when a VFD
S7 Connection List can be assigned unequivocally to an application after the logon (1:n
assignment). When the application logs on at the CP and the selected
VFD, the connections assigned to the VFD during configuration are
available from the S7 connection list of the CP. For example, in the
following diagram, communication is possible on connections 'C1', 'C2'
and 'C3' after a logon at 'CP 1' and 'VFD 1'.
C1 C2 C3 C4 C5 C6 C7 C8
CP 1 CP 2
25
S7 Programming Interface C79000-G8976-C077-07
Installation The procedure for installation is described in the documentation for the
particular products and is not part of this manual.
Requirements for To operate the S7 library, the communication system must be ready for
Operation operation, for example, VFDs and S7 connections must be configured.
These tasks are not supported by the SAPI-S7 programming interface.
26
C79000-G8976-C077-07 S7 Programming Interface
This section introduces you to the practical use of the S7 programming interface for the
'C' programming language.
How to use the calls on the programming interface that provide you with access to S7
services and objects is described in the subsections based on example programs. The
examples
➢ clearly describe the order that must be maintained for successful communicati-
on,
➢ introduce the programming interface step by step,
➢ supplement each other whereby new functions or modified program segments of
the previous example are printed in bold face,
➢ implement an error handling routine that you can adapt to your own require-
ments.
The S7 functions are not called directly in the example programs but are separated into
their own functions (for example 'my_init()' for 's7_init()'). This is not mandatory, but
makes the program more suitable for debugging, error output and subsequent extensi-
ons.
At the end of this chapter you will be familiar with the following:
➢ Which services are available on a host system,
➢ Which services are required for which tasks,
➢ Which communications sequences occur as a result of a service request and
service confirmation,
➢ Which call and sequence structure exists generally on the S7 programming inter-
face.
You will then be familiar with the basics of the S7 programming interface and be in a
position to start programming a SAPI-S7 application.
27
S7 Programming Interface C79000-G8976-C077-07
28
C79000-G8976-C077-07 S7 Programming Interface
Description The administrative services provide functions for reading out configu-
ration information and for logging on and logging off at the communi-
cations system. There are also functions that provide the references for
symbolic S7 connection names:
The following table provides you with an overview of the administrative
services. For a more detailed description, refer to Section 3.2 page 43.
29
S7 Programming Interface C79000-G8976-C077-07
Active Connection The following table provides you with an overview of the connection
Establishment management services for active connection establishment. For a more
detailed description refer to Section 3.4.1 page 63.
30
C79000-G8976-C077-07 S7 Programming Interface
Passive Connecti- The following table provides you with an overview of the connection
on Establishment management services for passive connection establishment. For a
detailed description refer to Section 3.4.3 page 65.
Aborting a Con- The following table provides you with an overview of the connection
nection management services for aborting a connection. For a detailed
description refer to Section 3.4.7 page 70.
31
S7 Programming Interface C79000-G8976-C077-07
Description The variable services contain functions for reading and writing one or
more variables.
Reading and The following table provides you with an overview of the variable ser-
Writing a Variable vices for reading and writing a variable. For a detailed description refer
to Section 3.5.1 page 79.
Reading and The following table provides you with an overview of the variable ser-
Writing more than vices for reading and writing more than one variable. For a detailed
One Variable description refer to Section 3.5.6 page 90.
32
C79000-G8976-C077-07 S7 Programming Interface
Initiating Cyclic The following table provides you with an overview of the variable ser-
Reading with Mul- vices for cyclic reading with multiple calls. For a detailed description
tiple Calls refer to Section 3.5.10 page 101.
Receiving Data The following table provides you with an overview of the variable ser-
Cyclically vices for receiving data cyclically. For a detailed description refer to
Section 3.5.14 page 107.
Stopping and The following table provides you with an overview of the variable ser-
Aborting Cyclic vices for stopping and aborting cyclic reading. For a detailed descripti-
Reading on refer to Section 3.5.15 page 109.
33
S7 Programming Interface C79000-G8976-C077-07
Initiating Cyclic The following table provides you with an overview of the variable ser-
Reading with one vice "initiate cyclic reading with one call“. For a detailed description
Call refer to Section 3.5.20 page 114.
34
C79000-G8976-C077-07 S7 Programming Interface
35
S7 Programming Interface C79000-G8976-C077-07
36
C79000-G8976-C077-07 S7 Programming Interface
Description The following table provides an overview of the VFD services. For a
detailed description refer to Section 3.8.1 page 156.
37
S7 Programming Interface C79000-G8976-C077-07
Description With the SAPI-S7 functions, not only standard S7 connections but also
fault-tolerant connections to SIMATIC S7 H systems can be establis-
hed provided that S7 H CPUs and approved SIMATIC NET products
are used. The fault-tolerant connections must be configured with
STEP 7, downloaded to the SIMATIC S7 PLC and copied to the PC in
the form of an XDB file.
The following table provides you with an overview of the diagnostic
services for fault-tolerant connections. For a detailed description refer
to Section 3.9 page 159.
38
C79000-G8976-C077-07 S7 Programming Interface
Description of the The following example describes the administrative services for log-
Example ging on ('s7_init()') and logging off ('s7_shut()') an S7 application with
the communications system.. Communication with a local CP and the-
refore also with a remote CP is only possible after a successful logon.
When an application logs on at the local VFD, the VFD-specific resour-
ces are reserved for the application so that before the program is ter-
minated, every logon must also be terminated by a logoff.
The transfer parameters required to log on such as the CP name and
the VFD name are queried by the communications system using the
functions 's7_get_device()' or 's7_get_vfd()'.
39
S7 Programming Interface C79000-G8976-C077-07
Example
#include "sapi_s7.h"
/* prototypings */
static void my_exit(ord32 cp_descr,char *msg,int32 ret);
static void my_get_cref(ord32 cp_descr,ord16 *cref_ptr);
static void my_init(ord32 *cp_descr_ptr);
static void my_shut(ord32 cp_descr);
/* exit application */
static void my_exit(ord32 cp_descr,char *msg,int32 ret)
{
printf("\n%s = %lx",msg,ret);
my_shut(cp_descr);
}
ret=s7_get_cref(cp_descr,“TEST“,cref_ptr);
if(ret!=S7_OK)
{
my_exit(cp_descr,“Error s7_get_cref“,ret);
}
}
/* initialize s7 */
static void my_init(ord32 *cp_descr_ptr)
{ int32 ret;
char vfd_name[S7_MAX_NAMLEN+1];
char dev_name[S7_MAX_DEVICELEN+1];
ord16 number;
/* initialize s7 */
ret=s7_init(dev_name,vfd_name,cp_descr_ptr);
if(ret!=S7_OK)
{ /* something has gone wrong */
printf( "\ns7_init = %lx",ret);
exit(-3);
}
}
40
C79000-G8976-C077-07 S7 Programming Interface
/* end communication */
static void my_shut(ord32 cp_descr)
{ int32 ret;
ret=s7_shut(cp_descr);
if(ret!=S7_OK)
{ /* error has occurred -> exit */
printf( "\ns7_shut = %lx",ret);
}
/* main */
void main(void)
{ ord32 cp_descr;
ord16 cref;
/* initialize s7 */
my_init(&cp_descr);
/* end communication */
my_shut(cp_descr);
}
41
S7 Programming Interface C79000-G8976-C077-07
Flowchart
s7_get_device()
= S7_OK,
number!=0
s7_get_vfd()
= S7_OK,
number!=0
s7_init()
= S7_OK
s7_get_cref()
= S7_OK
s7_shut()
= S7_OK
42
C79000-G8976-C077-07 S7 Programming Interface
3.2.1 s7_get_device
Description With this call, the user program can query all the configured names of
the communications processors installed in the communications sy-
stem. The names are relevant for logging on with 's7_init()'.
Parameters index The 'index' parameter selects one of the existing CPs
that are numbered continuously starting at 0.
Querying All CPs To query all CPs, it is best to use a program loop. The 'index' parame-
ter is used as the loop variable and runs from 0 to '*number_ptr-1'. The
'*number_ptr' has the default 1.
43
S7 Programming Interface C79000-G8976-C077-07
44
C79000-G8976-C077-07 S7 Programming Interface
3.2.2 s7_get_vfd
Description With this call, the user program can query all the configured VFDs of a
communications processor that were assigned connetions. The VFD
names are relevant for the logon with 's7_init()'.
Querying All VFDs To query all VFDs, it is best to use a program loop. The 'index' para-
meter is used as the loop variable and runs from 0 to '*number_ptr-1'.
The '*number_ptr' has the default 1.
45
S7 Programming Interface C79000-G8976-C077-07
46
C79000-G8976-C077-07 S7 Programming Interface
3.2.3 s7_init
Description With this call, the user program logs on at a VFD of an underlying
communications system.
As input parameters, the user program specifies the configured name
of the communications processor via which communication will take
place and the configured name of the VFD whose resources and servi-
ces will be used.
The user program receives a descriptor 'cp_descr' that must be speci-
fied for all subsequent calls as an addressing parameter for the se-
lected CP and VFD until the application logs off.
When the program logs on, the configuration information (for example,
the list of all S7 connections) is read from a file with a name derived
from the name of the CP as follows: The colon completing the CP na-
me is removed and the name extension '.LDB' is added. This assumes
that the file exists in the current working directory. As an alternative,
the name of the configuration file can also be assigned (using any na-
me) in the environment variable.
If several VFDs are being used at the same time in one or more com-
munications processors by one application, a corresponding number of
's7_init()' calls are necessary.
47
S7 Programming Interface C79000-G8976-C077-07
48
C79000-G8976-C077-07 S7 Programming Interface
3.2.4 s7_get_cref
Description From the communication system, the S7 application only sees the
symbolic name of the S7 connection that is cumbersome during opera-
tion. For this reason, the application uses the 's7_get_cref()' call to
obtain a reference to a symbolic connection name.
49
S7 Programming Interface C79000-G8976-C077-07
3.2.5 s7_get_conn
Description With this call, the user program can query all the configured S7 con-
nections of a VFD and their references on a communications proces-
sor. This identifier selects the real connection on the network and is
easier to use than the symbolic name.
Querying all S7 To query all S7 connections, it is best to use a program loop. The 'in-
Connections dex' parameter is used as the loop variable and runs from 0 to
'*number_ptr-1'. The '*number_ptr' has the default 1.
50
C79000-G8976-C077-07 S7 Programming Interface
51
S7 Programming Interface C79000-G8976-C077-07
3.2.6 s7_shut
Description With the 's7_shut()' call, the application cancels the logon ('s7_init()') at
the communications processor identified by the CP descriptor. All the
resources of the communications system are returned and established
connections are terminated. This call must therefore be made once for
every logon before the program is terminated.
52
C79000-G8976-C077-07 S7 Programming Interface
Description of the To extend the example in Section 3.2 the receive call 's7_receive()' is
Example used to check whether a message exists. In this simple case, a messa-
ge is not expected ('S7_NO_MSG' as return value); the example is
simply in preparation for programs coming later.
Example
:
:
/* additional prototypings */
static void my_receive(ord32 cp_descr,int32 last_event_expected);
do
{ ret=s7_receive(cp_descr,&cref,&orderid);
switch(ret)
{ case S7_NO_MSG:
break;
default:
s7_discard_msg();
printf("\nEvent unexpected: %lx", ret);
break;
}
} while(ret!=last_event_expected);
}
/* main */
void main(void)
{ ord32 cp_descr;
ord16 cref;
/* initialize s7 */
my_init(&cp_descr);
/* receive message */
my_receive(cp_descr,S7_NO_MSG);
/* end communication */
my_shut(cp_descr);
}
53
S7 Programming Interface C79000-G8976-C077-07
Flowchart
s7_receive()
= S7_NO_MSG
54
C79000-G8976-C077-07 S7 Programming Interface
3.3.1 s7_receive
Description The receive function of the S7 library has the central task of analyzing
events received from the underlying communications system and to
report these directly to the application without any further processing.
The 's7_receive()' call is absolutely necessary when the client
➢ has sent acknowledged calls and is waiting for the acknow-
ledgment from the server,
➢ wishes to receive unacknowledged messages from the network,
➢ has sent sequential jobs (for example establishing an S7 con-
nection) to ensure processing of the service sequence until it is
completed.
The return value of the 's7_receive()' function provides a service iden-
tifier when a message is received. This identifies the type of service of
the received response (for example 'S7_READ_CNF' when a variable
was read). After receiving a message with 's7_receive()', the call for
the corresponding processing function is mandatory (for example
's7_get_read_cnf()'). Further receive calls are otherwise rejected with
an error message.
55
S7 Programming Interface C79000-G8976-C077-07
56
C79000-G8976-C077-07 S7 Programming Interface
57
S7 Programming Interface C79000-G8976-C077-07
Description of the To extend the example from Section 3.3, an S7 connection with the
Example symbolic name 'TEST' is established ('s7_initiate_req()') and then
aborted ('s7_abort()') after the confirmation ('s7_get_initiate_cnf()') is
received.
Example :
:
/* additional prototypings */
static void my_abort(ord32 cp_descr,ord16 cref);
static void my_get_abort_ind(ord32 cp_descr);
static void my_get_initiate_cnf(ord32 cp_descr);
static void my_initiate_req(ord32 cp_descr,ord16 cref);
/* abort connection */
static void my_abort(ord32 cp_descr,ord16 cref)
{ int32 ret;
if((ret=s7_abort(cp_descr,cref))!=S7_OK)
{
my_exit(cp_descr,"Error s7_abort",ret);
}
}
if((ret=s7_get_abort_ind())!=S7_OK)
{
my_exit( cp_descr,
}
}
if((ret=s7_get_initiate_cnf())!=S7_OK)
{
my_exit( cp_descr,
}
}
if((ret=s7_initiate_req( cp_descr,cref))!=S7_OK)
{
my_exit(cp_descr,"Error
s7_initiate_req",ret);
}
}
58
C79000-G8976-C077-07 S7 Programming Interface
do
{ ret=s7_receive(cp_descr,&cref,&orderid);
switch(ret)
{
:
:
case S7_INITIATE_CNF:
my_get_initiate_cnf(cp_descr);
my_abort(cp_descr,cref);
break;
case S7_ABORT_IND:
my_get_abort_ind(cp_descr);
break;
default:
printf( "Event
unexpected", ret);
break;
}
} while( (ret!=last_event_expected)&&
}
/* main */
void main(void)
{ ord32 cp_descr;
ord16 cref;
/* initialize s7 */
my_init(&cp_descr);
/* initiate connection */
my_initiate_req(cp_descr,cref);
/* end communication */
my_shut(cp_descr);
}
59
S7 Programming Interface C79000-G8976-C077-07
Flowchart for
Active Connection
Establishment
= S7_OK
s7_receive()
= S7_NO_MSG
s7_receive()
= S7_INITIATE_CNF
Message (confirmation) there!
Fetch confirmation
s7_get_initiate_cnf()
= S7_OK
60
C79000-G8976-C077-07 S7 Programming Interface
Flowchart for
Preparing for
Passive
Connection
Establishment (not
part of example)
= S7_OK
s7_receive()
= S7_NO_MSG
s7_receive()
= S7_AWAIT_INITIATE_CNF
Message (confirmation) there!
Fetch confirmation
s7_get_await_initiate_cnf()
= S7_OK
61
S7 Programming Interface C79000-G8976-C077-07
Flowchart for
Passive
Connection
Establishment (not
part of example)
s7_receive()
= S7_NO_MSG
s7_receive()
= S7_INITIATE_IND
s7_get_initiate_ind()
Fetch indication
s7_initiate_rsp()
= S7_OK
62
C79000-G8976-C077-07 S7 Programming Interface
3.4.1 s7_initiate_req
63
S7 Programming Interface C79000-G8976-C077-07
3.4.2 s7_get_initiate_cnf
Parameters None
64
C79000-G8976-C077-07 S7 Programming Interface
3.4.3 s7_await_initiate_req
65
S7 Programming Interface C79000-G8976-C077-07
3.4.4 s7_get_await_initiate_cnf
Parameters None
66
C79000-G8976-C077-07 S7 Programming Interface
3.4.5 s7_get_initiate_ind
Parameters None
67
S7 Programming Interface C79000-G8976-C077-07
3.4.6 s7_initiate_rsp
Description Using the 's7_initiate_rsp()' call, the user program decides about a
passive establishment request.
The passive establishment of an S7 connection is in two parts as fol-
lows:
➢ With the 's7_get_initiate_ind()' call, the capability parameters are
entered in the mini-DB and can be read out. It is then possible to
receive further events from the communications system (for ex-
ample via other S7 connections).
➢ The establishment request is granted or rejected with the
's7_initiate_rsp()' call.
68
C79000-G8976-C077-07 S7 Programming Interface
69
S7 Programming Interface C79000-G8976-C077-07
3.4.7 s7_abort
70
C79000-G8976-C077-07 S7 Programming Interface
3.4.8 s7_get_abort_ind
Parameters None
71
S7 Programming Interface C79000-G8976-C077-07
Description of the To extend the example from Section 3.4, a job to read a variable cycli-
Example cally ('s7_cycl_read()') is sent on the established S7 connection. The
received data are copied to the user memory with the function
's7_get_cycl_read_ind()'. The cycle is completed with
's7_cycl_read_delete_req()'.
Symbolic Variable S7 variables are addressed symbolically. This type of access is orien-
Addressing ted on the notation of S7 tools. You do not need to learn different nota-
tions for variable addresses.
Examples:
DB5,X12.1 data block 5, data byte 12, data bit 1
DB5,B12 data block 5, data byte 12
DB5,W10 data block 5, data word 10
MB9,3 3 memory bytes starting at memory byte 9
DB5,W10,9 9 words in data block 5 starting at data word 10
Syntax:
The syntax is defined as follows (upper and lower case irrelevant):
Parameter Description:
72
C79000-G8976-C077-07 S7 Programming Interface
<range> Q output
C Counter - Detailed information on this
parameter can be found below in a se-
parate paragraph.
I input
M memory bit
PQ peripheral output
PI peripheral input
T Timer - Detailed information on this pa-
rameter can be found below in a sepa-
rate paragraph.
C Counter - Detailed information on this
parameter can be found below in a se-
parate paragraph.
73
S7 Programming Interface C79000-G8976-C077-07
Explanation of the The following table illustrates the meaning of the individual bits of the
Type 'T' of the result data of type 'T' of the 'area' parameter.
'Area' Parameter
Bit no. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Meaning 0 0 x x t t t t t t t t t t t t
Explanantions of The following table illustrates the meaning of the individual bits of the
the type 'C' or 'Z' of result data of type 'C' or 'Z' of the 'area' parameter.
the 'area'
parameter
Bit no. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Meaning 0 0 0 0 t t t t t t t t t t t t
74
C79000-G8976-C077-07 S7 Programming Interface
Example
:
:
/* additional prototypings */
static void my_cycl_read(ord32 cp_descr,ord16 cref,ord16 orderid);
static void my_cycl_read_delete_req( ord32 cp_descr,
ord16 cref,
ord16 orderid);
static void my_get_cycl_read_delete_cnf(ord32 cp_descr);
static void my_get_cycl_read_ind(ord32 cp_descr);
read_para.access=S7_ACCESS_SYMB_ADDRESS;
strcpy(read_para.var_name,“e0,10“);
ret=s7_cycl_read(cp_descr,cref,orderid,200,1,&read_para);
if(ret!=S7_OK)
{
my_exit( cp_descr,
"Error s7_cycl_read",
ret);
}
}
ret=s7_cycl_read_delete_req(cp_descr,cref,orderid);
if(ret!=S7_OK)
{
my_exit( cp_descr,
"Error s7_cycl_read",
ret);
}
}
if((ret=s7_get_cycl_read_delete_cnf())!=S7_OK)
{
my_exit( cp_descr,
"Error s7_get_cycl_read_delete_cnf",
ret);
}
}
75
S7 Programming Interface C79000-G8976-C077-07
value_array[0]=data_buffer;
do
{ ret=s7_receive(cp_descr,&cref,&orderid);
switch(ret)
{
:
:
case S7_INITIATE_CNF:
my_get_initiate_cnf(cp_descr);
my_cycl_read(cp_descr,cref,0);
break;
case S7_CYCL_READ_IND:
my_get_cycl_read_ind(cp_descr);
my_cycl_read_delete_req(
cp_descr,
cref,
orderid);
break;
case S7_CYCL_READ_DELETE_CNF:
my_get_cycl_read_delete_cnf(
cp_descr);
my_abort(cp_descr,cref);
break;
default:
printf( "Event unexpected",
ret);
break;
}
} while( (ret!=last_event_expected)&&
(ret!=S7_ABORT_IND));
}
76
C79000-G8976-C077-07 S7 Programming Interface
/* main */
void main(void)
{ ord32 cp_descr;
ord16 cref;
/* initialize s7 */
my_init(&cp_descr);
/* initiate connection */
my_initiate_req(cp_descr,cref);
/* end communication */
my_shut(cp_descr);
}
77
S7 Programming Interface C79000-G8976-C077-07
Flowchart
= S7_OK
s7_receive()
= S7_NO_MSG
s7_receive()
= S7_CYCL_READ_IND
Message (indication) there!
Fetch indication
s7_get_cycl_read_ind
= S7_OK
78
C79000-G8976-C077-07 S7 Programming Interface
3.5.1 s7_read_req
Description With the 's7_read_req()' call, a client application can read a variable on
the server. The variables can only be accessed using their symbolic
addresses. The data are transferred to the client in the confirmation
from the server and entered in the user buffer by the corresponding
processing function ('s7_get_read_cnf()').
struct S7_READ_PARA
{
ord16 access;
char var_name[S7_MAX_NAMLEN+2];
ord16 index;
ord16 subindex;
ord16 address_len;
ord8 address[S7_MAX_ADDRESSLEN];
}
The 'access' parameter indicates the type of access.
With 'S7_ACCESS_SYMB_ADDRESS', the symbolic
address in the 'var_name' field is expected.
The 'var_name' parameter specifies the symbolic
address of the variable to be read and is evaluated if
the variable is to be accessed by its symbolic ad-
dress ('access=S7_ACCESS_SYMB_ADDRESS').
(Please refer to the general information about varia-
ble addressing at the start of this section.)
79
S7 Programming Interface C79000-G8976-C077-07
Return Values
S7_OK The function was processed without errors.
Amounts of Data The maximum length of the result data depends on the negotiated
PDU size. For details of the PDU size, refer to Section 3.4.2,
's7_get_initiate_cnf', page 64 (formulas for calculating intermediate
values in Section 7.3, page 207).
The following table shows examples:
80
C79000-G8976-C077-07 S7 Programming Interface
3.5.2 s7_get_read_cnf
Description The 's7_get_read_cnf()' call receives a variable value that has be read.
With the 's7_receive()' call, the user program receives the
'S7_READ_CNF' confirmation if the read job could be executed. Fol-
lowing this, the corresponding processing function 's7_get_read_cnf()'
must be called for internal processing in the library so that the values
can be copied to the user buffer.
81
S7 Programming Interface C79000-G8976-C077-07
82
C79000-G8976-C077-07 S7 Programming Interface
3.5.3 s7_write_req
Description With the 's7_write_req()' call, a client application can write to a variable
of a server. The variables can only be accessed using their symbolic
addresses. The data are transferred in the request from the client to
the server.
struct S7_WRITE_PARA
{ ord16 access;
char var_name[S7_MAX_NAMLEN+2];
ord16 index;
ord16 subindex;
ord16 address_len;
ord8 address[S7_MAX_ADDRESSLEN];
ord16 var_length;
ord8 value[S7_MAX_BUFLEN];
}
83
S7 Programming Interface C79000-G8976-C077-07
84
C79000-G8976-C077-07 S7 Programming Interface
The maximum length of the user data depends on the negotiated PDU
size. For details of the PDU size, refer to Section 3.4.2,
's7_get_initiate_cnf', page 64 (formulas for calculating intermediate
Amounts of Data values in Section 7.3, page 207).
The following table shows examples:
*) Note
With the 's7_write_long_req' call, longer data are possible.
85
S7 Programming Interface C79000-G8976-C077-07
3.5.4 s7_write_long_req
struct S7_WRITE_PARA
{ ord16 access;
char var_name[S7_MAX_NAMLEN+2];
ord16 index;
ord16 subindex;
ord16 address_len;
ord8 address[S7_MAX_ADDRESSLEN];
ord16 var_length;
ord8 value[S7_MAX_BUFLEN_LONG];
}
86
C79000-G8976-C077-07 S7 Programming Interface
87
S7 Programming Interface C79000-G8976-C077-07
The maximum length of the user data depends on the negotiated PDU
size. For details of the PDU size, refer to Section 3.4.2,
's7_get_initiate_cnf', page 64 (formulas for calculating intermediate
Amounts of Data values in Section 7.3, page 207).
The following table shows examples:
88
C79000-G8976-C077-07 S7 Programming Interface
3.5.5 s7_get_write_cnf
Description The 's7_get_write_cnf()' call receives the result of the write variable
job.
With the 's7_receive()' call, the user program receives the
'S7_WRITE_CNF' confirmation if the write job was executed. Following
this, the corresponding processing function 's7_get_write_cnf()' must
be called for internal processing in the library.
Parameters None
89
S7 Programming Interface C79000-G8976-C077-07
3.5.6 s7_multiple_read_req
Description With the 's7_multiple_read_req()' call, a client application can read one
or more variables on the server at the same time. The variables can
only be accessed using their symbolic addresses. The data are trans-
ferred in the confirmation from the server to the client and transferred
to the user buffer by the corresponding processing function
('s7_get_multiple_read_cnf()').
90
C79000-G8976-C077-07 S7 Programming Interface
struct S7_READ_PARA
{ ord16 access;
char var_name[S7_MAX_NAMLEN+2];
ord16 index;
ord16 subindex;
ord16 address_len;
ord8 address[S7_MAX_ADDRESSLEN];
}
91
S7 Programming Interface C79000-G8976-C077-07
Amounts of Data The maximum length of the variable addresses and result data de-
pends on the negotiated PDU size. For details of the PDU size, refer to
Section 3.4.2, 's7_get_initiate_cnf', page 64 (formulas for calculating
intermediate values in Section 7.3, page 207).
The following table shows examples:
92
C79000-G8976-C077-07 S7 Programming Interface
3.5.7 s7_get_multiple_read_cnf
S7_RESULT_OK
This value indicates that the access was possible and
no error occurred.
S7_RESULT_HW_ERROR
A hardware problem occurred.
S7_RESULT_OBJ_ACCESS_DENIED
Access to a variable was denied.
S7_RESULT_OBJ_ADDRESS_INVALID
The specified address is invalid.
93
S7 Programming Interface C79000-G8976-C077-07
S7_RESULT_OBJ_TYPE_NOT_SUPPORTED
The server does not support the data type.
S7_RESULT_OBJ_TYPE_INCONSISTENT
The data type of the variable is not consistent.
S7_RESULT_OBJ_NOT_EXISTS
The variable does not exist.
94
C79000-G8976-C077-07 S7 Programming Interface
95
S7 Programming Interface C79000-G8976-C077-07
3.5.8 s7_multiple_write_req
struct S7_WRITE_PARA
{
ord16 access;
char var_name[S7_MAX_NAMLEN+2];
ord16 index;
ord16 subindex;
ord16 address_len;
ord8 address[S7_MAX_ADDRESSLEN];
ord16 var_length;
ord8 value[S7_MAX_BUFLEN_MULTIPLE];
}
96
C79000-G8976-C077-07 S7 Programming Interface
97
S7 Programming Interface C79000-G8976-C077-07
Amounts of Data The maximum number of variable addresses and maximum length of
the user data depends on the negotiated PDU size. For details of the
PDU size, refer to Section 3.4.2, 's7_get_initiate_cnf', page 64
(formulas for calculating intermediate values in Section 7.3, page 208).
The following table shows examples:
98
C79000-G8976-C077-07 S7 Programming Interface
3.5.9 s7_get_multiple_write_cnf
S7_RESULT_OK
This value indicates that the access was possible and
no error occurred.
S7_RESULT_HW_ERROR
A hardware problem occurred.
S7_RESULT_OBJ_ACCESS_DENIED
Access to a variable was denied.
S7_RESULT_OBJ_ADDRESS_INVALID
The specified address is invalid.
S7_RESULT_OBJ_TYPE_NOT_SUPPORTED
The server does not support the data type.
S7_RESULT_OBJ_TYPE_INCONSISTENT
The data type of the variable is not consistent.
S7_RESULT_OBJ_NOT_EXISTS
The variable does not exist.
99
S7 Programming Interface C79000-G8976-C077-07
100
C79000-G8976-C077-07 S7 Programming Interface
3.5.10 s7_cycl_read_init_req
Description With this request, an S7 application instructs the server to prepare for
cyclic reading of variables. The request contains the cycle time, the
number of variables and the variables to be read.
101
S7 Programming Interface C79000-G8976-C077-07
102
C79000-G8976-C077-07 S7 Programming Interface
Amounts of Data The maximum length of the variable addresses and result data de-
pends on the negotiated PDU size. For details of the PDU size, refer to
Section 3.4.2, 's7_get_initiate_cnf', page 64 (formulas for calculating
intermediate values in Section 7.3, page 208).
The following table shows examples:
103
S7 Programming Interface C79000-G8976-C077-07
3.5.11 s7_get_cycl_read_init_cnf
Parameters None
104
C79000-G8976-C077-07 S7 Programming Interface
3.5.12 s7_cycl_read_start_req
Description With this request, an S7 application instructs the server to start cyclic
reading of variables. The server must already have been prepared with
the 's7_cycl_read_init_req()' call.
105
S7 Programming Interface C79000-G8976-C077-07
3.5.13 s7_get_cycl_read_start_cnf
Parameters None
106
C79000-G8976-C077-07 S7 Programming Interface
3.5.14 s7_get_cycl_read_ind
Description The 's7_get_cycl_read_ind()' call receives the data sent by the server.
With the 's7_receive()' call, the user program receives the
'S7_CYCL_READ_IND' indication if a remote partner read a variable
cyclically. Following this, the corresponding processing function
's7_get_cycl_read_ind()' is called to copy the values that were read into
the user buffer.
S7_RESULT_OK
This value indicates that the access was possible and
no error occurred.
S7_RESULT_HW_ERROR
A hardware problem occurred.
S7_RESULT_OBJ_ACCESS_DENIED
Access to a variable was denied.
S7_RESULT_OBJ_ADDRESS_INVALID
The specified address is invalid.
107
S7 Programming Interface C79000-G8976-C077-07
S7_RESULT_OBJ_TYPE_NOT_SUPPORTED
The server does not support the data type.
S7_RESULT_OBJ_TYPE_INCONSISTENT
The data type of the variable is not consistent.
S7_RESULT_OBJ_NOT_EXISTS
The variable does not exist.
108
C79000-G8976-C077-07 S7 Programming Interface
3.5.15 s7_get_cycl_read_abort_ind
Parameters None
109
S7 Programming Interface C79000-G8976-C077-07
3.5.16 s7_cycl_read_stop_req
Description With this request, an S7 application instructs the server to stop cyclic
reading of variables. The server must already have been requested to
read variables cyclically.
110
C79000-G8976-C077-07 S7 Programming Interface
3.5.17 s7_get_cycl_read_stop_cnf
Parameters None
111
S7 Programming Interface C79000-G8976-C077-07
3.5.18 s7_cycl_read_delete_req
Description This function stops cyclic reading and logs off at the server.
112
C79000-G8976-C077-07 S7 Programming Interface
3.5.19 s7_get_cycl_read_delete_cnf
Parameters None
113
S7 Programming Interface C79000-G8976-C077-07
3.5.20 s7_cycl_read
Description With this job, an S7 application instructs the server to read variables
cyclically. This job includes the sequence consisting of the calls
's7_cycl_read_init_req()' (logon at server) and
's7_cycl_read_start_req()' (start reading variables). The cycle time, the
number of variables and the variables to be read are specified.
Important: in S7, this is an unacknowledged service (in contrast to the
individual jobs that make up this call).
114
C79000-G8976-C077-07 S7 Programming Interface
struct S7_READ_PARA
{ ord16 access;
char var_name[S7_MAX_NAMLEN+2];
ord16 index;
ord16 subindex;
ord16 address_len;
ord8 address[S7_MAX_ADDRESSLEN];
}
115
S7 Programming Interface C79000-G8976-C077-07
116
C79000-G8976-C077-07 S7 Programming Interface
Restrictions The block-oriented services are not available under MS-DOS and
Windows 3.x.
Standard Functi- The blocks BSEND and BRCV are available on the SIMATIC S7 PLC.
ons This functionality takes the form of 'block-oriented services' on the
programming device/PC.
Data Exchange A connection must be configured to allow data exchange between two
communication partners. With the block-oriented services, the block
pair BSEND and BRCV belong together. The connection must be con-
figured at both ends (COML S7 and STEP 7 NETPRO).
From the SIMATIC NET CD, November 99 onwards, configuration with
COML S7 is no longer necessary. The configuration file for the PC is
created by STEP 7; see Installation Instructions of your product.
In contrast to the non block-oriented services, the configured connecti-
on created with STEP 7 NETPRO must be downloaded to the PLC.
Several pairs of blocks can exchange data via one connection.
When configuring the connections, follow the instructions in the rele-
vant product information bulletins.
Amount of Data With this type of data transfer, up to 65534 bytes can be transferred
regardless of the size of the CPU. The data is segmented automatically
by the functions.
Address Parameter The address parameter R_ID is fixed for a block pair (BSEND/ BRCV)
and defined uniquely within a connection. This means that several
BSEND blocks can transmit via one connection with each using a diffe-
rent R_ID. The same R_IDs can be used for other connections.
117
S7 Programming Interface C79000-G8976-C077-07
Grouping of the The following table divides the block-oriented services into two groups:
Functions
➢ The 'bsend' group
➢ The 'brcv' group
118
C79000-G8976-C077-07 S7 Programming Interface
Example of 'bsend'
/* BSEND confirmation */
case S7_BSEND_CNF:
{
ret = s7_get_bsend_cnf();
printf("s7_get_bsend_cnf = 0x%x\n",ret );
if(ret == S7_OK)
{
/* end communication */
my_shut(cp_descr);
}
119
S7 Programming Interface C79000-G8976-C077-07
Flowchart for
'bsend'
s7_bsend_req()
= S7_OK
s7_receive()
= S7_NO_MSG
s7_receive()
= S7_BSEND_CNF
s7_get_bsend_cnf
= S7_OK
120
C79000-G8976-C077-07 S7 Programming Interface
Example of 'brcv'
/* BRCV initialize */
ret = s7_brcv_init( cp_descr,cref,r_id);
printf("s7_brcv_init =0x%x, r_id = 0x%x\n",ret, r_id;
while (ret == S7_NO_MSG )
{
ret = s7_receive(cp_descr,&cref,&orderid);
printf( "s7_receive = 0x%x\n", ret);
switch(ret)
{
/* BRCV indication */
case S7_BRCV_IND:
{
ret = s7_get_brcv_ind(
receive_buffer,
(ord32)sizeof(receive_buffer),
&ret_id,&ret_len);
printf( "s7_get_brcv_ind = 0x%x, r_id = 0x%x,
rec_len = %d Byte\n",ret, ret_id,
ret_len);
break;
}
default:
{
...
break;
}
}
}
/* BRCV stop */
ret = s7_brcv_stop( cp_descr,cref,r_id);
printf("s7_brcv_stop = 0x%x",ret);
/* end communication */
my_shut(cp_descr);
}
121
S7 Programming Interface C79000-G8976-C077-07
s7_brcv_init()
= S7_OK
s7_receive()
= S7_NO_MSG
s7_receive()
= S7_BRCV_IND
Receive all net data from
Partner (BSEND)!
s7_get_brcv_ind
= S7_OK
s7_brcv_stop
Stop brcv explicitly
= S7_OK
122
C79000-G8976-C077-07 S7 Programming Interface
3.6.1 s7_bsend_req
Description With the s7_bsend_req call, a client application can send up to 65534
bytes of data to a remote station.
123
S7 Programming Interface C79000-G8976-C077-07
124
C79000-G8976-C077-07 S7 Programming Interface
3.6.2 s7_get_bsend_cnf
None
Parameters
125
S7 Programming Interface C79000-G8976-C077-07
3.6.3 s7_brcv_init
Description With this call, the application logs on to receive BSEND jobs from the
connection partner. Each BSEND job with a specific R_ID of the con-
nection partner must correspond to exactly one s7_brcv_init with the
same R_ID.
126
C79000-G8976-C077-07 S7 Programming Interface
127
S7 Programming Interface C79000-G8976-C077-07
3.6.4 s7_get_brcv_ind
Description With the s7_get_brcv_ind call, the net data sent by the partner are
copied to the specified memory area.
128
C79000-G8976-C077-07 S7 Programming Interface
129
S7 Programming Interface C79000-G8976-C077-07
3.6.5 s7_brcv_stop
Description With this call, the application logs off at the connection partner.
130
C79000-G8976-C077-07 S7 Programming Interface
Description Using the message service, the application programmer can receive
messages from a SIMATIC S7 programmable controller.
These services have the following advantages:
➢ Transfer of monitored data only when changed!
➢ Any associated values can be added to the transferred data.
Restrictions The message services are not available under MS-DOS and Win-
dows 3...
Standard Functi- In SAPI S7, the following two types of message are processed:
ons
➢ configured messages (SCAN)
➢ programmed messages (ALARM, ALARM_8, ALARM_8P,
NOTIFY)
Programmed Mes- To use programmed messages, the user must include an S7 block
sages (ALARM) (ALARM, ALARM_8, ALARM_8P, NOTIFY) in the S7 user program.
This block queries signals every cycle to check whether or not they
have changed and then immediately sends a frame with the event
state of the signals, time of day and associated values. Messages can
also be received from PCS 7 blocks with message capability, for ex-
ample technological functions.
131
S7 Programming Interface C79000-G8976-C077-07
132
C79000-G8976-C077-07 S7 Programming Interface
Flowchart for
'scan'
ogon
s7_msg_initiate_req()
Logon for SCAN
=S7_OK
s7_receive() + acknowledge
=S7_MSG_INITIATE_CNF
s7_get_msg_initiate_cnf()
Receive messages
s7_receive()
SCAN_to_all
=S7_NO_MSG Acyclic frames
s7_receive()
=S7_SCAN_IND
Receive messages from
partner!
s7_get_scan_ind()
=S7_OK
Logoff
s7_msg_abort_req()
Logoff for SCAN
=S7_OK
s7_receive() + acknowledge
=S7_MSG_ABORT_CNF
s7_get_msg_abort_cnf()
133
S7 Programming Interface C79000-G8976-C077-07
s7_receive() + Acknowledge
=S7_MSG_INITIATE_CNF
s7_get_msg_initiate_cnf()
Receive messages
s7_receive()
ALARM_8P frames
=S7_NO_MSG acyclic
s7_receive()
=S7_ALARM_IND
Receive messages from
partner!
s7_get_alarm_ind()
=S7_OK
Logoff
s7_receive() + Acknowledge
=S7_MSG_ABORT_CNF
s7_get_msg_abort_cnf()
134
C79000-G8976-C077-07 S7 Programming Interface
3.7.1 s7_msg_initiate_req
135
S7 Programming Interface C79000-G8976-C077-07
3.7.2 s7_get_msg_initiate_cnf
Description With the 's7_receive' call, the user program receives the confirmation
S7_MSG_INITIATE_CNF when the logon is completed. The function
's7_get_msg_initiate_cnf' described here must then be called to obtain
the result of an ALARM or SCAN logon.
Parameters None
136
C79000-G8976-C077-07 S7 Programming Interface
3.7.3 s7_msg_abort_req
Description With this call, the application logs off for receiving message services.
137
S7 Programming Interface C79000-G8976-C077-07
3.7.4 s7_get_msg_abort_cnf
Description With the 's7_receive' call, the user program receives the confirmation
S7_MSG_ABORT_CNF when the logoff is completed. The function
's7_get_msg_abort_cnf' described here must then be called to obtain
the result of an ALARM or SCAN logoff.
Parameters None
138
C79000-G8976-C077-07 S7 Programming Interface
3.7.5 s7_get_scan_ind
Description The 's7_get_scan_ind' call copies the net data sent by the SIMATIC S7
programmable controller to the specified memory areas. When a mes-
sage is sent by the remote partner, the user program receives the indi-
cation 'S7_SCAN_IND' using the 's7_receive()' call.
Acknowledgment of SCAN messages is not currently possible with
SAPI-S7.
139
S7 Programming Interface C79000-G8976-C077-07
Explanation of the Address of a buffer of a structure for the time stamp of the event pro-
'time_stamp_ptr' vided by the user program; the time from the SIMATIC S7 pro-
Parameter grammable controller is used.
struct S7_TIME_STAMP
{
ord16 year;
ord16 month;
ord16 day;
ord16 week_day;
ord16 hour;
ord16 minute;
ord16 second;
ord16 millisecond;
}
140
C79000-G8976-C077-07 S7 Programming Interface
Explanation of the Address of a buffer of an array provided by the user program with the
'scan_objects_ number of S7_MAX_SCAN_OBJECT elements. The number of rele-
array' Parameter vant elements is returned in the 'no_scan_objects_ptr' parameter.
struct S7_SCAN_OBJECT
{
ord16 state;
ord16 ack_state;
ord16 event_state;
ord32 event_id;
ord16 no_add_value;
struct
{
ord16 data_type;
ord16 add_value_len;
ord8 value
[S7_MAX_SCAN_ADD_VALUE_LEN+2];
} add_value[S7_MAX_ADD_VALUES];
}
141
S7 Programming Interface C79000-G8976-C077-07
event_state
Bit Description
0 Current state of the event
1 to 15 irrelevant
142
C79000-G8976-C077-07 S7 Programming Interface
143
S7 Programming Interface C79000-G8976-C077-07
Example of calling
the
's7_get_scan_ind'
function
void my_get_scan_ind(void)
{
int32 iRet; /* Return Value */
struct S7_SCAN_OBJECT
scan_objects[NO_SCAN_OBJ_PER_TG];
/* SCAN Objects, depends on TPDU size */
ord16 no_scan_objects = 0;
/* returns number of SCAN objects received */
/* NO_SCAN_OBJ_PER_TG depends on TPDU size */
struct S7_TIME_STAMP time_stamp; /* Time stamp */
int i,j,l,len; /* loop variables */
char TempBuffer[1024]; /* Buffer for output string */
char *TempBufferPtr;
iRet = s7_get_scan_ind
(
NULL, /* IN: od_ptr */
&no_scan_objects, /* OUT: Number of SCAN objects */
&time_stamp, /* OUT: Time stamp */
(struct S7_SCAN_OBJECT*) &scan_objects
); /* OUT: SCAN objects */
MYPRINTF("s7_get_scan_ind = %04x",iRet);
/* ERROR? */
if(iRet != S7_OK)
my_error_handler();
else
{
MYPRINTF(" Number of received scan objects : %d ",
no_scan_objects);
/* loop over all scan objects received */
for (i = 0; i < no_scan_objects ; i++ )
{
/* general info of scan object */
MYPRINTF(" %d. Scan object ",i );
MYPRINTF(" Ack state = %04x",scan_objects[i].ack_state);
MYPRINTF
(" event state = %04x",
scan_objects[i].event_state);
MYPRINTF(" event id = %04x",scan_objects[i].event_id);
144
C79000-G8976-C077-07 S7 Programming Interface
} /* End of my_get_scan_ind */
145
S7 Programming Interface C79000-G8976-C077-07
3.7.6 s7_get_alarm_ind
Description The 's7_get_alarm_ind' call copies the net data sent by the
SIMATIC S7 programmable controller to the specified memory areas.
When an alarm is sent by the remote partner, the user program recei-
ves the indication 'S7_ALARM_IND' using the 's7_receive()' call.
Acknowledgment of alarms is not currently possible with SAPI-S7.
146
C79000-G8976-C077-07 S7 Programming Interface
147
S7 Programming Interface C79000-G8976-C077-07
148
C79000-G8976-C077-07 S7 Programming Interface
Explanation of the Address of a buffer of a structure for the time stamp of the event pro-
'time_stamp_ptr' vided by the user program.
Parameter
struct S7_TIME_STAMP
{
ord16 year;
ord16 month;
ord16 day;
ord16 week_day;
ord16 hour;
ord16 minute;
ord16 second;
ord16 millisecond;
}
149
S7 Programming Interface C79000-G8976-C077-07
Explanation of the Pointer to an array of associated values with the following structure.
'add_value_ptr'
Allocate memory for the number of elements specified in the
Parameter 'S7_MAX_ADD_VALUE' constant.
The associated value is stored in the array 'value' with a length of
'S7_MAX_ALARM_ADD_VALUE_LEN+2'. The number of relevant
bytes in this array depends on the data type of the associated value
configured on the SIMATIC S7 programmable controller. It is specified
in the 'add_value_len' parameter.
struct S7_ADD_VALUE
{
ord16 data_type;
ord16 add_value_len;
ord8 value
[S7_MAX_ALARM_ADD_VALUE_LEN+2];
} add_value[S7_MAX_ADD_VALUES];
150
C79000-G8976-C077-07 S7 Programming Interface
Example of calling
the
's7_get_alarm_ind'
function
void my_get_alarm_ind(void)
{
iRet = s7_get_alarm_ind
(
NULL, /* IN: od_ptr */
&state, /* OUT: Status */
&ack_state, /* OUT: acknowledge state */
&event_state,
&event_id, /* OUT: event id */
&time_stamp, /* OUT: time stamp */
&no_add_value, /* OUT: number of associated values */
add_value /* OUT: start address of associated values */
);
/* ERROR? */
if(iRet != S7_OK)
my_error_handler();
else
{
/* general info of alarm */
MYPRINTF(" Ack state = %04x",ack_state);
MYPRINTF(" event state = %04x",event_state);
MYPRINTF(" event id = %04x",event_id);
151
S7 Programming Interface C79000-G8976-C077-07
152
C79000-G8976-C077-07 S7 Programming Interface
Description of the To extend the example from Section 3.5, after stopping the cyclic rea-
Example ding of variables, the status of the remote VFD is queried. The status
provides information about whether or not the remote communication
partner is ready for operation.
Example
:
:
/* additional prototypings */
static void my_get_vfd_state_cnf(ord32 cp_descr);
static void my_vfd_state_req(ord32 cp_descr,ord16 cref);
ret=s7_get_vfd_state_cnf( &log_state,
&phy_state,
local_detail);
if(ret!=S7_OK)
{
my_exit( cp_descr,
"Error s7_get_vfd_state_cnf",
ret);
}
}
ret=s7_vfd_state_req(
cp_descr, /* cp_descr */
cref,0 /* cref,orderid */);
if(ret!=S7_OK)
{
my_exit( cp_descr,
"Error s7_vfd_state_req",
ret);
}
}
153
S7 Programming Interface C79000-G8976-C077-07
do
{ ret=s7_receive(cp_descr,&cref,&orderid);
switch(ret)
{
:
:
case S7_CYCL_READ_DELETE_CNF:
my_get_cycl_read_delete_cnf(
cp_descr);
my_vfd_state_req(cp_descr,cref);
break;
case S7_VFD_STATE_CNF:
my_get_vfd_state_cnf(cp_descr);
my_abort(cp_descr,cref);
break;
default:
printf( "Event unexpected",
ret);
break;
}
} while( (ret!=last_event_expected)&&
(ret!=S7_ABORT_IND));
}
/* main */
void main(void)
{ ord32 cp_descr;
ord16 cref;
/* initialize s7 */
my_init(&cp_descr);
/* initiate connection */
my_initiate_req(cp_descr,cref);
/* end communication */
my_shut(cp_descr);
}
154
C79000-G8976-C077-07 S7 Programming Interface
Flowchart
= S7_OK
s7_receive()
= S7_NO_MSG
s7_receive()
= S7_VFD_STATE_CNF
Message (confirmation) there!
Fetch confirmation
s7_get_vfd_state_cnf
= S7_OK
155
S7 Programming Interface C79000-G8976-C077-07
3.8.1 s7_vfd_state_req
Description With the 's7_vfd_state_req()' call, a client application can read the
logical and physical status of another (remote) virtual field device
(VFD).
156
C79000-G8976-C077-07 S7 Programming Interface
3.8.2 s7_get_vfd_state_cnf
157
S7 Programming Interface C79000-G8976-C077-07
158
C79000-G8976-C077-07 S7 Programming Interface
Restrictions The diagnostic functions are only available with the S7-REDCONNECT
product.
The diagnostic services can only be used on connections that were
configured with STEP 7 to a station of the type SIMATIC PC station.
159
S7 Programming Interface C79000-G8976-C077-07
s7_diag_init()
= S7_OK
Windows message
Windows message
s7_receive()
Diagnostic info is there
= S7_DIAG_IND
= S7_OK
= S7_OK
160
C79000-G8976-C077-07 S7 Programming Interface
3.9.1 s7_diag_init
161
S7 Programming Interface C79000-G8976-C077-07
3.9.2 s7_get_diag_ind
Description Read the diagnostic information, when a message was received with
s7_receive. This function displays relevant diagnostic information only
for the required VFD (in STEP 7 known as the "application“) and
transfers it to a data area made available for this purpose.
162
C79000-G8976-C077-07 S7 Programming Interface
163
S7 Programming Interface C79000-G8976-C077-07
3.9.3 s7_diag_stop
164
C79000-G8976-C077-07 S7 Programming Interface
In this chapter, you will learn how to use the trace and to call the mini-DB. You will le-
arn the following:
➢ How to enable entries in the library’s own trace.
➢ How to check or modify settings in the mini-DB.
➢ How to obtain information about the last error that occurred.
When you have worked through this chapter, you will be in a position to
➢ Use all the functions provided by S7 for your application while retaining a simple
SAPI-S7 programming interface.
➢ Recognize and eliminate errors using your application.
165
S7 Programming Interface C79000-G8976-C077-07
4.1 s7_trace
Description With this call, the user can make entries in the trace of the S7 library.
This makes it possible to save important data for analysis, to check the
program sequence or to synchronize with the trace entries.
166
C79000-G8976-C077-07 S7 Programming Interface
4.2 s7_write_trace_buffer
(
char *filename /* In call */
)
Parameters filename Name of the file to which the internal ring buffer will
be written.
167
S7 Programming Interface C79000-G8976-C077-07
4.3 s7_mini_db_set
Description With this call, settings in the mini-DB are overwritten to be able to ad-
apt the establishment of an S7 connection, the trace and querying er-
rors in a wide range of differing situations. There is a limited number of
combinations of data and values as described below.
Parameters
type Identifier for the setting to be modified. The pos-
sible transfer values are described below.
168
C79000-G8976-C077-07 S7 Programming Interface
Combinations of The trace is a simple and yet effective aid to debugging for the S7
Values for the library. It can be adapted to an extremely wide range of applications.
Trace The permitted combinations of values are described below starting with
the 'type' parameter.
With SAPI-S7 libraries from Version V 1.371.2002 onwards, it is pos-
sible to distribute the trace output among several files. This prevents
the trace file growing too large. The number and size of the files can be
set with the parameters S7_MINI_DB_TRACE_MAXFILES and S7_
MINI_DB _TRACE_MAXLINES.
S7_MINI_DB_TRACE_FILENAME
This parameter value specifies the name of the trace file. The file na-
me is transferred as 'value' (default: '[Link]' in the current
working directory).
S7_MINI_DB_TRACE_TARGET
This value specifies the target for the trace.
169
S7 Programming Interface C79000-G8976-C077-07
S7_ MINI_DB_TRACE_MAXFILES
The trace is a cyclic buffer with a number of files specified in the
S7_MINI_DB_TRACE_MAXFILES parameter. Once a file has reached
the maximum length, a new file is created. When all files have re-
ached the maximum length, the oldest is overwritten.
Values between 1 and 999 can be set. The default value is 2.
S7_MINI_DB_TRACE_MAXLINES
This sets the size of the S7_Trace files.
2
Values between 1 and 2 -1 can be set. The size should be adapted to
the available memory.
The default value is 10.000.
S7_MINI_DB_TRACE_DEPTH
This sets the trace depth.
170
C79000-G8976-C077-07 S7 Programming Interface
S7_MINI_DB_TRACE_SELECT
To be able to activate the trace for specific service classes, a define is
available in the header file 'SAPI_S7.H' for each service class. The
defines can be combined as explained above.
S7_MINI_DB_TRACE_NO_LINES
This parameter is used to modify the number of lines in the internal
ring buffer. For applications that require a lot of memory, the ring buf-
fer can be reduced in size or increased in size to record the history
when errors occur. The function 's7_write_trace_buffer()' allows the
ring buffer to be written to a file.
171
S7 Programming Interface C79000-G8976-C077-07
S7_MINI_DB_INIT_REQ_AMQ_CALLING
This value specifies how many acknowledged jobs can be received at
the same time on the connection by the active partner (default: '3').
The value is a proposal that can be accepted or reduced by the partner
station. The negotiated value can be read out by the function
's7_mini_db_get()'.
S7_MINI_DB_INIT_REQ_AMQ_CALLED
This value specifies how many acknowledged jobs can be sent at the
same time on the connection by the active partner (default: '3'). The
value is a proposal that can be accepted or reduced by the partner
station. The negotiated value can be read out by the function
's7_mini_db_get()'.
S7_MINI_DB_INIT_REQ_PDU_SIZE
This value specifies the maximum size of a PDU on this connection for
the active partner (default: '0x100'). The value is a proposal that can
be accepted or reduced by the partner station. The negotiated value
can be read out by the function 's7_mini_db_get()'.
S7_MINI_DB_INIT_RSP_AMQ_CALLING
This value specifies how many acknowledged jobs can be sent at the
same time on the connection by the passive partner (default: '3'). The
lower of the values set for the active or passive side is negotiated.
S7_MINI_DB_INIT_RSP_AMQ_CALLED
This value specifies how many acknowledged jobs can be received at
the same time on the connection by the passive partner (default: '3').
The lower of the values set for the active or passive side is negotiated.
172
C79000-G8976-C077-07 S7 Programming Interface
S7_MINI_DB_INIT_RSP_PDU_SIZE
This value specifies the maximum size of a PDU on this connection for
the passive partner (default: '0x100'). The lower of the values set for
the active or passive side is negotiated.
S7_MINI_DB_PERSISTANCE_COUNT
This value sets the number of attempts at active connection
establishment (default: '5'). When the partner station has rejected the
establishment request this number of times, the establishment is ter-
minated and a negative acknowledgment sent to the user program.
This value can have a different meaning with certain SIMATIC NET
products. In this case, the meaning is explained in the relevant product
information.
S7_MINI_DB_ABORT_TIMEOUT
This value specifies how long a station can attempt to establish a con-
nection if the remote station does not respond. The value is set in mul-
tiples of 51 ms (default: '3000'). The parameter applies both to the
connection establishment and to the data transfer phase.
This value can have a different meaning with certain SIMATIC NET
products. In this case, the meaning is explained in the relevant product
information.
This value is irrelevant for TCP/IP protocols.
173
S7 Programming Interface C79000-G8976-C077-07
4.4 s7_mini_db_get
Description With this call, the settings are read out of the mini-DB. The user speci-
fies an identifier for the setting to be read and receives a string that
must be interpreted depending on the identifier.
Parameters type Identifier for the data to be read. The possible values
are described below.
Return Values for The mini-DB allows all the modifiable settings of the trace to be read at
Trace Settings any time. These are as follows:
S7_MINI_DB_TRACE_FILENAME
With this parameter, the name of the trace file is returned.
S7_MINI_DB_TRACE_TARGET
With this parameter, the target of the trace is output.
S7_MINI_DB_TRACE_DEPTH
With this parameter, the trace depth can be queried.
S7_MINI_DB_TRACE_SELECT
With this parameter, the service classes that were activated for the
trace are indicated.
The return values correspond to the specified values in the
's7_mini_db_set()' call.
174
C79000-G8976-C077-07 S7 Programming Interface
Return Values for After receiving an initiate confirmation, the performance parameters
Establishing an S7 negotiated between client and server are entered in the mini-DB by the
Connection corresponding processing function 's7_get_initiate_cnf()'.
S7_MINI_DB_INIT_IND_AMQ_CALLING
After receiving the initiate indication, this value informs the passive
partner how many jobs the active partner on the connection can recei-
ve at the same time.
S7_MINI_DB_INIT_IND_AMQ_CALLED
After receiving the initiate indication, this value informs the passive
partner how many jobs the active partner on the connection can send
at the same time.
S7_MINI_DB_INIT_IND_PDU_SIZE
After receiving the initiate indication, this value informs the passive
partner how much data the active partner can receive on this connecti-
on.
S7_MINI_DB_INIT_CNF_AMQ_CALLING
After active connection establishment, this value indicates the number
of acknowledged jobs that can be received at the same time on this
connection. The value was negotiated by the partners when the con-
nection was established.
S7_MINI_DB_INIT_CNF_AMQ_CALLED
After active connection establishment, this value indicates the number
of acknowledged jobs that can be sent at the same time on this con-
nection. The value was negotiated by the partners when the connection
was established.
S7_MINI_DB_INIT_CNF_PDU_SIZE
After active connection establishment, this value indicates the maxi-
mum PDU size on this connection. The value was negotiated by the
partners when the connection was established.
175
S7 Programming Interface C79000-G8976-C077-07
4.5 s7_last_iec_err_no
Description The error identifiers are reduced to a practical number by the S7 library
to allow error handling to be implemented more simply in applications.
According to IEC 1131 (International Electrotechnical Commission),
there are standard error codes that can be read out with this call.
Parameters None
S7_ERR_IEC_NO
No error occurred.
S7_ERR_IEC_DATA_TYPE_MISMATCH
The data types do not match.
S7_ERR_IEC_INVALID_REF
The specified S7 connection reference does not exist.
S7_ERR_IEC_LOWER_LAYER
An error occurred in the lower layers.
S7_ERR_IEC_NEG_RESPONSE
The client has received a negative response from the communications
partner.
S7_ERR_IEC_NO_ACCESS_TO_REM_OBJECT
Access to an object was rejected.
S7_ERR_IEC_PARTNER_IN_WRONG_STATE
The partner station is in a status in which the requested job cannot be
processed.
176
C79000-G8976-C077-07 S7 Programming Interface
S7_ERR_IEC_RECEIVER_DISABLED
The server is not responding.
S7_ERR_IEC_RECEIVER_OVERRUN
The resources in the server are exhausted.
S7_ERR_IEC_RESET_RECEIVED
A reset request has been received.
177
S7 Programming Interface C79000-G8976-C077-07
4.6 s7_last_iec_err_msg
Description This call returns a string that describes the error indicated by the IEC
error code. This is an error string that can, for example, be displayed
on an operator console or written to a log file etc.
Parameters None
178
C79000-G8976-C077-07 S7 Programming Interface
4.7 s7_last_detailed_err_no
Description With this call, the caller receives an error number that provides more
detailed information about the cause of the error than the standard IEC
error codes.
Parameters None
S7_ERR_NO_ERROR
No error occurred.
S7_ERR_CONN_ABORTED
The S7 connection was aborted.
S7_ERR_CONN_CNF
The S7 connection could not be established.
S7_ERR_CONN_NAME_NOT_FOUND
The specified S7 connection name was not found.
S7_ERR_FW_ERROR
A firmware error has occurred on the communications processor.
S7_ERR_INSTALL
When installing the SIMATIC NET driver or initializing the communica-
tions processor an error occurred that makes communication impos-
sible.
S7_ERR_INTERNAL_ERROR
During communication, library-internal data were overwritten making it
impossible to continue operating the application.
179
S7 Programming Interface C79000-G8976-C077-07
S7_ERR_INVALID_CONN_STATE
The job that has been sent is not permitted in the current status of the
S7 connection.
S7_ERR_INVALID_CREF
The specified S7 connection reference is invalid.
S7_ERR_INVALID_CYCL_READ_STATE
The job is not permitted in the current status of the cyclic read job.
S7_ERR_INVALID_DATARANGE_OR_TYPE
Input parameter of the called function outside the valid range of valu-
es.
S7_ERR_INVALID_DATA_SIZE
The data buffer provided by the user program is too small.
S7_ERR_INVALID_ORDERID
There is no job with the specified job identifier (parameter 'orderid').
S7_ERR_INVALID_PARAMETER
A transferred parameter or a specified value in a transferred structure
is invalid.
S7_ERR_MAX_REQ
The maximum number of acknowledged jobs negotiated during con-
nection establishment has already been sent.
S7_ERR_MINI_DB_TYPE
The 'type' parameter is not permitted in a mini-DB call.
S7_ERR_MINI_DB_VALUE
The 'value' parameter is not permitted in a mini-DB call.
180
C79000-G8976-C077-07 S7 Programming Interface
S7_ERR_NO_LICENCE
The license required for the product could not be found.
S7_ERR_NO_RESOURCE
The resources available are currently exhausted.
S7_ERR_NO_SIN_SERV
The SIMATIC NET server required for S7 applications under Windows
that sends messages to the relevant application could not be started.
S7_ERR_OBJ_ACCESS_DENIED
Access to the required object was rejected.
S7_ERR_OBJ_ATTR_INCONSISTENT
The OD or the attributes of the addressed object are inconsistent.
S7_ERR_OBJ_UNDEFINED
The object to be accessed does not exist.
S7_ERR_ORDERID_USED
The job identifier transferred with the call (parameter 'orderid') is alrea-
dy being used.
S7_ERR_RECEIVE_BUFFER_FULL
A message was received however the corresponding processing
function has not yet been called.
S7_ERR_SERVICE_NOT_SUPPORTED
The requested service is not supported.
S7_ERR_SERVICE_VFD_ALREADY_USED
The application or a different process has already logged on at the
VFD.
181
S7 Programming Interface C79000-G8976-C077-07
S7_ERR_SYMB_ADDRESS
The symbolic address transferred in the job is incorrect.
S7_ERR_SYMB_ADDRESS_INCONSISTENT
The size of the user data contained in the symbolic address and the
size of the user buffer are contradictory.
S7_ERR_TOO_LONG_DATA
There are more data to be written than permitted by the standard.
S7_ERR_UNKNOWN_ERROR
An unknown error has occurred.
S7_ERR_WRONG_CP_DESCR
The CP descriptor in the call is incorrect.
S7_ERR_WRONG_IND_CNF
The wrong processing function was called for a received message.
182
C79000-G8976-C077-07 S7 Programming Interface
4.8 s7_last_detailed_err_msg
Description This call provides an error message about the detailed error number
that describes the error that has occurred and provides information
about eliminating the error. This is an error string that can, for examp-
le, be displayed on an operator console or written to a log file etc.
Parameters None
183
S7 Programming Interface C79000-G8976-C077-07
4.9 s7_discard_msg
Description With this call, the user program can discard a received message
without having called the corresponding processing function.
Per S7 connection, there is a maximum number of acknowledged jobs
that can be processed simultaneously, this maximum number is speci-
fied during configuration and is negotiated when the connection is
established. Ignoring (for example unexpected) events and the conse-
quent absence of responses therefore permanently reduces the num-
ber of acknowledged jobs that can be used at the same time.
Parameters None
184
C79000-G8976-C077-07 S7 Programming Interface
5 Configuration
In this chapter
➢ you will learn the meaning of configuration,
➢ you will obtain an overview of the configuration parameters necessary for opera-
tion,
➢ you will find a list of the services that use configuration parameters.
When you have worked through this chapter, you will be in a position to match your
SAPI-S7 application to the configuration.
185
S7 Programming Interface C79000-G8976-C077-07
186
C79000-G8976-C077-07 S7 Programming Interface
Administrative The logon function 's7_init()' requires two items of information from the
Services administrative services:
➢ Firstly, the CP name that identifies a CP must be specified and
must match the name selected during installation. The installati-
on is performed with SIMATIC NET products. The installation
procedures are described in the documentation accompanying
these products.
➢ Secondly, the name of the local VFD at which the user wants to
log on is required. The logon makes the VFD-specific S7 con-
nections and the VFD-specific objects accessible to the applica-
tion. The VFD name is specified during configuration.
The names of the installed CPs or the names of VFDs configured on a
CP that were assigned connetions can be queried with the
's7_get_device()' or 's7_get_vfd()' calls.
Management Ser- Only the 's7_get_cref()' call requires a configuration parameter from
vices for S7 Con- the management services for S7 connection lists in the form of the S7
nection Lists connection name. This function returns the configured reference for an
S7 connection. Once this reference has been obtained, the application
should continue to use the reference in future communication.
The names of all configured S7 connections can be read out with the
's7_get_conn()' call.
187
S7 Programming Interface C79000-G8976-C077-07
188
C79000-G8976-C077-07 S7 Programming Interface
This chapter explains the characteristics of the SAPI-S7 programming interface specific
to MS-DOS and Windows. In this context Windows stands for the Microsoft operating
systems Windows 3.x (3.1 and 3.11) in enhanced 386 mode, Windows 95 and Win-
dows NT if not specified otherwise.
When you have worked through this chapter, you will be in a position to
➢ translate your own program modules and to link them with the S7 library to form
an executable program,
➢ control the trace outputs of your application.
189
S7 Programming Interface C79000-G8976-C077-07
Memory Models The SAPI-S7 programming interface is available to the user in the form
of libraries. The definitions required to use the programming interface
are located in the 'SAPI_S7.H' file. Libraries are available for MS-DOS,
Windows 3.x, Windows 95 and Windows NT for various compilers.
The names of the libraries for MS-DOS and Windows 3.x are as fol-
lows:
<Memorymodel><Operatingsystem>s7<Compiler>.lib
For example, the file '[Link]' is the library translated with the
Turbo C compiler in the 'Large' memory model for the MS-DOS opera-
ting system.
For the Windows 3.x operating system, the '[Link]' file provides a
DLL version (Dynamic Link Library) with the import libraries
'[Link]' and '[Link]' for the Microsoft C- or Borland C compi-
lers.
For Windows 95 and Windows NT, a 32-bit DLL '[Link]' and the
corresponding import library '[Link]' are available.
190
C79000-G8976-C077-07 S7 Programming Interface
Program Abort To allow communication, user programs must log on at the communi-
cations system that occupies resources for management. If an applica-
tion is aborted with the key combination 'CTRL+C' the resources still
remain reserved for the process and the logon is still effective. To
avoid this, a 'CTRL+C' handler should be implemented in the user pro-
gram to handle all the logoffs at the communications system if the
program is aborted.
191
S7 Programming Interface C79000-G8976-C077-07
Requirements The following sections list compilation examples that illustrate the re-
quired compiler and linker options for your applications.
You must adapt the compile instruction to suit the situation on your
system selecting the correct drive and path.
Working with the Under MS DOS, the S7 library for the MSC Compiler 7.0 has the name
MSC Compiler 7.0 '[Link]'. The following example shows how a sample program
'EXP.C' is compiled and linked with the 'Large' memory model for MS
DOS:
[Link],
[Link],
[Link],
[Link]+
\msc70\lib\[Link]+
\msc70\lib\[Link]
;
192
C79000-G8976-C077-07 S7 Programming Interface
Working with the If you want to use the MS Visual C++ Compiler 1.0 for your MS-DOS
MS Visual C++ applications, you can use the same S7 library as for the MSC Compiler
Compiler 1.0 7.0. The compile instructions for the 'Large' memory model then ap-
pear as follows under MS-DOS:
[Link],
[Link],
[Link],
[Link]+
\msvc\lib\[Link]+
\msvc\lib\[Link]
;
Working With the For the Turbo C Compiler 1.0, there are two versions of the S7 library
Turbo C Compi- available for MS-DOS: '[Link]' for the 'Large' memory model
ler 1.0 and '[Link]' for the 'Huge' memory model. The following examp-
le shows how a typical program 'EXP.C' is translated and linked with
the 'Large' memory model for MS-DOS:
\tc10\lib\[Link] [Link]
[Link]
[Link]
\tc10\lib\[Link] \tc10\lib\[Link] \tc10\lib\[Link] [Link]
193
S7 Programming Interface C79000-G8976-C077-07
Requirements The following sections list compilation examples that illustrate the re-
quired compiler and linker options for your applications.
You must adapt the compile instruction to suit the situation on your
system selecting the correct drive and path.
Working with the Under Windows 3.x, the S7 library for the MSC Compiler 7.0 has the
MSC Compiler 7.0 name '[Link]'. The following example illustrates how a sample
program 'EXP.C' is translated and linked with the 'Large' memory mo-
del for Windows 3.x:
[Link],
[Link],
[Link],
[Link]+
\msc70\lib\[Link]+
\msc70\lib\[Link]+
\msc70\lib\[Link]
[Link];
NAME EXP
EXETYPE WINDOWS
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE MULTIPLE
HEAPSIZE 1024
STACKSIZE 10240
EXPORTS
194
C79000-G8976-C077-07 S7 Programming Interface
If you want to use the MS Visual C++ Compiler 1.0 for your Windows
Working with the
applications, you can use the same S7 library as for the MSC Compiler
MS Visual C++ 7.0. The compile instructions for the 'Large' memory model then ap-
Compiler 1.0 pear as follows under MS-DOS:
[Link],
[Link],
[Link],
[Link]+
\msvc\lib\[Link]+
\msvc\lib\[Link]+
\msvc\lib\[Link]
[Link];
NAME EXP
EXETYPE WINDOWS
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE MULTIPLE
HEAPSIZE 1024
STACKSIZE 10240
EXPORTS
195
S7 Programming Interface C79000-G8976-C077-07
Working with the The S7 library for the Borland C Compiler 3.1 has the name
Borland C Compi- '[Link]' under Windows 3.x . The following example shows
ler 3.1 how a typical program 'EXP.C' is translated and linked with the 'Large'
memory model for Windows:
\bc31\lib\[Link] [Link]
[Link]
[Link]
[Link] \bc31\lib\[Link] \bc31\lib\[Link]
\bc31\lib\[Link]
[Link]
NAME EXP
EXETYPE WINDOWS
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE MULTIPLE
HEAPSIZE 1024
STACKSIZE 10240
EXPORTS
SAPI-S7 as DLL Under the Windows 3.x operating system, in addition to the libraries
Version listed above, there is a DLL version (Dynamic Link Library) available
(file '[Link]') and the required import libraries for the MSC Compiler
7.0 and MS Visual C++ Compiler 1.0 (file '[Link]') or the Borland
C Compiler 3.1 (file '[Link]').
The compilation rules for SAPI-S7 applications that are based on the
DLL version of SAPI-S7 are similar to those for applications that use
the SAPI-S7 libraries. The SAPI-S7 libraries above '[Link]'
and '[Link]' must be replaced by the import libraries
'[Link]' and '[Link]. In addition to this, the define 'S7_DLL'
must be set when compiling source files that use the SAPI-S7 functi-
ons.
For using this DLL in other programming languages, such as BASIC or
Pascal, refer to the corresponding manuals.
196
C79000-G8976-C077-07 S7 Programming Interface
Requirements The following sections list compilation examples that illustrate the re-
quired compiler and linker options for your applications.
You must adapt the compile instruction to suit the situation on your
system selecting the correct drive and path.
Working with the The S7 import library for the Microsoft Visual C++ compiler 2.2 has the
MSVC Compiler 2.2 name '[Link]' under Windows 95 and Windows NT. The
corresponding DLL has the name '[Link]'. The following Example
shows how a sample program 'EXP.C' is compiled and linked for
Windows 95. For Windows NT, simply replace the directory
'SAPI_S7.W95' with 'SAPI_S7.NT'.
[Link],
\sinec\sapi_s7.w95\lib\[Link]
\msvc20\lib\[Link]
\msvc20\lib\[Link]
\msvc20\lib\[Link]
\msvc20\lib\[Link]
/SUBSYSTEM:windows /MACHINE:I386
197
S7 Programming Interface C79000-G8976-C077-07
What Does Envi- The term environment means a memory area in which the parameters
ronment Mean? are saved that are set with the MS-DOS commands 'path', 'prompt' and
'set'. The area consists of a series of ASCII strings that are completed
by a NULL character. The area is used to hold information about the
entire computer system.
Environment Va- The values of the environment variables required for the SAPI-S7
riables of the library must be set using the configuration program "Setting the
SAPI-S7 Library PG/PC Interface“. This program sets the values under the key
under Windows 95 "HKEY_LOCAL_MACHINE\SOFTWARE\SIEMENS\SINEC\SAPI_S7“
and Windows NT in the Windows 95/NT registry. The variable is only searched for in the
program environment when no entry with the required name exists in
the registry.
Controlling the The S7 library can be controlled by a total of three environment varia-
Trace bles. Using the variables 'S7_TRACE_SELECT', 'S7_TRACE_DEPTH'
and 'S7_TRACE_TARGET' the service classes for which entries will be
made in the trace, the trace depth and the target of the trace can be
set (see file 'SAPI_S7.H').
Example: set S7_TRACE_DEPTH=104
The existence of the environment variable is checked and evaluated
when the trace is initialized. Trace settings made earlier are then over-
written.
It is advisable to set the trace using the environment variables and not
with mini-DB calls. This allows the default values to be overwritten for
debugging without the user having to modify his application and
retranslate it.
Under Windows 95 and Windows NT, you can also specify the value
of the variables by making entries with the names of the environment
variables in the registration database under the key
“HKEY_LOCAL_MACHINE\SOFTWARE\SIEMENS\SINEC\SAPI_S7”.
198
C79000-G8976-C077-07 S7 Programming Interface
How to Access the In the configuration program "Setting the PG/PC Interface“, you can
Configuration set the drive name, the and name of the configuration file.
Step Procedure
1 Select a module parameter set from the "Module Parame-
ter Set Used" list box.
2 Click the "Properties" button.
3 Select the "S7 Protocol" tab.
4 Check the "Activate S7" option box.
5 In the "SAPI S7 Database" input field, enter the drive na-
me, the path, and the name of the configuration file.
Under MS-DOS and Windows 3.x, the SAPI-S7 library attempts to read
the path of the configuration file from an environment variable. The
name of the environment variable corresponds to the entry in the regi-
stry under Windows 95 and Windows NT. For a CP with the name
'CP_L2_1:' this would be 'CP_L2_1:_S7LDB'.
If no corresponding environment variable is found either when the
's7_init()' function is called, the SAPI S7 library attempts to read out
the configuration data from a file in the currently active directory. |The
file name is obtained from the transfer parameter 'cp_name' by remo-
ving the colon completing the CP name and appending the extension
'.LDB'. If a CP with the name CP 'CP_L2_1:' logs on, for example, the
file 'CP_L2_1.LDB' is read out.
199
S7 Programming Interface C79000-G8976-C077-07
General It is possible to make a setting for a specific operating system for the
trace of the SAPI-S7 programming interface. Using a mini-DB call, the
target of the trace can be modified to the value
'S7_TRACE_TARGET_CONSOLE'. The way in which the trace then
reacts depends on the specific operating system and is explained be-
low.
The Trace Setting Under Windows, the trace setting described above means that the
for Windows entire trace is output on the monitor in its own window. You can then
adapt the window to suit your needs. How you configure the window
and the number of trace lines displayed can be found in the on-line
help texts.
The Trace Setting With the trace setting above, no trace entries whatsoever are made
for MS-DOS under the MS-DOS operating system. As a single-user operating sy-
stem, MS-DOS is not in a position to start a second application parallel
to the S7 user program that edits and represents the trace on the moni-
tor.
200
C79000-G8976-C077-07 S7 Programming Interface
Differences Betwe- One of the differences between Windows applications and MS-DOS
en MS-DOS and programs is that they receive events at a central point in the main pro-
Windows Pro- gram and pass them on to a suitable Windows procedure ('WndProc')
grams for further processing. This Windows procedure must be made acces-
sible to Windows management when the program is started.
Example of a
Typical Windows :
Application :
#define MY_MSG_ID 1500
WndProc(hWnd,msg,...)
{
:
:
switch(msg)
{
case ....: /* init code */
s7_init("CP_L2_1:","MY_VFD",&cp_descr);
s7_set_window_handle_msg(
cp_descr,hWnd,MY_MSG_ID);
break;
case ....:
s7_....(cp_descr,...);
break;
case MY_MSG_ID:
s7_receive(cp_descr,&cref,
&orderid);
break;
}
}
:
:
201
S7 Programming Interface C79000-G8976-C077-07
6.7.1 s7_set_window_handle_msg
Description In a Windows program, after a successful 's7_init()', the user must call
the routine 's7_set_window_handle_msg()'. This informs the underlying
communication system to which window and with which ID it should
send its messages.
Parameters
cp_descr Handle as return value of the 's7_init()' call.
202
C79000-G8976-C077-07 S7 Programming Interface
7 Appendix
At the end of the chapter you will find a list of the most common abbreviations used in
this manual and a list of documentation available for further reading.
203
S7 Programming Interface C79000-G8976-C077-07
SAPI-S7 as a Sub- The SAPI-S7 programming interface provides access to the following
set of S7 services (abbreviation: 'req' for Request, 'ind' for Indication, 'con' for
Confirmation and 'rsp' for Response):
PICS Part 2
Supported Services
Service Primitive
Initiate req, con, ind, rsp
Abort req, ind
Status req, con
Unsolicited-Status ind
Read req, con
Write req, con
204
C79000-G8976-C077-07 S7 Programming Interface
PICS Part 3
S7 Parameters and Options Detail
Addressing by names YES
Maximum length for names 32
Access-Protection Supported -
Maximum length for Extension 32
Maximum length for Extension 0
Arguments
PICS Part 4
Local Implementation Values Detail
Maximum length of S7-PDU 1024
Maximum number of Services -
Outstanding Calling
Maximum number of Services -
Outstanding Called
Syntax and semantics of the Exe- -
cution Argument
Syntax and semantics of Exten- -
sion
Conditions for When operating the SAPI-S7 programming interface, remember the
Operation following restrictions:
205
S7 Programming Interface C79000-G8976-C077-07
Data Consistency, For information about data consistency, user data size and cyclic rea-
User Data Size, ding refer to the SIMATIC communication manual (order number
Cyclic Reading 6ES7398-8EA00-8BA0).
206
C79000-G8976-C077-07 S7 Programming Interface
The following table shows the maximum length of the result/user data
depending on the PDU size as a formula.
In services with several variables, the maximum number of variables is
also shown dependent on the PDU size as a formula.
207
S7 Programming Interface C79000-G8976-C077-07
208
C79000-G8976-C077-07 S7 Programming Interface
Byte Alignment The SAPI-S7 programming interface requires that variables are saved
byte aligned in main memory. This means that there must be no pad-
ding bytes, for example between the individual components of a
structure. This is achieved with suitable compiler options or by prag-
mas.
Network Represen- The SAPI-S7 library supplies data that have been read or variable
tation of Variable values to be written in the network representation. A conversion bet-
Values ween the host and network representation is intended for future versi-
ons of the library. For this reason, all the affected functions have been
extended by a transfer parameter 'od_ptr' that must be assigned the
NULL pointer in the current version of the library.
This parameter ('od_ptr') will control the representation of variables in a
later version of the library. The representation depends on the follo-
wing:
➢ Whether or not data should be converted from the host to the
network representation and vice versa (currently not implemen-
ted in the SAPI-S7 library), or whether the network representati-
on is required on the host.
➢ Which host CPU is being used (for example Intel).
209
S7 Programming Interface C79000-G8976-C077-07
Network Represen-
tation and Range
Bit MSB LSB
of Values
Octet 8 7 6 5 4 3 2 1
1 0 0 0 0 0 0 0 x
For 'FALSE', 'x' has the value '0', for 'TRUE' the value '1'.
Host Representati-
on and Range of
Bit MSB LSB
Values (Intel CPU)
Octet 8 7 6 5 4 3 2 1
1 x8 x7 x6 x5 x4 x3 x2 x1
If at least 1 bit of the bits 'x8' to 'x1' are set, the value 'TRUE' is assu-
med. For 'FALSE', all bits must be '0’
210
C79000-G8976-C077-07 S7 Programming Interface
Range of Values With the 'integer' data type, a distinction must be made according to
the length of the data type.
Network Represen- Network representation (2’s complement representation) for 8-bit inte-
tation gers ('SI' represents the sign bit and has the value '1' for negative
numbers, otherwise the value '0'):
211
S7 Programming Interface C79000-G8976-C077-07
Host Representati- Host representation (2’s complement representation) for 8-bit integers
on (Intel CPU) ('SI' represents the sign bit and has the value '1' for negative numbers,
otherwise the value '0'):
212
C79000-G8976-C077-07 S7 Programming Interface
213
S7 Programming Interface C79000-G8976-C077-07
Range of Values With the 'unsigned' data type, a distinction must be made according to
the length of the data type.
214
C79000-G8976-C077-07 S7 Programming Interface
215
S7 Programming Interface C79000-G8976-C077-07
Network Represen- Network representation ('SI' is the sign of the mantissa, the shaded
tation fields belong to the exponent, the remaining fields to the mantissa).
Host Representati- Host representation ('SI' is the sign of the mantissa, the shaded fields
on (Intel CPU) belong to the exponent, the remaining fields to the mantissa).
216
C79000-G8976-C077-07 S7 Programming Interface
217
S7 Programming Interface C79000-G8976-C077-07
Range of Values For variables of the type 'visible string', all letters (upper and lower
case), numbers, the underscore ('_') and the Dollar sign ('$') are permit-
ted.
Network Represen-
Bit MSB LSB
tation
Octet 8 7 6 5 4 3 2 1
1 1st character
2 2nd character
..
n nth character
Host Representati-
on (Intel CPU) Bit MSB LSB
Octet 8 7 6 5 4 3 2 1
1 1st character
2 2nd character
..
n nth character
n+1 NULL terminator
218
C79000-G8976-C077-07 S7 Programming Interface
Network Represen- A variable of the type 'octet string' is represented on the network like a
tation 'visible string'. In this case, however, all byte values are permitted.
Host Representati-
on (Intel CPU) Bit MSB LSB
Octet 8 7 6 5 4 3 2 1
1 Length information
2 1st character
..
n (n-1)th character
n+1 nth character
219
S7 Programming Interface C79000-G8976-C077-07
Network Represen- A variable of the type 'bit string' is represented on the network as fol-
tation lows:
Host Representati-
on (Intel CPU) Bit MSB LSB
Octet 8 7 6 5 4 3 2 1
1 Length information
2 7 6 5 4 3 2 1 0
3 15 14 13 12 11 10 9 8
..
220
C79000-G8976-C077-07 S7 Programming Interface
Glossary
DB Data block
Multi-CP operation More than one CP can be operated at one time in a programming de-
vice/PC.
PG Programming device.
221
S7 Programming Interface C79000-G8976-C077-07
PROFIBUS Process Field Bus - Network for the cell and field area of the mid per-
formance range with its main application in an industrial environment
complying with EN 50 170, Volume 2. PROFIBUS.
222
B89077/07 S7 Programming Interface
Index
223
S7 Programming Interface B89077/07
224