简体   繁体   English

如何在西门子 S7-1200 和 python 之间进行通信?

[英]How can I communicate between a Siemens S7-1200 and python?

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.我正在 S7-1200 plc 上运行一个进程,我需要它向我的 python 脚本发送启动信号,在脚本运行完成后,它需要向 plc 发送一些信息以启动下一阶段。 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?是否有一种快速而肮脏的方式通过 profibus 发送东西,或者我最好只使用 RS232 东西?

After failing with libnodave and OPC, I created a TCON,TSEND and TRECV communication thing.在使用 libnodave 和 OPC 失败后,我创建了一个 TCON、TSEND 和 TRECV 通信事物。 It transmits a byte over TCP and it works.它通过 TCP 传输一个字节并且它可以工作。

I would go with SNAP7 and python-snap7 libraries.我会使用 SNAP7 和 python-snap7 库。 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关于如何在此博客中找到的 Raspberry pi 上安装它的示例: 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关于如何设置的视频演练: https : //youtu.be/yJNEsI5KJxs

I would use libnodave on PC side to change value of some M tag or a DB tag in a PLC.我会在 PC 端使用libnodave来更改 PLC 中某些 M 标记或 DB 标记的值。 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 .尽管 libnodave 本身不支持 Python,但您可以在此处找到 Python 包装器。

Well, a quick google of 'python rs232' turned up some site talking about doing robotics controls through rs232 using pySerial .好吧,'python rs232' 的快速谷歌出现了一些谈论使用pySerial通过 rs232 进行机器人控制的网站 So if you know how to hook your widget up to rs232, you could go that route.因此,如果您知道如何将您的小部件连接到 rs232,您就可以走这条路。

There is a commercial library called "S7connector" by Rothenbacher GmbH (obviously it's not the "s7connector" on sourceforge). Rothenbacher GmbH 有一个名为“S7connector”的商业库(显然它不是 sourceforge 上的“s7connector”)。 It is for the .NET framework, so could be used with IronPython.它用于 .NET 框架,因此可以与 IronPython 一起使用。

It does work with S7-1200 PLCs.它适用于 S7-1200 PLC。 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.您只需确保要读取/写入的 DB 不是优化的 S7-1200 样式 DB,而是兼容 S7-300/400 的 DB,您可以在 TIA Portal 中创建 DB 时设置该选项。 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.该库还允许读取和写入所有 I/O 端口——“影子寄存器”(不确定它们的正式名称)和直接读取和写入,覆盖前者。

There are several Modbus TCP Clients available for Python, I have used pyModbusTCP successfully.有几个可用于 Python 的 Modbus TCP 客户端,我已经成功使用了 pyModbusTCP。 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. S7-1200 的 TIA Portal IDE 具有 Modbus 服务器功能,您可以将其加载到 PLC 中,从而允许您直接读取和写入 PLC。

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

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).与 S7-1200 PLC cpu 通信的最佳方式是使用 OPC UA 或 Classic OPC(通常称为 OPC DA。)Libnodave 是为 S7-300 和 S7-400 设计的,而不是为 S71200(2.x 固件)设计的。

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.如果您使用第三方解决方案与 S7-1200(或 S7-1500)通信,您必须通过允许放置和获取机制来降低 PLC 的安全级别。 Put and get are pure evil to use. Put 和 get 是纯粹的邪恶使用。 You open the memory of the CPU for every process.您为每个进程打开 CPU 的内存。 Don't use them anymore.不要再使用它们了。 Siemens should actually block this.西门子实际上应该阻止这一点。

This applies for all firmware release for S7-1200.这适用于 S7-1200 的所有固件版本。 Siemens pushes people you use OPC UA as default communication from PLC.西门子推送您使用 OPC UA 作为 PLC 的默认通信的人。 What makes sense, because OPC UA is the protocol for industry 4.0 and IIoT.这是有道理的,因为 OPC UA 是工业 4.0 和 IIoT 的协议。

Edit: rewrite everything.编辑:重写一切。 Info was heavily outdated.信息严重过时。 If you use a firmware 2 or 3 1200, consider replacement or upgrade.如果您使用固件 2 或 3 1200,请考虑更换或升级。 These versions are no longer supported and contains the worm issue.这些版本不再受支持并且包含蠕虫问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM