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.


spec/DB layout

# Byte index    Variable name  Datatype
4             ID             INT
6             NAME           STRING[6]

12.0          testbool1      BOOL
12.1          testbool2      BOOL
12.2          testbool3      BOOL
12.3          testbool4      BOOL
12.4          testbool5      BOOL
12.5          testbool6      BOOL
12.6          testbool7      BOOL
12.7          testbool8      BOOL
13            testReal       REAL
17            testDword      DWORD

client = snap7.client.Client()
client.connect('', 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)


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

    17+2,                   # size of the specification 17 is start
                            # of last value
                            # which is a DWORD which is 2 bytes,

    1,                      # number of row's / specifications

    id_field='ID',          # field we can use to identify a row.
                            # default index is used
    layout_offset=4,        # sometimes specification does not start a 0
                            # like in our example
    db_offset=0             # At which point in 'all_data' should we start
                            # reading. if could be that the specification
                            # does not start at 0

Now we can use db1 in python as a dict. if 'ID' contains
the 'test' we can identify the 'test' row in the all_data bytearray

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

If we do not specify a id_field this should work to read out the
same data.


to read and write a single Row from the plc. takes like 5ms!


class snap7.util.DB(db_number, _bytearray, specification, row_size, size, id_field=None, db_offset=0, layout_offset=0, row_offset=0)[source]

Manage a DB bytearray block given a specification of the Layout.

It is possible to have many repetitive instances of a specification this is called a “row”.

probably most usecases there is just one row

db1[0][‘testbool1’] = test db1.write() # puts data in plc

class snap7.util.DB_Row(_bytearray, _specification, row_size=0, db_offset=0, layout_offset=0, row_offset=0)[source]

Provide ROW API for DB bytearray


export dictionary with values


return bytearray from self or DB parent


Calculate correct beginning position for a row the db_offset = row_size * index


read current data of db row from plc


Write current data to db in plc

snap7.util.get_bool(_bytearray, byte_index, bool_index)[source]

Get the boolean value from location in bytearray

snap7.util.get_int(_bytearray, byte_index)[source]

Get int value from bytearray.

int are represented in two bytes

snap7.util.get_real(_bytearray, byte_index)[source]

Get real value. create float from 4 bytes

snap7.util.get_string(_bytearray, byte_index, max_size)[source]

parse string from bytearray


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)[source]

Set boolean value on location in bytearray

snap7.util.set_int(_bytearray, byte_index, _int)[source]

Set value in bytearray to int

snap7.util.set_real(_bytearray, byte_index, real)[source]

Set Real value

make 4 byte data from real

snap7.util.set_string(_bytearray, byte_index, value, max_size)[source]

Set string value

Params value:string data
Params max_size:
 max possible string size