Skip to main content

A Python library interacting with Siemens S7 PLC datablocks

Project description

S7DataBlock

Introduction

A Python library for parsing Siemens S7 PLC data blocks, in conjunction with Snap7.

s7datablock is a Python library designed to parse and interpret Siemens S7 PLC data blocks. It works in conjunction with the python-snap7 library, providing a set of tools and utilities to read and write the bytes in a PLC datablock as structured data, using the DB definitions as exported from a TIA Portal project. This makes it easier for developers and engineers to work with Siemens S7 PLCs in their Python applications.

Instead of manually mapping bytes, the dataformat defined in Tia Portal can be used to generate the appropriate Python mapping. The configuration file can be kept separate from the code, making it easier to track versions and support complicated data structures.

Installation

pip install s7datablock

Parsing Data Blocks

In this example, we will demonstrate how to use the s7datablock library to parse and interpret Siemens S7 PLC data blocks.

First, we define a s7_1200_out user-defined type (UDT) and a data block in Structured Control Language (SCL). The UDT, named "s7_1200_out_udt", consists of five boolean variables and one integer variable. The data block, named "s7_1200_out", uses this UDT. This file was defined in and exported from Tia Portal, using the generate source from blocks, including all dependent blocks option.

# s7_1200_out.db
TYPE "s7_1200_out_udt"
VERSION : 0.1
   STRUCT
      PLC_DQ_0 : Bool;  # Boolean variable
      PLC_DQ_1 : Bool;  # Boolean variable
      PLC_DQ_2 : Bool;  # Boolean variable
      PLC_DQ_3 : Bool;  # Boolean variable
      PLC_DQ_4 : Bool;  # Boolean variable
      SB_AQ_0 : Int;    # Integer variable
   END_STRUCT;
END_TYPE

DATA_BLOCK "s7_1200_out"
{ S7_Optimized_Access := 'FALSE' }
VERSION : 0.1
NON_RETAIN
"s7_1200_out_udt"

BEGIN

END_DATA_BLOCK

Next, we use the S7DataBlock class from the s7datablock library to parse the data block definition file. We then display the parsed data block and its initial buffer state. Finally, we modify one of the boolean variables in the data block and display the updated buffer state.

# Import necessary modules
from s7datablock.mapping import S7DataBlock
from pathlib import Path

# Parse the data block definition file
db1200 = S7DataBlock.from_definition_file(
    Path("tests/definitions/s7_1200_out.db"),
    db_number=1200
)

# The printing the db1200 datasrutcure produces a nicely formatted output
print(db1200)
                   Data Block Structure
┏━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━┓
┃ Name             ┃ Data type   ┃   Offset ┃ Value       ┃
┡━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━┩
│ PLC_DQ_0         │ Bool        │      0.0 │ False       │
│ PLC_DQ_1         │ Bool        │      0.1 │ False       │
│ PLC_DQ_2         │ Bool        │      0.2 │ False       │
│ PLC_DQ_3         │ Bool        │      0.3 │ False       │
│ PLC_DQ_4         │ Bool        │      0.4 │ False       │
│ SB_AQ_0          │ Int         │      2.0 │ 0           │
│ TIMEFIELD        │ DTL         │      4.0 │ None        │
│   YEAR           │ UInt        │      4.0 │ 0           │
│   MONTH          │ USInt       │      6.0 │ 0           │
│   DAY            │ USInt       │      7.0 │ 0           │
│   WEEKDAY        │ USInt       │      8.0 │ 0           │
│   HOUR           │ USInt       │      9.0 │ 0           │
│   MINUTE         │ USInt       │     10.0 │ 0           │
│   SECOND         │ USInt       │     11.0 │ 0           │
│   NANOSECOND     │ UDInt       │     12.0 │ 0           │
└──────────────────┴─────────────┴──────────┴─────────────┘
             Total size: 16 bytes
# db1200 maps the data like a dictionary but the hood it stores the data in a continuous buffer,
# using the same format and byte ordering as the PLC
# Initially the buffer is only zeros
print(db1200.buffer)
# Output: bytearray(b'\x00\x00\x00\x00')

# Modify one of the boolean variables
db1200["PLC_DQ_3"] = True

# Now one bit has changed
print(db1200.buffer)
# Output: bytearray(b'\x08\x00\x00\x00')

Contributing and Issues

The library is in its early stages, and contributions are welcome. Feel free to submit a pull request or open an issue on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

s7datablock-0.0.0a0.tar.gz (39.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

s7datablock-0.0.0a0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file s7datablock-0.0.0a0.tar.gz.

File metadata

  • Download URL: s7datablock-0.0.0a0.tar.gz
  • Upload date:
  • Size: 39.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for s7datablock-0.0.0a0.tar.gz
Algorithm Hash digest
SHA256 664bddd6e85776713a460fd7bfb69695c4cc9e95ae94b24b61ff5aeb2d6c74e2
MD5 25145020e11193beb586e1267fcf9bba
BLAKE2b-256 48bfb8c18380750afc094ae20a7a642f6a1520bcd04244919627d0aa91e8070e

See more details on using hashes here.

Provenance

The following attestation bundles were made for s7datablock-0.0.0a0.tar.gz:

Publisher: ci.yml on CEAD-group/s7datablock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file s7datablock-0.0.0a0-py3-none-any.whl.

File metadata

  • Download URL: s7datablock-0.0.0a0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for s7datablock-0.0.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d2c9e2e8abdadf2f3e1a4b2539167614623797e0f96124bd4eaaf4edbfa00d0
MD5 a5b1cb366117131a843c15913a492d99
BLAKE2b-256 7270393118214b2ec1efeb5593e428d2f1a87d408674c59f5fd696add9322293

See more details on using hashes here.

Provenance

The following attestation bundles were made for s7datablock-0.0.0a0-py3-none-any.whl:

Publisher: ci.yml on CEAD-group/s7datablock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page