How can I communicate between a Siemens S7-1200 and python?
Asked Answered
A

7

10

I am running a process on a S7-1200 plc and I need it to send a start signal to my python script, after the script is done running it needs to send something back to the plc to initiate the next phase. Oh, and it has to be done in ladder.

Is there a quick and dirty way to send things over profibus or am I better off using just a RS232 thing?

Aggi answered 27/4, 2012 at 18:30 Comment(2)
did you google it? this may help scorpion.tordivel.no/help/Python/Unsupported%20Modules/…Sofia
I was hoping i'd be able to do it without an opc server.Aggi
A
5

After failing with libnodave and OPC, I created a TCON,TSEND and TRECV communication thing. It transmits a byte over TCP and it works.

Aggi answered 28/5, 2012 at 10:18 Comment(0)
L
6

I would go with SNAP7 and python-snap7 libraries. Example on how to install it on a Raspberry pi found in this blog: http://simplyautomationized.blogspot.com/2014/12/raspberry-pi-getting-data-from-s7-1200.html

Update:

Video walkthrough on how to set it up: https://youtu.be/yJNEsI5KJxs

Lucullus answered 14/12, 2014 at 6:16 Comment(0)
A
5

After failing with libnodave and OPC, I created a TCON,TSEND and TRECV communication thing. It transmits a byte over TCP and it works.

Aggi answered 28/5, 2012 at 10:18 Comment(0)
B
4

I would use libnodave on PC side to change value of some M tag or a DB tag in a PLC. You can easily make a ladder rung which will monitor this tag and execute further actions. Although libnodave doesn't support Python natively, you can find a python wrapper here.

Balzer answered 30/4, 2012 at 9:50 Comment(5)
I don't suppose you have an example of the wrapper in use?Aggi
I've tried various solutions with libnodave and all of them return in a -1 from connectPLC(). Someone on the internet claims it is because a change in the S7-1200 TCP layer, and I don't know enough C to change that in the dll.Aggi
If I remember well testISO_TCP.exe was working fine. I don't know about other connecting methods. I was just aware of Python wrapper, I didn't use it. I use libnodave with great success in Linux (GCC and FreePascal) and in Windows (Delphi, Lazarus/FPC). I don't have any S7 1200 to test it again, but someone already did with Android: automation-se.de/index.php/s7droid/s7droidlite.Balzer
testISO_TCP.exe worked fine, however studying the source code and writing the exact same thing in python, it returned -1. I couldn't even establish a connection to the PLC. After a week or so of trying or failing, my teacher told me to not waste anymore time. Anyway the project is delivered now and I've learned a lot from trying to make these things work, so you deserve that +1. In the future I'll try to avoid anything made by Siemens.Aggi
@Megimoo, a lot of things improved over time, even in the S7 world. (not a sponsored comment ;p)Pageantry
C
2

Well, a quick google of 'python rs232' turned up some site talking about doing robotics controls through rs232 using pySerial. So if you know how to hook your widget up to rs232, you could go that route.

Cockrell answered 27/4, 2012 at 19:46 Comment(0)
G
2

There is a commercial library called "S7connector" by Rothenbacher GmbH (obviously it's not the "s7connector" on sourceforge). It is for the .NET framework, so could be used with IronPython.

It does work with S7-1200 PLCs. You just have to make sure a DB you want to read from / write to is not an optimized S7-1200 style DB, but a S7-300/400 compatible one, an option which you can set when creating a DB in TIA portal. This lib also allows to read and write all I/O ports - the "shadow registers" (not sure what they're called officially) and directly as well, overriding the former.

Giggle answered 5/6, 2014 at 9:20 Comment(0)
P
2

There are several Modbus TCP Clients available for Python, I have used pyModbusTCP successfully. TIA Portal the IDE for the S7-1200 has Modbus Server function that you can load to your PLC that will allow you to read and write directly to the PLC.

https://support.industry.siemens.com/cs/ww/en/view/83130159

Provisory answered 6/9, 2016 at 20:56 Comment(0)
L
1

Ther best way to communicate with S7-1200 PLC cpu's is with OPC UA or Classic OPC (ommonly known as OPC DA. ) Libnodave is made for S7-300 and S7-400 not for S71200 (2.x firmware).

If you use a third party solution to communicate with S7-1200 (or S7-1500) you have to decrease the security level at the PLC by allowing the put and get mechanism. Put and get are pure evil to use. You open the memory of the CPU for every process. Don’t use them anymore. Siemens should actually block this.

This applies for all firmware release for S7-1200. Siemens pushes people you use OPC UA as default communication from PLC. What makes sense, because OPC UA is the protocol for industry 4.0 and IIoT.

Edit: rewrite everything. Info was heavily outdated. If you use a firmware 2 or 3 1200, consider replacement or upgrade. These versions are no longer supported and contains the worm issue.

Lagunas answered 27/5, 2012 at 10:57 Comment(3)
"There is no way that you can communicate with the S7-1200 PLC cpu's without OPC" Not true. See my answer.Giggle
We are two firmware versions further then when this answer was made. OPC is also finally official released for the S7-1200 with non-optimised access only.Lagunas
This answer relates to S7-1200 firmware V2.2 (Oct 2011) or older releases. Enjoy your cleanup badge ;)Pageantry

© 2022 - 2024 — McMap. All rights reserved.