Skip to main content

S7DataBlock

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

s7datablock is a Python utility designed to parse and interpret Siemens S7 PLC data blocks using structured data definitions exported from the TIA Portal. It works seamlessly with the excellent python-snap7 library to enable high-level, dictionary-like access to binary PLC data.

Instead of manually mapping byte offsets and types, you can use .db definition files exported from TIA Portal to generate a clean, versionable Python representation of your data block structures.


Key Features

  • Uses exported TIA Portal *.db files for accurate structure mapping
  • Read and write entire PLC data blocks with .GET() and .SET() (or the more pythonic pull() and push())
  • Structure and sub-structure access via dot notation
  • Clean terminal output using Rich
  • Easy integration into polling and monitoring loops

example.png


Data Transfer

This library deliberately uses manual .GET() and .SET() calls instead of automatic syncing to prioritize:

  • Clarity – Developers control exactly when data is transferred
  • Performance – Avoid unnecessary communication overhead
  • Atomicity – Batch multiple updates into one write

Live Monitoring PLC Values

You can monitor PLC values by creating a polling loop. Here's an example using Rich for clean terminal output:

from time import sleep
from rich.live import Live
from datetime import datetime
from io import StringIO

from s7datablock.mapping import S7DataBlock

# Example in-line definition file contents. Alternatively, you can pass a PAth to a *.db file.
df_file_contents = StringIO(
    """
        TYPE "my_udt"
        VERSION : 0.1
        STRUCT
            PLC_DQ_0 : Bool;
            PLC_DQ_1 : Bool;
            WHEN : DTL;
            Value1 : Int;
            Value2 : Real;
        END_STRUCT;
        END_TYPE
        DATA_BLOCK "my_udt"
        VERSION : 0.1
        "my_udt"
        BEGIN
        END_DATA_BLOCK
    """
)

db = S7DataBlock.from_definition_file(df_file_contents, db_number=1200)
print(db) # This will print the structure of the data block as a table

print(repr(db))  # Print the raw representation of the data block
# In a real application, you would create a snap7 client and connect to the PLC

# client = snap7.client.Client()
# client.connect('192.168.0.1', 0, 1)

with Live(db.to_table(), refresh_per_second=10) as live:
    while True:
        # In a real application, you would call GET() here
        # db.GET(client)

        # For demo, we'll just modify some values
        db["WHEN"] = datetime.now()
        db["PLC_DQ_0"] = not db["PLC_DQ_0"]  # Toggle the boolean
        db["Value1"] = db["Value1"] + 1  # Increment the integer value
        db["Value2"] = db["Value2"] + 0.1  # Increment the real value

        # Update the display with current values
        live.update(db.to_table())
        sleep(0.2)

Running this code should render a live table in your terminal

                        Data Block Structure
┏━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┓
┃ Name              ┃ Data type   ┃   Offset ┃ Value               ┃
┡━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━┩
│ PLC_DQ_0          │ Bool        │      0.0 │ True                │
│ PLC_DQ_1          │ Bool        │      0.1 │ False               │
│ WHEN              │ DTL         │      2.0 │ 2025-07-14 17:08:40 │
│   WHEN.YEAR       │ UInt        │      2.0 │ 2025                │
│   WHEN.MONTH      │ USInt       │      4.0 │ 7                   │
│   WHEN.DAY        │ USInt       │      5.0 │ 14                  │
│   WHEN.WEEKDAY    │ USInt       │      6.0 │ 1                   │
│   WHEN.HOUR       │ USInt       │      7.0 │ 17                  │
│   WHEN.MINUTE     │ USInt       │      8.0 │ 8                   │
│   WHEN.SECOND     │ USInt       │      9.0 │ 40                  │
│   WHEN.NANOSECOND │ UDInt       │     10.0 │ 568441000           │
│ Value1            │ Int         │     14.0 │ 757                 │
│ Value2            │ Real        │     16.0 │ 75.699              │
└───────────────────┴─────────────┴──────────┴─────────────────────┘

Installation

pip install s7datablock

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.

Metadata

Release files for s7datablock 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for s7datablock 0.0.3
File Size Uploaded Provenance Actions
s7datablock-0.0.3.tar.gz 291.5 kB With provenance Details for s7datablock-0.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for s7datablock 0.0.3
File Interpreter ABI Platform Actions
s7datablock-0.0.3-py3-none-any.whl Python 3 none any Details for s7datablock-0.0.3-py3-none-any.whl

Total release size: 305.8 kB

Release files / s7datablock-0.0.3.tar.gz

Download URL s7datablock-0.0.3.tar.gz
Size 291.5 kB
Tags Source
SHA-256 checksum
How to use checksums
494345586460ce8efda65ab9c6a95c360232ce27201902848ec9e812c9138e1f
BLAKE2b-256 checksum
How to use checksums
b8d5b8e0e83cd57b5cde998dac41f8c41db133bfb0621ac4005819fa900624b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.

Transparency log

Release files / s7datablock-0.0.3-py3-none-any.whl

Download URL s7datablock-0.0.3-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4bc43aead8a0e289c696437930702e4dac03e0ef76425b5025336802aa9dfcdf
BLAKE2b-256 checksum
How to use checksums
91e3bc125865f635ee7f062dc372d2f41fa35ef2b2d6189869f22ea2fbc53091
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.

Transparency log

Security for s7datablock 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for s7datablock-0.0.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page