Example Code

S7 PLC TCP/IP Protocol Reference Example

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Download All

Description

Overview

This example provides a library of LabVIEW VI's that can be used to communicate with a Siemens S7-300 PLC via an Ethernet port over TCP/IP.

 

Description

This example communicates with a S7-300 series PLC through Ethernet TCP/IP - no adapter is needed. The example provides an API to read from / write to registers on the PLC.

S7_TCP_API_2.PNG

To my knowledge, Siemens has never released the details of the S7 protocol.  Much of what is publicly available on S7 is based on observations of the protocol by others.  There is an open source S7 data exchange package available at http://libnodave.sourceforge.net/ that documents many S7 features.

 

These VIs were based upon the example posted at http://decibel.ni.com/content/docs/DOC-5467.  They were modified by observing and mimicking a S7 data exchange between a protocol converter and a S7-300 series PLC.  The observations were accomplished with the wireshark network protocol analyzer, available at http://www.wireshark.org/ and the Wireshark Plugin für S7-Protokoll, available at http://sps-forum.de/showthread.php?p=202763.

 

Steps to Execute Code

The example contains two top-level VIs: S7Com_Once.vi and S7Com_W+R_Loop.vi.

     1.S7Com_Once.vi performs one read or write per execution.  It writes to the target PLC from an array of I32, it reads from the target PLC into an array of I32.  The number of bytes written is proportional to the number of elements in the array to be written and the transport size parameter.

     2.S7Com_W+R_Loop.vi regularly writes to and reads from a pair of DBs in the target PLC.  In order to use it, your Step7 PLC project should provide two Data Blocks, DB11 and DB12.  See the screen capture image db11&12.PNG for their layout.  After downloading the DBs to your PLC, monitor and change DB VAlues with a VAT.  As can be seen in the VI, the transfer size is DWORD.

     3. For more information, refer to the Read Me included in the download.

 

Requirements

Software

LabVIEW 8.5 or Later

 

Hardware

Siemens S7-300

 

Download Versions

Siemens S7 TCP v3_3 LV 8_5.zip

     This code is setup to be fault tolerant; i.e. - if a cable is disconnected or connection is lost the "S7_Error_Reconnect.vi" can be called to cleanup the bad connection and re-open a good connection. There are some optimizations for reading multiple registers from a single DB block.This code is derived from the Serial MPI S7 Protocol Exaple

S7Com_2009_11_06.zip

     This code was provided by a fellow developer (thanks!) and uses a slightly different architecture.

Note - the attached files are experimental VIs provided for educational purposes only.  No warrantee is expressed or implied.  You should test your code and completely understand the implications of writing to or reading from an operating PLC.

 

**This document has been updated to meet the current required format for the NI Code Exchange.**

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
MarcoPolo5
Member
Member
on

sounds like potentially a byte swap issue; check if the PLC is setting MSB and LSB locations different; or if there's some issue with recieving the code as a different data type.

led986
Member
Member
on

I'm working with a 315 Vipa plc, 100% compatible with Siemens! However, this may be the problem ?!

Current_93
Member
Member
on

Dear Bobi

Thank you very much for sharing this new modification for S7-1200.

Please help me - what about I did wrong (see the picture). I'm opening connection to my PLC S7-1214 and at once occur 66 error code from "Read TCP command"

MPolo.jpg

Current_93
Member
Member
on

Dear Bobi and all fellows!

All good working now, I've simply setting up all formats TPKT exactly!

"people who share knowledge - live in eternity"

heribert
Member
Member
on

Hello!

My connection to a Siemens PLC is running well. But, has anybody tried to read or write varable of type REAL?!

Does it work? How?

KR heri

bobi
Member
Member
on

Hello!

I tried and everything works fine. Do you get any error message?

BR Bobi

Best Regards
awink
Member
Member
on

For your information - also working perfectly for Siemens LOGO! 0BA8 over Ethernet.

Thank you very much!

cortocircuito2003
Member
Member
on

can you help me to reead my logo8?

Tank you

awink
Member
Member
on

Of course!

Based on the project of the threadstarter find the FP as picture containing the request-settings and the BD as snippet (why are no attachments possible here?).

Best regards!

SiemensRead_FP.png

SiemensRead_BD.png

Current_93
Member
Member
on

Dear fellows!

Really it doesn't work!    I can't to read/write any REAL data types of variables

led986
Member
Member
on

.... I have the same problem.... I can't read/write any real data types correctly.

kunalbhate
Member
Member
on

To read the REAL datatypes, you have to read 4 or 8 bytes depending upon the data type (single or double) and then TYPECAST to real (single or double).

Remember the bytes might be transmitted in bigendian format. You will have to rearrange them and then typecast.

awink
Member
Member
on

So what is the Error-Cluster saying?

What do you mean with "correctly"?

Siegmund
Member
Member
on

Thought this might help. I managed to succesfully connect to a Siemens S7 1200, after implementing the changes proposed by Bobi. However, i wasn't able to read or write any data. Then i came across this Forum:

http://sourceforge.net/p/libnodave/discussion/205657/thread/7ec9f370/

and the solution that worked for me was enabling:

"Permit acces with PUT/GET communication from remote partner (PLC, HMI, OPC,...)" in the TIA Portal (the setting can be found under Properties > Protection > Connection mechanisms). Until this option was set, i couldnt write or read the sps.

Jahi
Member
Member
on

Seems to work nicely with S7-300 but has anyone tried these VIs with S7-1500 series?

I tried with CPU 1516F but VIs did not work directly. With little bit modification I was able to "open" connection with PLC but was not able to read data from PLC (memoery area). VIs gave error “Error 66 occurred at TCP Read”

I was wondering has someone already figured out how to modify VIs to work with S7-1500?

swdegs
Member
Member
on

Jahi - I have a CPU 1515-2 PN.  I was able to get it communicating with LabView after making the modification somebody mentioned earlier in the thread about S7-1200's.  I modified the 18th element of the "Connected to Adapter" and "Connect to Adapter" arrays from "x 2" to "x 0".  Arrays are found in the "S7_Init_Adapter_TCP.vi".Capture.PNG

Current_93
Member
Member
on

Really does this code works with Logo8?

bobi
Member
Member
on

Communicatio works OK. You must set the 18.octet in "TPDU connection request" to x00 (in Init TCP). For S7-300 x02, for S7-400 x03, for S7-1200 and Logo x00. I think it work also with VIPA S7-300 if you set it to x00. I hope this will help you.

Best Regards
soren_jensen
Member
Member
on

I can confirm that it works great with Logo8 !. But do be carefull with fast polling. I have observed strange behaviours if pooling rates go below 100ms. The logo8 acted as if it closed the TCP connection if the polling rate was set to fast.

Thanks for everyone for sharing theese great tools.

/søren

Advait123
Member
Member
on

Thanks for the code..it is very useful.

I suceed to read and write with the PLC-300 but only when the values are integer. But when I force the float values (like 40.6, 24.8) from the PLC, I am not able to read in LabVIEW and vice versa.

Do I need to make setting change in the current code or it communicates only with the integer values?

Blokk
Trusted Enthusiast
Trusted Enthusiast
on

Hello dear PLC gurus. We did not have too much success testing the shared example VIs, but this is mainly because of our very little knowledge of PLCs. However, using a simple TCP Listen VI we are now able to read data coming from the PLC. However the "from-PC-to-PLC" data communications seems non deterministic, namely we have to send around 100 data blocks until the PLC reacts and updates the target value.

Our issues are explained in the following post, someone might give us a hint what else to try. Thanks very much!

http://forums.ni.com/t5/LabVIEW/TCP-IP-access-to-Siemens-S7-from-RT-no-OPC/td-p/3318979

sqrt(-1)
Member
Member
on

Hello,

I tried this library with S7-300 and it works very very fine.

Does this library can be used wit S7-1200 1300 and 1500?

 

Adrien_31
Member
Member
on

Hello,

I'm using this Library with S7-1200 CPU 1212C and I am not able to write/read BYTE data in DB areas.

The connection seems good ; confirmed by the S7Com_OPEN(SubVI).vi execution, but when I uses the S7Com_WRT+READ(SubVI).vi in order to write a BYTE data into the DB#1, an error occurs and nothing is writting.

I send this following frame {0x32; 0x01; 0x00; 0x00; 0x00; 0x03; 0x00; 0x0E; 0x00; 0x07; 0x05; 0x01; 0x12; 0x0A; 0x10; 0x02; 0x00; 0x03; 0x00; 0x01; 0x84; 0x00; 0x00; 0x00; 0x00; 0x04; 0x00; 0x18; 0x01; x03; 0x05} according to these inputs parameters :

   S7_Function = Write ;

   S7_Transport_Size = BYTE ;

   Read Length = 4 ;

   Data Block # = 1 ;

   S7_Add_Area = DB_Area ;

   Start_Address = 0 ;

   Write_Data = {1; 3; 5}

So, I do not understand why the 3 values are not written into DB#1 area, addresses 0 to 2.

Could someone please help me to resolve this issue ?

Thanks in advance,

Adrien

梦魂飞
Member
Member
on
感谢分享,我在S7com,我在使用过程中发现,运行一段时间会断开。但是网络并无异常,是否有其他朋友也遇到类似情况?
PADHU.SR
Member
Member
on

 Dear ALL,

                 Could you help me to write data to S7-1200 PLC using the S7 APIs i dint understand the method of writing since i randomly gave output values in APIs and checked,but entered data and output on PLC is different,also can you share some document or manual on S7 API LABVIEW

joerg.hampel
Active Participant Active Participant
Active Participant
on

I'm wondering, has anybody continued work on either of the two examples posted in this document since 2017? Is there a place somewhere (like github etc) to actually contribute to the code? 




DSH Pragmatic Software Development Workshops (Fab, Steve, Brian and me)
Release Automation Tools for LabVIEW (CI/CD integration with LabVIEW)
HSE Discord Server (Discuss our free and commercial tools and services)
DQMH® (The Future of Team-Based LabVIEW Development)


joerg.hampel
Active Participant Active Participant
Active Participant
on

As there is no easy way to collaborate on code posted to the Example Programs, I got in touch with MarcoPolo5 (the original poster), and with his consent republished the code he shared on a public GitLab repository:

https://gitlab.com/hampel-soft/open-source/s7netcom

 

We started documenting and reworking the code, to make it more accessible and easier to use with our customer projects. Beware, it's still a long way from being finished and polished:

https://dokuwiki.hampel-soft.com/code/hse/s7netcom

https://dokuwiki.hampel-soft.com/kb/production/s7-communication

 

We welcome any input that anybody is willing to give!




DSH Pragmatic Software Development Workshops (Fab, Steve, Brian and me)
Release Automation Tools for LabVIEW (CI/CD integration with LabVIEW)
HSE Discord Server (Discuss our free and commercial tools and services)
DQMH® (The Future of Team-Based LabVIEW Development)


Rasesh
Member
Member
on

I tried this library with S7-300 and it works very very fine.

Does this library can be used with S7- 1500 PLCs?

If yes, Please provide necessary modifications in code.

 

Thanks in advance.

 

Rasesh J.Dave

Email : raseshjd@yahoo.com

 

joerg.hampel
Active Participant Active Participant
Active Participant
on

Please refer to the context help of the driver VIs, and to our Dokuwiki which I linked to in my last post, it contains information on how to configure the driver for different PLC types (no source code changes needed):

 

https://dokuwiki.hampel-soft.com/kb/production/s7-communication/constants#plc_types

 

Since my last post here, we‘ve packaged the driver into a .vip and released it on vipm.io:

 

https://www.vipm.io/package/hse_lib_s7netcom/




DSH Pragmatic Software Development Workshops (Fab, Steve, Brian and me)
Release Automation Tools for LabVIEW (CI/CD integration with LabVIEW)
HSE Discord Server (Discuss our free and commercial tools and services)
DQMH® (The Future of Team-Based LabVIEW Development)


Rasesh
Member
Member
on

Dear All,

I am able to perform Read and Write Operations with provided source code.

Now ,I would like to write array (~ 30 WORD) in single DB.

Please suggest necessary changes in VI.

I am using Siemens S7 TCP v3_3 LV 8_5.zip

 

Thanks in advance.

Rasesh J.Dave

raseshjd@yahoo.com

joerg.hampel
Active Participant Active Participant
Active Participant
on

I'm sorry, we cannot give support for legacy VIs we did not create. 

 

Feel free to give our open-source toolkit S7NetCom a try, maybe it helps with your problem. It is available via vipm.io

 

If you think there is a bug in the S7NetCom toolkit, please create an issue at https://gitlab.com/hampel-soft/open-source/s7netcom/-/issues

 

For commercial support, please contact office@hampel-soft.com.




DSH Pragmatic Software Development Workshops (Fab, Steve, Brian and me)
Release Automation Tools for LabVIEW (CI/CD integration with LabVIEW)
HSE Discord Server (Discuss our free and commercial tools and services)
DQMH® (The Future of Team-Based LabVIEW Development)


kim-momo
Member
Member
on

kimmomo_0-1680253226804.pngkimmomo_1-1680253267189.png

When I connect PLC with S7NetCom, there is an error as shown in the picture. Please help to answer it, thank you

kim-momo
Member
Member
on

When I change the execution of the TPKT-Read Packet.vi attribute to non-reentre, there will be no error, but there will be a delay when multiple PLCS fetch DB area

kimmomo_0-1680253591333.png

 

LV2017
Member
Member
on

Dear All

I would like to communicate with the attached file 'S7Com_2009_11_06.zip' with S7-1500.

Could you inform the 'TPKT', 'TPDU' Packet and slot number?

is not working.

Could someone please help me to resolve this issue ?

Thanks in advance

 

joerg.hampel
Active Participant Active Participant
Active Participant
on

@kim-momo can you share more details about the setup you used when running into the described error? I've created issue #29 in the S7NetCom driver's GitLab project, it'd be great if we could continue the discussion there. Thank you!




DSH Pragmatic Software Development Workshops (Fab, Steve, Brian and me)
Release Automation Tools for LabVIEW (CI/CD integration with LabVIEW)
HSE Discord Server (Discuss our free and commercial tools and services)
DQMH® (The Future of Team-Based LabVIEW Development)