You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

---------->>>>>Get FREE PLC Programming Tips

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

PLC training tools sale

Reply
Thread Tools Display Modes
Unread September 30th, 2014, 09:26 AM   #1
DiegoE
Member
Brazil

DiegoE is offline
 
Join Date: Sep 2014
Location: 98280000
Posts: 21
Question PLC and Python Communication, HELP

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!
  Reply With Quote
Unread September 30th, 2014, 12:40 PM   #2
Thomas_v2
Member
Germany

Thomas_v2 is offline
 
Join Date: Apr 2009
Location: Ger
Posts: 411
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.
  Reply With Quote
Unread October 1st, 2014, 06:53 AM   #3
DiegoE
Member
Brazil

DiegoE is offline
 
Join Date: Sep 2014
Location: 98280000
Posts: 21
Hi Thomas_v2

Can you tell me more about Tsend, TRecv?
Thank you!
  Reply With Quote
Unread October 1st, 2014, 12:34 PM   #4
DiegoE
Member
Brazil

DiegoE is offline
 
Join Date: Sep 2014
Location: 98280000
Posts: 21
Hi Thomas_v2

Do you can give me an example of Python code for connection with PLC?

Thank you!
  Reply With Quote
Unread October 1st, 2014, 04:22 PM   #5
Thomas_v2
Member
Germany

Thomas_v2 is offline
 
Join Date: Apr 2009
Location: Ger
Posts: 411
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")
Attached Files
File Type: pdf S71200-TconPassive-Demo-TIA-V11-s1.pdf (178.1 KB, 311 views)
File Type: pdf S71200-TconPassive-Demo-TIA-V11-s2.pdf (173.1 KB, 170 views)
File Type: pdf S71200-TconPassive-Demo-TIA-V11-s3.pdf (173.8 KB, 156 views)
  Reply With Quote
Unread October 7th, 2014, 07:00 AM   #6
DiegoE
Member
Brazil

DiegoE is offline
 
Join Date: Sep 2014
Location: 98280000
Posts: 21
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!

  Reply With Quote
Unread October 7th, 2014, 10:49 AM   #7
Thomas_v2
Member
Germany

Thomas_v2 is offline
 
Join Date: Apr 2009
Location: Ger
Posts: 411
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")
  Reply With Quote
Unread October 7th, 2014, 11:42 AM   #8
DiegoE
Member
Brazil

DiegoE is offline
 
Join Date: Sep 2014
Location: 98280000
Posts: 21
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!
  Reply With Quote
Unread October 7th, 2014, 11:52 AM   #9
Thomas_v2
Member
Germany

Thomas_v2 is offline
 
Join Date: Apr 2009
Location: Ger
Posts: 411
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.
  Reply With Quote
Unread October 7th, 2014, 12:26 PM   #10
DiegoE
Member
Brazil

DiegoE is offline
 
Join Date: Sep 2014
Location: 98280000
Posts: 21
Hi Thomas_v2

Here in this part of the blocks did exactly what tava the screenshots, I think that is correct.

Thank you!
  Reply With Quote
Unread October 8th, 2014, 06:39 AM   #11
DiegoE
Member
Brazil

DiegoE is offline
 
Join Date: Sep 2014
Location: 98280000
Posts: 21
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!!!!!!!!
  Reply With Quote
Unread October 8th, 2014, 12:12 PM   #12
Thomas_v2
Member
Germany

Thomas_v2 is offline
 
Join Date: Apr 2009
Location: Ger
Posts: 411
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.
Attached Images
File Type: jpg S71200-V11-TSEND.jpg (26.7 KB, 319 views)
  Reply With Quote
Unread October 9th, 2014, 06:20 AM   #13
DiegoE
Member
Brazil

DiegoE is offline
 
Join Date: Sep 2014
Location: 98280000
Posts: 21
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 !!
  Reply With Quote
Unread October 9th, 2014, 11:10 AM   #14
DiegoE
Member
Brazil

DiegoE is offline
 
Join Date: Sep 2014
Location: 98280000
Posts: 21
I remembered one more thing, is instead of the PLC I read with a CHAR, can read with a WORD?

Thank you!
  Reply With Quote
Unread October 16th, 2014, 06:33 AM   #15
DiegoE
Member
Brazil

DiegoE is offline
 
Join Date: Sep 2014
Location: 98280000
Posts: 21
??
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
PC and PLC Communication DiegoE LIVE PLC Questions And Answers 6 September 30th, 2014 07:56 AM
Universal PLC Monitor Nitay LIVE PLC Questions And Answers 11 August 15th, 2014 04:11 AM
PC <-> PLC & real time communication DINT2 LIVE PLC Questions And Answers 1 February 11th, 2014 02:21 PM
Communication between Simotion and Linux wim_ LIVE PLC Questions And Answers 1 April 16th, 2010 03:41 AM
A communication problem between a PLC and a PC through WLAN Decamber LIVE PLC Questions And Answers 3 October 15th, 2008 07:45 AM


All times are GMT -5. The time now is 03:31 AM.


.