PLC and Python Communication, HELP

DiegoE

Member
Join Date
Sep 2014
Location
98280000
Posts
21
Hi, I need to create a Python script to exchange data with a PLC Siemens S7-1200 via Ethernet cable, if anyone knows something and can help me I appreciate it.

Thank you!:D
 
Hi,
there are at least three possible ways to communicate with a S7-1200.

1) Using a raw TCP connection
On plc side this is called open communication.
You have to configure a connection (active or passive), and to program the communication functions TSEND/TRECV.
On python side you can use the usual python sockects.
Seems easy, but you remind that TCP is a stream and you have to consider the different endianess in a Siemens plc.

2) Using native S7 protocol with additional communication library
On plc side you have to do nothing.
On python side you need an additional communication library which implements the S7 protocol.
For python this could be this:
https://pypi.python.org/pypi/python-snap7/

You get only access to not-optimized datablocks in your S7-1200.

3) Using Modbus TCP
On plc side you have to program the modbus function blocks.
On python side you'll need a modbus TCP library.
 
I've attached some screenshots how you can send data from python into your S7-1200.
In the shown configuration the Plc is passive, so it waits for incoming connections.

Add a Datablock to your project, and name it "RecvData". Insert a variable "data" with type "Array [0..255] of char" into this DB.

Add a new function block to your project, name it "TcpTest" for example.

Open the FB "TcpTest" and insert program shown in the screenshots.

The absolute minimalistic python script to send data is:
Code:
import socket

address = "192.168.1.191"
port = 2000
sock = socket.socket()
sock.connect(address, port)
sock.send("hello")
 
Hi Thomas_v2

I am having the following errors on the side of the Python script ....
The first mistake putting solve (.......) of (address, port).
The second error does not know how to solve .... This is translated into English error:

Traceback (most recent call last):
File "C: /Users/Anderson/Desktop/teste.py", line 6, in <module>
sock.connect ((address, port))
TimeoutError: [winError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host failed to respond

I saw a strange thing in the TIA Portal v12 I use when I go to download the program to the PLC it shows an IP assing (if I'm not mistaken it is) and assigns an IP 192.168.1.241, because he does it?

If you have a solution please let me know!

Thank you so much!

300ebus.png
 
I've used Python 2. Unfortunately Python 2 and Python 3 are not fully compatible.

Change the last two lines of my example to:
Code:
import socket

address = "192.168.1.191"
port = 2000
sock = socket.socket()
sock.connect((address, port))
sock.send(b"hello")
 
Hi Thomas_v2

Could resolve these errors, ta all right now, without error, only one thing, I send the data "hello" and quiet, but do not write anything in DB RecvDaten remains blank, know what can be?

Thank you very outset friend, you helped me a lot!
 
Check in your plc program the parameter "LEN" of the TRCV call. Only if this number of bytes are received, the data is copied into the area given in parameter "DATA".

In my example I've choosen 5 because the string "hello" is 5 bytes long.
 
Hi Thomas_v2

Already have only to thank you friend, you helped me very much the same, I could make the switch successful data !! Now I have one more doubt, it is possible to do the opposite? Ie the PLC to send data this time?

Thank you so much!!!!!!!! :D
 
You can use the TSEND function to send data back to your python script, when a client has connected or specific data were received. In my example shown in the attached screenshot, if the first two received chars are 'h' and 'e', then TSEND is triggered and the first 5 bytes of "SendData".data (which could be 'world') is sent back.

So if you send "hello" with your python script, you should receive "world".

Code:
sock.send(b"hello")
print("SENT hello")
data = sock.recv(5)
print ("RECIEVED:", data)
Remind that this is only an example without any error handling. The more difficult part in TCP communication is to program it robust, so it can handle all the different types of problems which could occur.

S71200-V11-TSEND.jpg
 
Hi Thomas_v2

In the TCON block of previous posts, I have to define a number of characters that will be sent or has as he gets set for any amount?

Thank you !!
 

Similar Topics

Hello everybody! I found option for creating communication with Q series via python. But can't find any tips for connecting with PLC FX series...
Replies
3
Views
5,241
Hi, I need to create a Python script to exchange data with a PLC schneider modicon m580 via Ethernet cable, if anyone knows something and can help...
Replies
14
Views
4,514
Good day dear Experts! Is there a way to simulate a communication between a S7-1200 PLC and python script? I know this is possible physically...
Replies
3
Views
3,525
I'm currently a senior in college working on a senior design project involving PLCs and Raspberry Pi. We have data taken using the PLC that we...
Replies
14
Views
3,998
It is pretty easy to read data from xml, json or .csv files using python. Can Rockwell and/or Siemens trends be saved in any of these formats...
Replies
9
Views
3,034
Back
Top Bottom