Skip to main content

pyS7


pyS7 is a lightweight, pure Python library that implements the Siemens S7 communication protocol over ISO-on-TCP (RFC1006). It provides operations for S7-200, S7-300, S7-400, S7-1200, and S7-1500 PLC families; actual compatibility depends on CPU and PLC configuration.

Production/Stable • 2x Performance • Fully Tested

⚠️ Neither this project nor its authors are affiliated with Siemens. S7-200, S7-300, S7-400, S7-1200, and S7-1500 are registered trademarks of Siemens AG.

ha-s7plc banner

Features

  • Pure Python – No external dependencies, easy installation across platforms
  • Intuitive API – Clean, readable code with full typing support for IDE assistance
  • Async Support – Full asyncio client (AsyncS7Client) for non-blocking I/O
  • High Performance – Optimized hot paths, 2x faster request preparation (v2.5.0)
  • Graceful error handling – read_detailed() and write_detailed() provide per-tag success/failure info
  • Strict batch writes – Batch execution with structured failures and optional best-effort rollback
  • Optimized multi-variable reads – Automatically groups contiguous tags to reduce network requests
  • Automatic chunking – Transparently splits large STRING/WSTRING reads exceeding PDU size
  • Siemens TIME – DB TIME values represented by exact datetime.timedelta values
  • CPU diagnostics – Read PLC status (RUN/STOP) and information (model, firmware) via SZL protocol
  • Broad compatibility – Supports S7-200/300/400/1200/1500 series
  • Validated protocol core – Extensive automated tests and strict type checking

Safety Notice

Industrial safety must always remain your top priority. By using pyS7 you accept full responsibility for any damage, data loss, downtime, or unintended effects. Understand your system and the implications of each operation before interacting with live equipment.

Installation

Requires Python 3.11 or later.

Upgrading from 2.x? Read the 3.0 migration guide and the evidence-based compatibility matrix.

pip install pys7

Or install from GitHub:

pip install git+https://github.com/xtimmy86x/pyS7

Quick Start

Reading data

from pyS7 import BatchWriteError, S7Client

with S7Client(address="192.168.5.100", rack=0, slot=1) as client:
    tags = [
        "DB1,X0.0",     # Bit 0 of DB1
        "DB1,SINT20",   # SINT (signed byte -128 to 127) at byte 20 of DB1
        "DB1,USINT21",  # USINT (unsigned byte 0-255) at byte 21 of DB1
        "DB1,I30",      # INT at byte 30 of DB1
        "M54.4",        # Bit 4 of marker memory
        "IW22",         # WORD at byte 22 of input area
        "QR24",         # REAL at byte 24 of output area
        "DB1,S10.5"     # String of 5 characters at byte 10 of DB1
    ]

    data = client.read(tags)
    print(data)  # [True, -50, 200, 123, True, 10, 3.14, 'Hello']

read(tags, optimize=True) is the default and supported optimized-read API for both S7Client and AsyncS7Client. In addition to merging nearby reads, it requests every BIT through its containing BYTE, coalesces bits in the same byte where possible, and masks the individual Boolean values locally in the original tag order. This avoids native single-bit reads that some PLC and configuration combinations reject with INVALID_DATA_SIZE. Set optimize=False only when native, unmerged read transport is specifically required.

Writing data

from pyS7 import S7Client

with S7Client(address="192.168.5.100", rack=0, slot=1) as client:
    tags = ["DB1,X0.0", "DB1,SINT20", "DB1,USINT21", "DB1,I30", "DB1,R40", "DB1,S10.5"]
    values = [True, -50, 200, 25000, 1.2345, "Hello"]

    client.write(tags, values)

Graceful error handling

from pyS7 import S7Client

with S7Client(address="192.168.5.100", rack=0, slot=1) as client:
    # read_detailed() continues on errors, returns per-tag results
    results = client.read_detailed(["DB1,I0", "DB99,I0", "DB1,R4"])

    for result in results:
        if result.success:
            print(f"{result.tag}: {result.value}")
        else:
            print(f"{result.tag} failed: {result.error}")

    # write_detailed() provides per-tag success/failure info
    tags = ["DB1,I0", "DB1,I2", "DB99,I0"]
    values = [100, 200, 300]
    write_results = client.write_detailed(tags, values)

    for result in write_results:
        if result.success:
            print(f"✓ {result.tag}: Written")
        else:
            print(f"✗ {result.tag}: {result.error}")

read_detailed() supplies one success or error result per requested tag and uses the same optimize behavior. It is not a separate optimized-read API.

Strict batch writes with best-effort rollback

Batch writes are not atomic. A successful result means the PLC write response reported success; pyS7 does not perform post-write read-back verification. If any result fails, commit raises BatchWriteError. Optional rollback only attempts to restore a pre-write snapshot and can fail. PLC logic, concurrent clients, or a lost connection may observe or alter partially written values before restoration.

from pyS7 import BatchWriteError, S7Client

with S7Client(address="192.168.5.100", rack=0, slot=1) as client:
    # Batch write with best-effort rollback after a reported write failure
    with client.batch_write() as batch:
        batch.add("DB1,I0", 100)
        batch.add("DB1,I2", 200)
        batch.add("DB1,R4", 3.14)
        # Auto-commits on exit, rolls back on error

    # Or with explicit control
    batch = client.batch_write(auto_commit=False)
    batch.add("DB1,I0", 100)
    batch.add("DB1,I2", 200)

    try:
        results = batch.commit()  # Returns only when every write succeeded
    except BatchWriteError as exc:
        print(f"Write failed: {exc}")
        print(exc.rollback_attempted, exc.rollback_succeeded)

Reading CPU status

from pyS7 import S7Client

with S7Client(address="192.168.5.100", rack=0, slot=1) as client:
    # Get CPU status
    status = client.get_cpu_status()
    print(f"CPU Status: {status}")  # "RUN" or "STOP"

    # Get CPU information
    info = client.get_cpu_info()
    print(f"Model: {info['module_type_name']}")
    print(f"Firmware: {info['firmware_version']}")
    print(f"Hardware: {info['hardware_version']}")

See docs/CPU_STATUS_READING.md for details.

Async client

For asyncio-based applications (SCADA dashboards, IoT, Home Assistant, FastAPI):

import asyncio
from pyS7 import AsyncS7Client

async def main():
    async with AsyncS7Client(address="192.168.5.100", rack=0, slot=1) as client:
        # Read
        values = await client.read(["DB1,I0", "DB1,R4"])
        print(values)

        # Write
        await client.write(["DB1,I0"], [42])

        # Detailed read with per-tag error handling
        results = await client.read_detailed(["DB1,I0", "DB99,I0"])
        for r in results:
            print(f"{r.tag}: {r.value if r.success else r.error}")

        # Async batch write with rollback
        async with client.batch_write() as batch:
            batch.add("DB1,I0", 100)
            batch.add("DB1,I2", 200)

        # CPU diagnostics
        status = await client.get_cpu_status()
        print(f"CPU: {status}")

asyncio.run(main())

See docs/ADVANCED_USAGE.md for concurrent patterns and advanced usage.

String data types

pyS7 uses Latin-1 (ISO-8859-1) for single-byte STRING/CHAR data and UTF-16-BE for WSTRING. ASCII text is unchanged; extended characters such as °, ü, and é are preserved in normal, optimized, and chunked transfers.

# STRING (Latin-1) - All S7 models
tags = ["DB1,S10.20"]  # STRING at byte 10, max 20 chars
data = client.read(tags)
print(data[0])  # "Heating 72°C"

# WSTRING (UTF-16-BE; PLC support/configuration dependent)
tags = ["DB1,WS100.30"]  # WSTRING at byte 100, max 30 chars
data = client.read(tags)
print(data[0])  # "Hello 世界! 🌍"

# Large strings automatically chunked if exceeding PDU size
tags = ["DB1,S100.254"]  # STRING[254] - handled transparently
data = client.read(tags)  # Complete string returned

Latin-1 is not Windows-1252: bytes 0x80–0x9F map to Latin-1 control characters, not symbols such as €. Writes containing characters outside Latin-1 raise S7AddressError instead of silently substituting ?; use WSTRING for Unicode text where supported by the PLC. See the string API reference for details.

Siemens TIME

from datetime import timedelta

value = client.read(["DB1,TIME100"])[0]  # returns timedelta
client.write(["DB1,TIME100"], [timedelta(seconds=1)])

TIME uses signed 32-bit big-endian milliseconds, with an inclusive range of −2,147,483,648 through 2,147,483,647 ms. Writes accept only timedelta values with exact millisecond precision; integers and sub-millisecond values are rejected rather than converted, rounded, or truncated. The 3.1.0 string-address contract is DB-only, using canonical syntax such as DB1,TIME100.

Monitoring and Metrics

Built-in metrics collection for monitoring PLC communication performance and diagnostics:

from pyS7 import S7Client

# Metrics enabled by default
client = S7Client(address="192.168.5.100", rack=0, slot=1)
client.connect()

# Perform operations
client.read(["DB1,I0", "DB1,R4"])
client.write(["DB1,I0"], [100])

# Access real-time metrics
print(f"Connected: {client.metrics.connected}")
print(f"Uptime: {client.metrics.connection_uptime:.1f}s")
print(f"Total operations: {client.metrics.total_operations}")
print(f"Success rate: {client.metrics.success_rate}%")
print(f"Error rate: {client.metrics.error_rate}%")
print(f"Avg read time: {client.metrics.avg_read_duration*1000:.1f}ms")
print(f"Throughput: {client.metrics.operations_per_minute:.1f} ops/min")

# Export to dict for logging/monitoring systems
metrics_dict = client.metrics.as_dict()

# Integration with Home Assistant, Prometheus, Grafana, etc.

See docs/METRICS.md for complete metrics documentation and integration examples.

Documentation

Guides

Technical Documentation

Examples

Example scripts in the examples/ directory demonstrate:

  • read_data.py – Basic reading operations
  • write_data.py – Basic writing operations
  • read_detailed_demo.py – Graceful error handling for reads
  • write_detailed_demo.py – Graceful error handling for writes
  • batch_write_demo.py – Strict batch writes with best-effort rollback
  • metrics_demo.py – Metrics collection and monitoring
  • get_cpu_status.py – CPU status monitoring
  • get_cpu_info.py – CPU information retrieval
  • read_data_tsap.py – TSAP connection example
  • bit_read_workaround.py – Bit operations
  • manage_reconnection.py – Connection handling
  • connection_state_demo.py – Connection state management
  • async_client_demo.py – Async client usage with asyncio

License

This project is distributed under the MIT License. See the LICENSE file for details.

Acknowledgements

Special thanks to filocara for the original project that inspired this work.

Metadata

Release files for pys7 3.1.2

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

Source distribution (sdist)

Source distribution for pys7 3.1.2
File Size Uploaded Provenance Actions
pys7-3.1.2.tar.gz 103.6 kB Without provenance Details for pys7-3.1.2.tar.gz

Built distribution (wheel)

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

Total release size: 167.9 kB

Release files / pys7-3.1.2.tar.gz

Download URL pys7-3.1.2.tar.gz
Size 103.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2386399da4773597cb3555b7a46c2ce156814529a1f5c3eef1e032f743d5e428
BLAKE2b-256 checksum
How to use checksums
b5431c18403a2da296cc39e8a5779a0d9a3333f297c6c33d996a00011c17ea8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.17

Release files / pys7-3.1.2-py3-none-any.whl

Download URL pys7-3.1.2-py3-none-any.whl
Size 64.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e3241e4cfe9c89683b2168d1921b875cdd09c2814298eb24c0f67026c093348e
BLAKE2b-256 checksum
How to use checksums
7229e3dd42049eb07069cb3511771cdec1d711799b0b0a1850aaf935e10e2e70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.17

Security for pys7 3.1.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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