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
# Byte index Variable name Datatype
layout="""
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('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)
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
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.
db1[0]['testbool1']
to read and write a single Row from the plc. takes like 5ms!
db1['test'].write()
db1['test'].read()
-
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
-
snap7.util.
get_bool
(bytearray_: bytearray, byte_index: int, bool_index: int) → bool[source]¶ Get the boolean value from location in bytearray
-
snap7.util.
get_dint
(bytearray_: bytearray, byte_index: int)[source]¶ Get dint value from bytearray. DINT (Double integer) 32bit 4 bytes Decimal number signed L#-2147483648 to L#2147483647
-
snap7.util.
get_int
(bytearray_: bytearray, byte_index: int)[source]¶ Get int value from bytearray.
int are represented in two bytes
-
snap7.util.
get_real
(bytearray_: bytearray, byte_index: int)[source]¶ Get real value. create float from 4 bytes
-
snap7.util.
get_sint
(bytearray_: bytearray, byte_index: int) → int[source]¶ get the small int
- Args:
- bytearray_ (bytearray)
byte_index (int): index of the bytearray
- Returns:
- int: small int (-127 - 128)
-
snap7.util.
get_string
(bytearray_: bytearray, byte_index: int, max_size: int) → str[source]¶ parse string from bytearray
-
snap7.util.
get_usint
(bytearray_: bytearray, byte_index: int) → int[source]¶ get the unsigned small int from the bytearray
- Args:
- bytearray_ (bytearray)
byte_index (int): index of the bytearray
- Returns:
- int: unsigned small int (0 - 255)
-
snap7.util.
get_word
(bytearray_: bytearray, byte_index: int)[source]¶ Get word value from bytearray. WORD 16bit 2bytes Decimal number unsigned B#(0,0) to B#(255,255) => 0 to 65535
-
snap7.util.
parse_specification
(db_specification: str) → collections.OrderedDict[source]¶ Create a db specification derived from a dataview of a db in which the byte layout is specified
-
snap7.util.
set_bool
(bytearray_: bytearray, byte_index: int, bool_index: int, value: bool)[source]¶ Set boolean value on location in bytearray
-
snap7.util.
set_dint
(bytearray_: bytearray, byte_index: int, dint: int)[source]¶ Set value in bytearray to dint
-
snap7.util.
set_int
(bytearray_: bytearray, byte_index: int, _int: int)[source]¶ Set value in bytearray to int
-
snap7.util.
set_real
(bytearray_: bytearray, byte_index: int, real)[source]¶ Set Real value
make 4 byte data from real
-
snap7.util.
set_sint
(bytearray_: bytearray, byte_index: int, _int) → bytearray[source]¶ set small int
- Args:
- bytearray_ (bytearray)
byte_index (int): index of the bytearray _int (int): small int (-128 - 127)
- Returns:
- bytearray
-
snap7.util.
set_string
(bytearray_: bytearray, byte_index: int, value: str, max_size: int)[source]¶ Set string value
Params value: string data Params max_size: max possible string size
-
snap7.util.
set_usint
(bytearray_: bytearray, byte_index: int, _int: int) → bytearray[source]¶ set unsigned small int
- Args:
- bytearray_ (bytearray): bytearray
byte_index (int): index of the bytearray _int (int): positive value to set (0 - 255)
- Returns:
- bytearray: bytearray of the db