<p>经过一些尝试和错误的实验,以及snap7中涉及的一些开发人员的信息,我修复了这个问题。snap7.dll和.lib文件所在的文件夹必须存在于Windows的环境变量中。另外,如果从Python安装程序中选中了“add path”选项,则可以将文件复制到Python install目录。</p>
<p>详见图片:编辑环境变量</p>
<p><a href="http://i.stack.imgur.com/mwaLI.png" rel="nofollow">edit enviroment vars</a></p>
<p>为了给像我这样的新手提供一个良好的起点,这里有一个简单的snap7教程,从一个带有Python3的S7 1212C PLC读取DB的变量:</p>
<pre><code>import snap7
from snap7.util import *
import struct
plc = snap7.client.Client()
plc.connect("10.112.115.10",0,1)
#---Read DB---
db = plc.db_read(1234,0,14)
real = struct.iter_unpack("!f",db[:12] )
print( "3 x Real Vars:", [f for f, in real] )
print( "3 x Bool Vars:", db[12]&1==1, db[12]&2==2, db[12]&4==4 )
plc.disconnect()
</code></pre>
<h2>IP和子网掩码</h2>
<p>PLC的IP必须在PC LAN设备的子网掩码范围内。如果LAN设备的IP为10.112.115.1,子任务为255.255.255.0,则您的PLC的范围为10.112.115.2到10.112.115.255。超出此范围的每个可编程逻辑控制器IP都会给您一个“无法访问的对等”错误。</p>
<h2>防火墙</h2>
<p>确保防火墙允许PC和PLC之间的通信。</p>
<h2>可编程逻辑控制器数据位置</h2>
<p>如果您不喜欢STEP 7/TIA门户。查找“联机诊断”按钮并查看图片以查找数据的位置。</p>
<p><a href="http://i.stack.imgur.com/X0zXH.png" rel="nofollow">DB Number and Variable Offsets</a></p>
<h2>可编程逻辑控制器配置</h2>
<p>除了使用要读取的变量的可编程逻辑控制器程序外,可编程逻辑控制器不需要其他部件来与snap7通信。与snap7通信所需的服务由打开电源的固件启动。</p>