问题
i try to install snap7 (to read from a S7-1200) with it's python-snap7 0.4 wrapper but i get always a traceback with the following simple code.
from time import sleep
import snap7
from snap7.util import *
import struct
plc = snap7.client.Client()
Traceback:
>>>
Traceback (most recent call last):
File "Y:\Lonnox\Projekte\Bibliothek\Python und SPS\S7-1200 Test.py", line 6, in <module>
plc = snap7.client.Client()
File "C:\Python34\lib\site-packages\snap7\client.py", line 30, in __init__
self.library = load_library()
File "C:\Python34\lib\site-packages\snap7\common.py", line 54, in load_library
return Snap7Library(lib_location).cdll
File "C:\Python34\lib\site-packages\snap7\common.py", line 46, in __init__
raise Snap7Exception(msg)
snap7.snap7exceptions.Snap7Exception: can't find snap7 library. If installed, try running ldconfig
The steps i do to install snap7 and python wrapper are:
- Download snap7 from sourceforge and copy snap7.dll and snap7.lib to system32 folder of windows 8
- Install wrapper by using pip install python-snap7
How to install snap7 on windows correctly ?
[log of pip install][1]
回答1:
After some try and error experiments and with some infos of snap7 involved developers, i fixed the problem. The folder where the snap7.dll and .lib file are located must be present in the Enviroment variables of Windows. Alternative you can copy the files to the Python install dir if you have checked the "add path" option from the Python installer.
See the picture for Details: Edit Enviroment Vars
edit enviroment vars
To give a good starting point for everyone who is a greenhorn like me here is a minimal snap7 tutorial to read variables of a DB from a S7 1212C PLC with Python3:
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()
IP and Subnetmask
The IP of the PLC must be in the range of the subnetmask of the PC LAN Device. If the IP of the LAN device is 10.112.115.1 and the submask is 255.255.255.0 this give you a range of 10.112.115.2 to 10.112.115.255 for your PLC. Every PLC IP outside this range will give you a "Unreachable peer" Error.
Firewall
Make sure that your firewall allow the communication between your PC and PLC.
PLC Data Location
If you are unfamilar with STEP 7/ TIA Portal. Look for the "Online Diagnostics" Button and see the pictures to find the location of your data.
DB Number and Variable Offsets
PLC Configuration
Beside a PLC program that uses the variables you want to read, the PLC need no additional parts to communicate with snap7. The services that are needed to communicate with snap7 are started by the firmware on power on.
回答2:
Try this: Search the snap7 folder for snap7.dll and snap7.lib files Copy the snap7.dll and snap7.lib into the "C:/PythonXX/site-packages/snap7 " directory and run you code again. You can figure out this in the common.py file in the same directory.
回答3:
The latest setup to use snap7 looks as follows for me:
install snap7 for python with pip in the command line by "pip install python-snap7"
download the latest snap7 package from sourceforge
copy the 32 or 64bit version to any folder, for example your project folder
- do an import snap7 in your python program
temporarily edit your enviroment variables in your python program
#---Temporarily Change The Path Enviroment Variable For Snap7.dll--- if not snapPath in os.environ["PATH"]: os.environ["PATH"] = os.environ["PATH"] + ";" + snapPath.replace("/","\\")
Spaces in the path are allowed. This works great although if your create installer for example with xcfreeze.
来源:https://stackoverflow.com/questions/33697263/python-snap7-windows-cant-find-snap7-library