Sharp7 Refman
Sharp7 Refman
Reference manual
Davide Nardella
Rev.1 – October 7, 2016
Page |2
Sharp7 1.0.0 - Reference manual
Summary
Summary ............................................................................................................... 2
Project overview ...................................................................................................... 4
Main features .......................................................................................................... 4
Licensing ................................................................................................................ 5
Disclaimer of Warranty ............................................................................................. 5
About this manual ................................................................................................... 6
Siemens S7 Protocol ................................................................................................ 7
Compatibility........................................................................................................... 9
S7 300/400/WinAC ................................................................................................ 10
S7 1200/1500 ....................................................................................................... 11
LOGO! 0BA7/0BA8 ................................................................................................. 13
S7 200 (via CP243)................................................................................................ 17
Sharp7 deploy ....................................................................................................... 19
S7Client reference ................................................................................................. 20
Administrative functions ......................................................................................... 20
SetConnectionType ............................................................................................ 21
ConnectTo ........................................................................................................ 22
SetConnectionParams ........................................................................................ 24
Connect ............................................................................................................ 25
Disconnect ........................................................................................................ 26
Base Data I/O functions ......................................................................................... 27
ReadArea .......................................................................................................... 28
WriteArea ......................................................................................................... 30
ReadMultiVars ................................................................................................... 31
WriteMultiVars ................................................................................................... 32
Lean Data I/O functions ......................................................................................... 33
Block oriented functions ......................................................................................... 34
GetAgBlockInfo ................................................................................................. 35
DBGet .............................................................................................................. 37
DBFill .............................................................................................................. 38
Date/Time functions ............................................................................................... 39
GetPlcDateTime ................................................................................................. 40
SetPlcDateTime ................................................................................................. 41
SetPlcSystemDateTime ....................................................................................... 42
System info functions ............................................................................................. 43
ReadSZL ........................................................................................................... 44
Page|2
Sharp7 1.0.0 - Reference manual
Page |3
Sharp7 1.0.0 - Reference manual
GetOrderCode ................................................................................................... 45
GetCpuInfo ....................................................................................................... 46
GetCpInfo ......................................................................................................... 47
PLC control functions ............................................................................................. 48
PlcColdStart ...................................................................................................... 49
PlcHotStart ....................................................................................................... 50
PlcStop ............................................................................................................. 51
PlcGetStatus ..................................................................................................... 52
Security functions .................................................................................................. 53
SetSessionPassword ........................................................................................... 54
ClearSessionPassword ........................................................................................ 55
GetProtection .................................................................................................... 56
Properties/Info Functions ........................................................................................ 57
ConnTimeout..................................................................................................... 57
RecvTimeout ..................................................................................................... 57
SendTimeout ..................................................................................................... 57
Connected ........................................................................................................ 57
PduSizeNegotiated ............................................................................................. 57
PduSizeRequested ............................................................................................. 57
PLCPort ............................................................................................................ 57
ExecutionTime ................................................................................................... 57
S7 Helper reference ............................................................................................... 58
Data access example ............................................................................................. 59
S7MultiVar reference.............................................................................................. 61
Error codes ........................................................................................................... 62
Page|3
Sharp7 1.0.0 - Reference manual
Page |4
Sharp7 1.0.0 - Reference manual
Project overview
Sharp7 is the C# port of Snap7 Client. It’s not a wrapper, i.e. you don’t have an
interface code that loads [Link] (or .so) but it’s a pure C# implementation of the
S7Protocol.
Sharp7 is deployed as a single source file that contains some classes that you can use
directly in your .NET project to communicate with S7 PLCs.
It’s designed to work with small hardware .NET-based or even for large projects which
don’t needs of extended control functions.
Main features
Page|4
Sharp7 1.0.0 - Reference manual
Page |5
Sharp7 1.0.0 - Reference manual
Licensing
Sharp7 is distributed as a source code library under GNU Library or Lesser General
Public License version 3.0 (LGPLv3).
Basically this means that you can distribute your commercial firmware containing
Sharp7 without the requirement to distribute the source code of your application and
without the requirement that your firmware be itself distributed under LGPL. A small
mention is however appreciated if you include it in your applications.
Disclaimer of Warranty
IF ANYONE BELIEVES THAT, WITH SHARP7 PROJECT HAVE BEEN VIOLATED SOME
COPYRIGHTS, PLEASE EMAIL US, AND ALL THE NECESSARY CHANGES WILL BE MADE.
Page|5
Sharp7 1.0.0 - Reference manual
Page |6
Sharp7 1.0.0 - Reference manual
This manual describes only the Sharp7 library, what it consists of and how to use it.
For a more detailed information about PLC interfacing and S7 Protocol please refer to
the Snap7 reference manual.
Page|6
Sharp7 1.0.0 - Reference manual
Page |7
Sharp7 1.0.0 - Reference manual
Siemens S7 Protocol
If you already know the Siemens Ethernet communication you can skip this chapter.
Each block is named PDU (Protocol Data Unit), its maximum length depends on the
CP and is negotiated during the connection.
The first two elements are always present, the other are optional.
To understand:
Write, DB, 10, 4 and data are the components of the command and are formatted in a
message in accord to the protocol specifications.
S7 Protocol, ISO TCP and TCP/IP follow the well-known encapsulation rule : every
telegram is the "payload" part of the underlying protocol.
Protocols Encapsulation
Page|7
Sharp7 1.0.0 - Reference manual
Page |8
Sharp7 1.0.0 - Reference manual
Data Read/Write
Cyclic Data Read/Write
Directory info
System Info
Blocks move
PLC Control
Date and Time
Security
Programming
Siemens provides a lot of FB/FC (PLC side), Simatic NET software (PC side) and a
huge excellent documentation about their use, but no internal protocol specifications.
PDU independence
As said, every data packet exchanged with a PLC must fit in a PDU, whose size is fixed
and varies from 240 up to 960 bytes.
All Sharp7 functions completely hide this concept, the data that you can
transfer in a single call depends only on the size of the available memory.
If this data size exceeds the PDU size, the packet is automatically split across more
subsequent transfers.
Page|8
Sharp7 1.0.0 - Reference manual
Page |9
Sharp7 1.0.0 - Reference manual
Compatibility
Page|9
Sharp7 1.0.0 - Reference manual
P a g e | 10
Sharp7 1.0.0 - Reference manual
S7 300/400/WinAC
No special consideration has to be made about these CPU, Sharp7 has full access to
these PLC either directly (with the integrated interface 3xx-PN or 4xx-PN) or via the
CPX43 interface.
Connection
Use ConnectTo() specifying IP_Address, Rack, Slot for the first connection, this
functions set the internal parameters and connects to the PLC. if a TCP error occurred
and a disconnection was needed, for reconnecting you can simply use Connect()
which doesn't requires any parameters. Look at the reference of ConnectTo() for a
detailed explanation of Rack and Slot.
It’s possible but it’s not mandatory to specify the connection type via the function
SetConnectionType() which must be called before ConnectTo(). By default the client
connects as a PG (the programming console), with this function is possible change the
connection resource type to OP (the Siemens HMI panel) or S7 Basic (a generic data
transfer connection).
In the hardware configuration (Simatic Manager) of the CPU, under “Communication”
tab, you can change, PLC-side, the connection's distribution, if you need.
PG, OP and S7 Basic communications are client-server connections, i.e. they don’t
require that the PLC have a connection designed by NetPro.
Note : This is an optimization function, if the client doesn’t connect, the problem is
elsewhere.
RemoteTSAP=(ConnectionType<<8)+(Rack*0x20)+Slot;
P a g e | 10
Sharp7 1.0.0 - Reference manual
P a g e | 11
Sharp7 1.0.0 - Reference manual
S7 1200/1500
An external equipment can access to S71200/1500 CPU using the S7 “base” protocol,
only working as an HMI, i.e. only basic data transfer are allowed.
Connection
To connect with these PLC use ConnectTo() just like the other “S7” CPUs.
The only difference is that Rack and Slot are fixed (Rack=0, Slot=0).
Data Access
DB property
Select the DB in the left pane under “Program blocks” and press Alt-Enter (or in the
contextual menu select “Properties…”)
P a g e | 11
Sharp7 1.0.0 - Reference manual
P a g e | 12
Sharp7 1.0.0 - Reference manual
Protection
Select the CPU project in the left pane and press Alt-Enter (or in the contextual menu
select “Properties…”)
In the item Protection, select “Full access” and Check “Permit access with PUT/GET ….”
as in figure.
P a g e | 12
Sharp7 1.0.0 - Reference manual
P a g e | 13
Sharp7 1.0.0 - Reference manual
LOGO! 0BA7/0BA8
LOGO is a small Siemens PLC suited for implementing simple automation tasks in
industry and building management systems.
It’s very user friendly and the last model is equipped with an Ethernet port for both
programming and data exchange.
Communication
Due to its architecture, the LOGO communication is different from its Siemens cousins.
It implements two Ethernet protocols, the first that we can call PG protocol, is used
by the software LOGO Comfort (the developing tool) to perform system tasks such as
program upload/download, run/stop and configuration.
The second, used for data exchange, is the well-known (from the Sharp7 point of
view) S7 Protocol.
They are very different, and the first is not covered by Snap7 and Sharp7 because is a
stand-alone protocol that is not present, Is far I know, in different contexts.
To communicate with LOGO, the Ethernet connections must be designed with LOGO
Comfort in advance.
Of course I will show you how.
LOGO must be set as MASTER (i.e. NORMAL mode as LOGO Comfort says).
I assume that your LOGO Comfort is already set and connected to the LOGO.
P a g e | 13
Sharp7 1.0.0 - Reference manual
P a g e | 14
Sharp7 1.0.0 - Reference manual
Connection configuration
Configuring a server connection allows you to connect Sharp7 Client with LOGO for
reading and writing the memory just like an HMI panel would do.
Double-click the new connection created and edit its parameters selecting
Server Connection.
Note:
P a g e | 14
Sharp7 1.0.0 - Reference manual
P a g e | 15
Sharp7 1.0.0 - Reference manual
You can chose for Remote TSAP the same value of the Local TSAP, in the example I
used two different values to remark (as you will see) the crossing parameters.
Confirm the dialog, close the connection editor and download the
configuration into the LOGO.
The LOGO is ready, to test it run the ReadDemo, insert the LOGO IP Address
and modify the connection routine as in figure.
IPAddress Peer(192,168,0,73);
[Link](Peer, 0x0300, 0x0200);
[Link]();
Notice that the Local TSAP of the Client corresponds to the Remote TSAP of the LOGO
and vice-versa. This is the key concept for the S7 connections.
The LOGO memory that we can Read/Write is the V area that is seen by all HMI (and
Snap7 too) as DB 1.
Into it are mapped all LOGO resources organized by bit, byte or word.
There are several tutorials in the Siemens site that show how to connect an HMI (via
WinCC flexible or TIA) to the LOGO and the detailed map.
Please refer to them for further information.
P a g e | 15
Sharp7 1.0.0 - Reference manual
P a g e | 16
Sharp7 1.0.0 - Reference manual
Finally this is the connections editor of LOGO!Soft Comfort 8 (different window but
absolutely the same concept)
P a g e | 16
Sharp7 1.0.0 - Reference manual
P a g e | 17
Sharp7 1.0.0 - Reference manual
S7200 was out of the scope for Snap7 and Sharp7 because beginning November 2013
the S7-200 product family entered into the Phase Out stage of its product life cycle,
but after working with LOGO also this PLC can be accessed since it uses the same
connection mechanism.
As said, the connection is very similar to that of LOGO, you need to design a
connection using the Ethernet wizard of MicroWin as in figure.
or
P a g e | 17
Sharp7 1.0.0 - Reference manual
P a g e | 18
Sharp7 1.0.0 - Reference manual
In the first case the PLC expects to be connected to an OP and you must supply
LocalTSAP = 0x1000 and RemoteTSAP = 0x0200 to the SetConnectionParams
function.
If you make a S7200 HMI project, the runtime of WinCC itself uses these parameters.
In the second case you should use LocalTSAP = 0x1000 and RemoteTSAP = 0x1000.
P a g e | 18
Sharp7 1.0.0 - Reference manual
P a g e | 19
Sharp7 1.0.0 - Reference manual
Sharp7 deploy
For VB users refer to the example, you need to create the assembly [Link]
P a g e | 19
Sharp7 1.0.0 - Reference manual
P a g e | 20
Sharp7 1.0.0 - Reference manual
S7Client reference
Administrative functions
Function Purpose
ConnectTo Connects a Client Object to a PLC.
SetConnectionType Sets the connection type (PG/OP/S7Basic)
SetConnectionParams Sets Address, Local and Remote TSAP for the connection.
Connect Connects a Client Object to a PLC with implicit parameters.
Disconnect Disconnects a Client.
P a g e | 20
Sharp7 1.0.0 - Reference manual
P a g e | 21
Sharp7 1.0.0 - Reference manual
SetConnectionType
Description
Sets the connection resource type, i.e. the way in which the Clients connects to a PLC.
Declaration
Parameters
Type Dir.
ConnectionType short In See the table
P a g e | 21
Sharp7 1.0.0 - Reference manual
P a g e | 22
Sharp7 1.0.0 - Reference manual
ConnectTo
Description
Declaration
Parameters
Type Dir.
PLC/Equipment IPV4 Address
Address String In
ex. “[Link]”
Rack int In PLC Rack number (see below)
Slot int In PLC Slot number (see below)
Return value
In addition to the IP Address, that we all understand, there are two other parameters
that index the unit : Rack (0..7) and Slot (1..31) that you find into the hardware
configuration of your project, for a physical component, or into the Station
Configuration manager for WinAC.
There is however some special cases for which those values are fixed or can work with
a default as you can see in the next table.
Rack Slot
S7 300 CPU 0 2 Always
S7 400 CPU Not fixed Follow the hardware configuration.
WinAC CPU Not fixed Follow the hardware configuration.
S7 1200 CPU 0 0 Or 0, 1
S7 1500 CPU 0 0 Or 0, 1
WinAC IE 0 0 Or follow Hardware configuration.
P a g e | 22
Sharp7 1.0.0 - Reference manual
P a g e | 23
Sharp7 1.0.0 - Reference manual
S7 400 Rack
Rack Slot
CPU 1 0 4
CPU 2 0 11
The same concept for WinAC CPU which index can vary inside the PC Station Rack.
S7300 Rack
Rack Slot
CPU 0 2
P a g e | 23
Sharp7 1.0.0 - Reference manual
P a g e | 24
Sharp7 1.0.0 - Reference manual
SetConnectionParams
Description
Declaration
Parameters
Type Dir.
PLC/Equipment IPV4 Address
Address String In
ex. “[Link]”
LocalTSAP int In Local TSAP (PC TSAP)
RemoteTSAP int In Remote TSAP (PLC TSAP)
Remarks
P a g e | 24
Sharp7 1.0.0 - Reference manual
P a g e | 25
Sharp7 1.0.0 - Reference manual
Connect
Description
Connects the client to the PLC with the parameters specified in the previous call of
ConnectTo() or SetConnectionParams().
Declaration
Return value
Remarks
P a g e | 25
Sharp7 1.0.0 - Reference manual
P a g e | 26
Sharp7 1.0.0 - Reference manual
Disconnect
Description
Declaration
Remarks
P a g e | 26
Sharp7 1.0.0 - Reference manual
P a g e | 27
Sharp7 1.0.0 - Reference manual
Function Purpose
ReadArea Reads a data area from a PLC.
WriteArea Writes a data area into a PLC.
ReadMultiVars Reads different kind of variables from a PLC simultaneously.
WriteMultiVars Writes different kind of variables into a PLC simultaneously.
P a g e | 27
Sharp7 1.0.0 - Reference manual
P a g e | 28
Sharp7 1.0.0 - Reference manual
ReadArea
Description
Declaration
public int ReadArea(int Area, int DBNumber, int Start, int Amount, int WordLen,
byte[] Buffer)
public int ReadArea(int Area, int DBNumber, int Start, int Amount, int WordLen,
byte[] Buffer, ref int BytesRead)
Parameters
(1) Note the use of the parameter name “amount”, it means quantity of elements, not byte size.
Area table
Value Mean
S7Consts.S7AreaPE 0x81 Process Inputs.
S7Consts.S7AreaPA 0x82 Process Outputs.
S7Consts.S7AreaMK 0x83 Merkers.
S7Consts.S7AreaDB 0x84 DB
S7Consts.S7AreaCT 0x1C Counters.
S7Consts.S7AreaTM 0x1D Timers
WordLen table
Value Mean
S7WLBit 0x01 Bit (inside a word)
S7WLByte 0x02 Byte (8 bit)
S7WLWord 0x04 Word (16 bit)
S7WLDWord 0x06 Double Word (32 bit)
S7WLReal 0x08 Real (32 bit float)
S7WLCounter 0x1C Counter (16 bit)
S7WLTimer 0x1D Timer (16 bit)
Return value
P a g e | 28
Sharp7 1.0.0 - Reference manual
P a g e | 29
Sharp7 1.0.0 - Reference manual
Remarks
As said, every data packet exchanged with a PLC must fit in a PDU, whose size is fixed
and varies from 240 up to 960 bytes.
This function completely hides this concept, the data that you can transfer in
a single call depends only on the size available of the data area (i.e. obviously,
you cannot read 1024 bytes from a DB whose size is 300 bytes).
If this data size exceeds the PDU size, the packet is automatically split across more
subsequent transfers.
Particularly:
Where:
Word size
S7Consts.S7WLBit 1
S7Consts.S7WLByte 1
S7Consts.S7WLWord 2
S7Consts.S7WLDWord 4
S7Consts.S7WLReal 4
S7Consts.S7WLCounter 2
S7Consts.S7WLTimer 2
Notes
P a g e | 29
Sharp7 1.0.0 - Reference manual
P a g e | 30
Sharp7 1.0.0 - Reference manual
WriteArea
Description
This is the main function to write data into a PLC. It’s the complementary function of
ReadArea(), the parameters and their meanings are the same.
The only difference is that the data is transferred from the byte buffer into PLC.
Declaration
public int WriteArea(int Area, int DBNumber, int Start, int Amount, int WordLen,
byte[] Buffer)
public int WriteArea(int Area, int DBNumber, int Start, int Amount, int WordLen,
byte[] Buffer, ref int BytesWritten)
Use S7 helper methods to insert S7 data types (int, word, real …) into the byte buffer.
P a g e | 30
Sharp7 1.0.0 - Reference manual
P a g e | 31
Sharp7 1.0.0 - Reference manual
ReadMultiVars
Description
This is function allows to read different kind of variables from a PLC in a single call.
With it you can read DB, Inputs, Outputs, Merkers, Timers and Counters.
Declaration
Parameters
Type Dir.
Item S7DataItem[] In See below
ItemsCount integer In Number of Items to read.
S7DataItem struct
Type Dir.
Area int In Area identifier.
Wordlen int In Word size
Result int Out Item operation result (2)
DB Number if Area = S7AreaDB,
DBNumber int In
otherwise is ignored.
Start int In Offset to start
Amount int In Amount of words to read (1)
pData IntPtr In Pointer to user Buffer
(1) Note the use of the parameter name “amount”, it means quantity of words, not byte size.
Return value
(2) Since could happen that some variables are read, some other not because maybe
they don't exist in PLC. Is important to check the single item Result.
Remarks
To use ReadMultiVars a special class is supplied S7MultiVar that avoids the use of
unsafe code (since there is a IntPtr)
Due the different kind of variables involved , there is no split feature available for this
function, so the maximum data size must not exceed the PDU size.
The advantage of this function becomes big when you have many small noncontiguous
variables to be read.
P a g e | 31
Sharp7 1.0.0 - Reference manual
P a g e | 32
Sharp7 1.0.0 - Reference manual
WriteMultiVars
Description
This is function allows to write different kind of variables from a PLC in a single call.
With it you can read DB, Inputs, Outputs, Merkers, Timers and Counters.
Declaration
Parameters
Type Dir.
Item S7DataItem[] In See below
ItemsCount integer In Number of Items to write.
S7DataItem struct
Type Dir.
Area int In Area identifier.
Wordlen int In Word size
Result int Out Item operation result (2)
DB Number if Area = S7AreaDB,
DBNumber int In
otherwise is ignored.
Start int In Offset to start
Amount int In Amount of words to write (1)
pData IntPtr In Pointer to user Buffer
(2) Note the use of the parameter name “amount”, it means quantity of words, not byte size.
Return value
(2) Since could happen that some variables are written, some other not because
maybe they don't exist in PLC. Is important to check the single item Result.
Remarks
To use WriteMultiVars a special class is supplied S7MultiVar that avoids the use of
unsafe code (since there is a IntPtr)
Due the different kind of variables involved , there is no split feature available for this
function, so the maximum data size must not exceed the PDU size
The advantage of this function becomes big when you have many small noncontiguous
variables to be written.
P a g e | 32
Sharp7 1.0.0 - Reference manual
P a g e | 33
Sharp7 1.0.0 - Reference manual
These are utility functions that simplify the use of ReadArea and WriteArea.
I.e. they call internally ReadArea and WriteArea passing the correct Area and
WordLen.
Function Purpose
DBRead Reads a part of a DB from a PLC.
DBWrite Writes a part of a DB into a PLC.
ABRead Reads a part of IPU area from a PLC.
ABWrite Writes a part of IPU area into a PLC.
EBRead Reads a part of IPI area from a PLC.
EBWrite Writes a part of IPI area into a PLC.
MBRead Reads a part of Merkers area from a PLC.
MBWrite Writes a part of Merkers area into a PLC.
TMRead Reads timers from a PLC.
TMWrite Write timers into a PLC.
CTRead Reads counters from a PLC.
CTWrite Write counters into a PLC.
P a g e | 33
Sharp7 1.0.0 - Reference manual
P a g e | 34
Sharp7 1.0.0 - Reference manual
Function Purpose
GetAgBlockInfo Returns info about a given block in PLC memory.
DBGet Uploads a DB from the PLC
DBFill Fills a DB into the PLC with a given value.
P a g e | 34
Sharp7 1.0.0 - Reference manual
P a g e | 35
Sharp7 1.0.0 - Reference manual
GetAgBlockInfo
Description
Declaration
Parameters
Type Dir.
BlockType int In Type of Block that we need
BlockNum int In Number of Block
Block S7BlockInfo in S7BlockInfo struct
BlockType values
S7BlockInfo struct
P a g e | 35
Sharp7 1.0.0 - Reference manual
P a g e | 36
Sharp7 1.0.0 - Reference manual
This struct is filled by the function, some fields require additional info:
SubBlockType table
Value Type
SubBlk_OB 0x08 OB
SubBlk_DB 0x0A DB
SubBlk_SDB 0x0B SDB
SubBlk_FC 0x0C FC
SubBlk_SFC 0x0D SFC
SubBlk_FB 0x0E FB
SubBlk_SFB 0x0F SFB
LangType table
Return value
P a g e | 36
Sharp7 1.0.0 - Reference manual
P a g e | 37
Sharp7 1.0.0 - Reference manual
DBGet
Description
Read an entire DB from the PLC without the need of specifying its size. As output
SizeRead will contain the size read.
Declaration
Parameters
Type Dir.
DBNumber int In DB Number
Data byte array in Address of the user buffer
In : Buffer size supplied
SizeRead int ref In/Out
Out : Number of bytes read
Return value
Remarks
This function first gathers the DB size via GetAgBlockInfo then calls ReadArea if the
Buffer size is greater than the DB size, otherwise returns an error.
P a g e | 37
Sharp7 1.0.0 - Reference manual
P a g e | 38
Sharp7 1.0.0 - Reference manual
DBFill
Description
Fills a DB in AG with a given byte without the need of specifying its size.
Declaration
Parameters
Type Dir.
Return value
Remarks
Fillchar is an integer for efficiency reasons, only the lowest byte is used.
P a g e | 38
Sharp7 1.0.0 - Reference manual
P a g e | 39
Sharp7 1.0.0 - Reference manual
Date/Time functions
Imagine a production line in which each PLC saves the data with date/time field inside,
it is very important that the date be up to date.
Both CP X43 and internal PN allow to synchronize date and time but you need an NTP
server, and in some cases (old hardware or CP343-1 Lean or old firmware release)
this doesn’t work properly.
Function Purpose
GetPlcDateTime Returns the PLC date/time.
SetPlcDateTime Sets the PLC date/time with a given value.
SetPlcSystemDateTime Sets the PLC date/time with the host (PC) date/time.
P a g e | 39
Sharp7 1.0.0 - Reference manual
P a g e | 40
Sharp7 1.0.0 - Reference manual
GetPlcDateTime
Description
Declaration
Parameters
Type Dir.
DateTime Date In DateTime class instance
Return value
P a g e | 40
Sharp7 1.0.0 - Reference manual
P a g e | 41
Sharp7 1.0.0 - Reference manual
SetPlcDateTime
Description
Declaration
Parameters
Type Dir.
DateTime Date In DateTime class instance
Return value
P a g e | 41
Sharp7 1.0.0 - Reference manual
P a g e | 42
Sharp7 1.0.0 - Reference manual
SetPlcSystemDateTime
Description
Sets the PLC date and time in accord to the PC system Date/Time.
Declaration
Return value
P a g e | 42
Sharp7 1.0.0 - Reference manual
P a g e | 43
Sharp7 1.0.0 - Reference manual
these functions access to SZL (or SSL - System Status List) to give you all the same
information that you can get from S7 Manager.
The system status list (SSL) describes the current status of a programmable logic
controller.
The contents of the SSL can only be read using information functions but cannot be
modified. The partial lists are virtual lists, in other words, they are only created by the
operating system of the CPUs when specifically requested.
You can access to system status list using SFC 51 too "RDSYSST."
Function Purpose
ReadSZL Reads a partial list of given ID and Index.
GetOrderCode Returns the CPU order code.
GetCpuInfo Returns some information about the AG.
GetCpInfo Returns some information about the CP (communication processor).
P a g e | 43
Sharp7 1.0.0 - Reference manual
P a g e | 44
Sharp7 1.0.0 - Reference manual
ReadSZL
Description
Declaration
public int ReadSZL(int ID, int Index, ref S7SZL SZL, ref int Size)
Parameters
Type Dir.
ID int In List ID
Index int In List Index
SZL S7Szl in S7Szl class instance
Size int out Size Read in bytes
S7SZL
Return value
Remarks
P a g e | 44
Sharp7 1.0.0 - Reference manual
P a g e | 45
Sharp7 1.0.0 - Reference manual
GetOrderCode
Description
Declaration
Parameters
Type Dir.
Info S7OrderCode Out See below
P a g e | 45
Sharp7 1.0.0 - Reference manual
P a g e | 46
Sharp7 1.0.0 - Reference manual
GetCpuInfo
Description
Declaration
Parameters
Type Dir.
Info S7CpuInfo Out See below
Return value
P a g e | 46
Sharp7 1.0.0 - Reference manual
P a g e | 47
Sharp7 1.0.0 - Reference manual
GetCpInfo
Description
Declaration
Parameters
Type Dir.
Info S7CpInfo Out See below
Return value
P a g e | 47
Sharp7 1.0.0 - Reference manual
P a g e | 48
Sharp7 1.0.0 - Reference manual
With these control function it’s possible to Start/Stop a CPU and read the PLC status.
Function Purpose
PlcColdStart Puts the CPU in RUN mode performing an COLD START.
PlcHotStart Puts the CPU in RUN mode performing an HOT START.
PlcStop Puts the CPU in STOP mode.
PlcGetStatus Returns the CPU status (running/stopped).
P a g e | 48
Sharp7 1.0.0 - Reference manual
P a g e | 49
Sharp7 1.0.0 - Reference manual
PlcColdStart
Description
Declaration
Return value
Remarks
P a g e | 49
Sharp7 1.0.0 - Reference manual
P a g e | 50
Sharp7 1.0.0 - Reference manual
PlcHotStart
Description
Declaration
Return value
Remarks
P a g e | 50
Sharp7 1.0.0 - Reference manual
P a g e | 51
Sharp7 1.0.0 - Reference manual
PlcStop
Description
Declaration
Return value
Remarks
P a g e | 51
Sharp7 1.0.0 - Reference manual
P a g e | 52
Sharp7 1.0.0 - Reference manual
PlcGetStatus
Description
Declaration
Parameters
Type Dir.
Status int Out Plc status
Status values
Return value
P a g e | 52
Sharp7 1.0.0 - Reference manual
P a g e | 53
Sharp7 1.0.0 - Reference manual
Security functions
With these functions is possible to know the current protection level, and to set/clear
the current session password.
Especially because, if you look at the source code, there is an encoding function that
translates the plain password before send it to the PLC.
PASSWORD HACKING IS VERY FAR FROM THE AIM OF THIS PROJECT, MOREOVER
YOU NEED TO KNOW THE CORRECT PASSWORD TO MEET THE CPU SECURITY
LEVEL.
Detailed information about the protection level can be found in §33.19 of "System
Software for S7-300/400 System and Standard Functions".
Function Purpose
SetSessionPassword Send the password to the PLC to meet its security level.
ClearSessionPassword Clears the password set for the current session (logout).
GetProtection Gets the CPU protection level info.
P a g e | 53
Sharp7 1.0.0 - Reference manual
P a g e | 54
Sharp7 1.0.0 - Reference manual
SetSessionPassword
Description
Declaration
Parameters
Type Dir.
Password String In 8 chars UTF-8 string
Return value
Remarks
P a g e | 54
Sharp7 1.0.0 - Reference manual
P a g e | 55
Sharp7 1.0.0 - Reference manual
ClearSessionPassword
Description
Declaration
Return value
P a g e | 55
Sharp7 1.0.0 - Reference manual
P a g e | 56
Sharp7 1.0.0 - Reference manual
GetProtection
Description
Declaration
Parameters
Type Dir.
Protection S7Protection Out See below
TS7Protection fields
public struct S7Protection
{
public ushort sch_schal;
public ushort sch_par;
public ushort sch_rel;
public ushort bart_sch;
public ushort anl_sch;
};
Field Values
Values
sch_schal 1, 2, 3 Protection level set with the mode selector.
sch_par 0, 1, 2, 3 Password level, 0 : no password
sch_rel 0, 1, 2, 3 Valid protection level of the CPU
Mode selector setting (1:RUN, 2:RUN-P, 3:STOP,
bart_sch 1, 2, 3, 4 4:MRES, 0:undefined or cannot be determined)
Startup switch setting (1:CRST, 2:WRST,
anl_sch; 0, 1, 2 0:undefined, does
not exist of cannot be determined)
See also §33.19 of "System Software for S7-300/400 System and Standard Functions"
Return value
P a g e | 56
Sharp7 1.0.0 - Reference manual
P a g e | 57
Sharp7 1.0.0 - Reference manual
Properties/Info Functions
ConnTimeout
int, in/Out – Gets/Sets the connection timeout (in milliseconds) for a telegram.
RecvTimeout
int, in/out – Gets/Sets the receiving timeout (in milliseconds) for a telegram.
SendTimeout
int, in/out – Gets/Sets the sending timeout (in milliseconds) for a telegram.
Connected
PduSizeNegotiated
PduSizeRequested
PLCPort
ExecutionTime
Returns the last operation execution time in ms. (compatibility with [Link])
P a g e | 57
Sharp7 1.0.0 - Reference manual
P a g e | 58
Sharp7 1.0.0 - Reference manual
S7 Helper reference
This class allow to read/write an “S7” value into a byte buffer, all new S71200/1500 types
are supported.
public static <.NET native Type> Get<S7 Type>At(byte[] Buffer, int Pos)
They extract a <S7 Type> var from the byte buffer starting from the byte number “pos”.
public static void Set<S7 Type>At(byte[] Buffer, int Pos, <.NET native Type> Value)
They insert a .net native type var into the byte buffer starting from the byte number
“pos”.
P a g e | 58
Sharp7 1.0.0 - Reference manual
P a g e | 59
Sharp7 1.0.0 - Reference manual
Now, just a real example of how to read a S7 struct from a PLC into .NET struct using
this class in C# and VB.
Let’s suppose that we make a leak test on an automotive component and that we
want to acquire the result of this test.
The data of this test consists of some values (Component serial number, Leak Value
etc..) stored in DB 100 as in figure.
Every time a component is tested this struct is filled by the PLC and we want to read it
into a .NET struct, picking the fields and adjusting their byte-order (the PLC is Big-
Endian, the PC is Little-Endian, so the bytes must be reversed).
And this is the C# sample function that fills the struct. Notice that the outcome of any
Client function call should be checked, here is skipped for brevity.
P a g e | 59
Sharp7 1.0.0 - Reference manual
P a g e | 60
Sharp7 1.0.0 - Reference manual
The struct
Finally, if you put a button and 4 labels in the form of VB demo, with this simple code
you can test the mechanism.
End Sub
P a g e | 60
Sharp7 1.0.0 - Reference manual
P a g e | 61
Sharp7 1.0.0 - Reference manual
S7MultiVar reference
This class allows to fill the S7DataItem[] and performs a ReadMultivar or WriteMultivar
without using unsafe code.
The best way to understand the use of this class is analyzing an example.
Read example
// Our buffers
byte[] DB_A = new byte[1024];
byte[] DB_B = new byte[1024];
byte[] DB_C = new byte[1024];
If everything is ok into DB_A, DB_B, DB_C we will find the first 16 bytes of DB1, DB2 and
DB3.
Remarks
1. [Link][] will contain the single outcome of the items, it’s possible that some
items were read and some other not.
2. The parameters of Add() are the same of Read/WriteArea, you can read/write non
only DB but also E/A/MK/T/C and also bits.
P a g e | 61
Sharp7 1.0.0 - Reference manual
P a g e | 62
Sharp7 1.0.0 - Reference manual
Error codes
Please refer to code of the function ErrorText() for the explanation
errTCPSocketCreation = 0x00000001;
errTCPConnectionTimeout = 0x00000002;
errTCPConnectionFailed = 0x00000003;
errTCPReceiveTimeout = 0x00000004;
errTCPDataReceive = 0x00000005;
errTCPSendTimeout = 0x00000006;
errTCPDataSend = 0x00000007;
errTCPConnectionReset = 0x00000008;
errTCPNotConnected = 0x00000009;
errTCPUnreachableHost = 0x00002751;
errIsoConnect = 0x00010000;
errIsoInvalidPDU = 0x00030000;
errIsoInvalidDataSize = 0x00040000;
errCliNegotiatingPDU = 0x00100000;
errCliInvalidParams = 0x00200000;
errCliJobPending = 0x00300000;
errCliTooManyItems = 0x00400000;
errCliInvalidWordLen = 0x00500000;
errCliPartialDataWritten = 0x00600000;
errCliSizeOverPDU = 0x00700000;
errCliInvalidPlcAnswer = 0x00800000;
errCliAddressOutOfRange = 0x00900000;
errCliInvalidTransportSize = 0x00A00000;
errCliWriteDataSizeMismatch = 0x00B00000;
errCliItemNotAvailable = 0x00C00000;
errCliInvalidValue = 0x00D00000;
errCliCannotStartPLC = 0x00E00000;
errCliAlreadyRun = 0x00F00000;
errCliCannotStopPLC = 0x01000000;
errCliCannotCopyRamToRom = 0x01100000;
errCliCannotCompress = 0x01200000;
errCliAlreadyStop = 0x01300000;
errCliFunNotAvailable = 0x01400000;
errCliUploadSequenceFailed = 0x01500000;
errCliInvalidDataSizeRecvd = 0x01600000;
errCliInvalidBlockType = 0x01700000;
errCliInvalidBlockNumber = 0x01800000;
errCliInvalidBlockSize = 0x01900000;
errCliNeedPassword = 0x01D00000;
errCliInvalidPassword = 0x01E00000;
errCliNoPasswordToSetOrClear = 0x01F00000;
errCliJobTimeout = 0x02000000;
errCliPartialDataRead = 0x02100000;
errCliBufferTooSmall = 0x02200000;
errCliFunctionRefused = 0x02300000;
errCliDestroying = 0x02400000;
errCliInvalidParamNumber = 0x02500000;
errCliCannotChangeParam = 0x02600000;
errCliFunctionNotImplemented = 0x02700000;
P a g e | 62
Sharp7 1.0.0 - Reference manual