python snap7 windows - can't find snap7 library

Lavalu picture Lavalu · Nov 13, 2015 · Viewed 14.5k times · Source

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:

  1. Download snap7 from sourceforge and copy snap7.dll and snap7.lib to system32 folder of windows 8
  2. Install wrapper by using pip install python-snap7

How to install snap7 on windows correctly ?

[log of pip install][1]

Answer

Lavalu picture Lavalu · Nov 20, 2015

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.