Python Snap7
Python Snap7
Release 0.0rc0
1 Introduction 3
2 Installation 5
2.1 Snap7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 snap7-python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 development 7
3.1 Github . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Test suite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 Credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4 Client 9
5 Server 13
6 Partner 15
7 Util 17
i
ii
python-snap7 Documentation, Release 0.0rc0
Contents:
Contents 1
python-snap7 Documentation, Release 0.0rc0
2 Contents
CHAPTER 1
Introduction
python-snap7 is a Python wrapper for the Snap7 library. Snap7 i san open source, 32/64 bit, multi-platform Ethernet
communication suite for interfacing natively with Siemens S7 PLCs.
Python-snap7 is developer for snap7 1.1.0 and Python2.7. It is tested on Windows (8.1 64 bit) and Linux, but it may
work on other operating systems. Python 2.6 and Python 3+ may work, but are not fully tested yet.
The project development is centralized on github.
3
python-snap7 Documentation, Release 0.0rc0
4 Chapter 1. Introduction
CHAPTER 2
Installation
here you can find out how to install python-snap7 on your system.
Snap7
Ubuntu
If you are using Ubuntu you can use the Ubuntu packages from our launchpad PPA. To install:
$ sudo add-apt-repository ppa:gijzelaar/snap7
$ sudo apt-get update
$ sudo apt-get install libsnap71 libsnap7-dev
Windows
Download the zip file from the sourceforce page. Unzip the zip file, and copy* releaseWin-
dows<Win64/Win32>snap7.dll* somewhere in you system PATH, for example C:WINDOWSsystem32. Alternatively
you can copy the file somewhere on your file system and adjust the system PATH.
If you are not using Ubuntu or if you want to have more control you can download the latest source from the sourceforce
page and do a manual compile. Download the file and run:
$ p7zip -d snap7-full-1.0.0.7z # requires the p7 program
$ cd build/<platform> # where platform is unix or windows
$ make -f <arch>.mk # where arch is your architecture, for example x86_64_
linux
5
python-snap7 Documentation, Release 0.0rc0
For more information about or help with compilation please check out the documentation on the snap7 website.
snap7-python
You can also install it from the git repository or from a source tarball:
6 Chapter 2. Installation
CHAPTER 3
development
Github
We develop python-snap7 on github. If you have any problems with python-snap7 please raise an issue in the issue
tracker. Even better is if you have a solution to problem! In that case you can make our live easier by following these
steps:
fork our repository on github
Add a tests that will fail because of the problem
Fix the problem
Run the test suite again
Commit to your repository
Issue a github pullrequest.
Also we try to be as much pep8 compatible as possible, where possible and reasonable.
Test suite
python-snap7 comes with a test suite with 100% coverage. This test suite verifies that the code actually works and
makes development much easier. To run all tests please run from the source:
$ ./run_tests.sh
Note that some tests require to run as root, since snap7 needs to bind on a privileged TCP port.
If the test complain about missing Python modules make sure the source directory is in your PYTHONPATH environ-
ment variable, or the python-snap7 module is installed.
7
python-snap7 Documentation, Release 0.0rc0
Credits
8 Chapter 3. development
CHAPTER 4
Client
9
python-snap7 Documentation, Release 0.0rc0
10 Chapter 4. Client
python-snap7 Documentation, Release 0.0rc0
11
python-snap7 Documentation, Release 0.0rc0
12 Chapter 4. Client
CHAPTER 5
Server
13
python-snap7 Documentation, Release 0.0rc0
14 Chapter 5. Server
CHAPTER 6
Partner
15
python-snap7 Documentation, Release 0.0rc0
get_last_error()
Returns the last job result.
get_param(number)
Reads an internal Partner object parameter.
get_stats()
Returns some statistics.
Returns a tuple containing bytes send, received, send errors, recv errors
get_status()
Returns the Partner status.
get_times()
Returns the last send and recv jobs execution time in milliseconds.
set_recv_callback()
Sets the user callback that the Partner object has to call when a data packet is incoming.
set_send_callback()
Sets the user callback that the Partner object has to call when the asynchronous data sent is complete.
stop()
Stops the Partner, disconnects gracefully the remote partner.
snap7.partner.error_wrap(func)
Parses a s7 error code returned the decorated function.
16 Chapter 6. Partner
CHAPTER 7
Util
This module contains utility functions for working with PLC DB objects. There are functions to work with the raw
bytearray data snap7 functions return In order to work with this data you need to make python able to work with the
PLC bytearray data.
For example code see test_util.py and example.py in the example folder.
example:
spec/DB layout
client = snap7.client.Client()
client.connect('192.168.200.24', 0, 3)
# this looks confusing but this means uploading from the PLC to YOU
# so downloading in the PC world :)
all_data = client.upload(db_number)
17
python-snap7 Documentation, Release 0.0rc0
simple:
db1 = snap7.util.DB(
db_number, # the db we use
all_data, # bytearray from the plc
layout, # layout specification DB variable data
# A DB specification is the specification of a
# DB object in the PLC you can find it using
# the dataview option on a DB object in PCS7
To test of you layout matches the data from the plc you can
just print db1[0] or db['test'] in the example
db1['test']['testbool1'] = 0
db1[0]['testbool1']
to read and write a single Row from the plc. takes like 5ms!
db1['test'].write()
db1['test'].read()
18 Chapter 7. Util
python-snap7 Documentation, Release 0.0rc0
get_bytearray()
return bytearray from self or DB parent
get_offset(byte_index)
Calculate correct beginning position for a row the db_offset = row_size * index
read(client)
read current data of db row from plc
write(client)
Write current data to db in plc
snap7.util.get_bool(_bytearray, byte_index, bool_index)
Get the boolean value from location in bytearray
snap7.util.get_int(_bytearray, byte_index)
Get int value from bytearray.
int are represented in two bytes
snap7.util.get_real(_bytearray, byte_index)
Get real value. create float from 4 bytes
snap7.util.get_string(_bytearray, byte_index, max_size)
parse string from bytearray
snap7.util.parse_specification(db_specification)
Create a db specification derived from a dataview of a db in which the byte layout is specified
snap7.util.set_bool(_bytearray, byte_index, bool_index, value)
Set boolean value on location in bytearray
snap7.util.set_int(_bytearray, byte_index, _int)
Set value in bytearray to int
snap7.util.set_real(_bytearray, byte_index, real)
Set Real value
make 4 byte data from real
snap7.util.set_string(_bytearray, byte_index, value, max_size)
Set string value
Params value string data
Params max_size max possible string size
19
python-snap7 Documentation, Release 0.0rc0
20 Chapter 7. Util
CHAPTER 8
genindex
modindex
search
21
python-snap7 Documentation, Release 0.0rc0
s
snap7.client, 9
snap7.partner, 15
snap7.server, 13
snap7.util, 17
23
python-snap7 Documentation, Release 0.0rc0
25
python-snap7 Documentation, Release 0.0rc0
R
read() (snap7.util.DB_Row method), 19
read_area() (snap7.client.Client method), 10
read_multi_vars() (snap7.client.Client method), 10
S
Server (class in snap7.server), 13
set_bool() (in module snap7.util), 19
set_connection_params() (snap7.client.Client method),
11
set_connection_type() (snap7.client.Client method), 11
set_int() (in module snap7.util), 19
set_real() (in module snap7.util), 19
set_recv_callback() (snap7.partner.Partner method), 16
set_send_callback() (snap7.partner.Partner method), 16
set_string() (in module snap7.util), 19
snap7.client (module), 9
snap7.partner (module), 15
snap7.server (module), 13
snap7.util (module), 17
stop() (snap7.partner.Partner method), 16
U
upload() (snap7.client.Client method), 11
W
write() (snap7.util.DB_Row method), 19
26 Index