AsyncClient

Warning

The AsyncClient is experimental. The API may change in future releases. If you encounter problems, please open an issue.

The AsyncClient provides a native asyncio interface for communicating with Siemens S7 PLCs. It has feature parity with the synchronous Client and is safe for concurrent use via asyncio.gather().

Quick start

import asyncio
import snap7

async def main():
    async with snap7.AsyncClient() as client:
        await client.connect("192.168.1.10", 0, 1)
        data = await client.db_read(1, 0, 4)
        print(data)

asyncio.run(main())

Concurrent reads

An internal asyncio.Lock serialises each send/receive cycle so that multiple coroutines can safely share a single connection:

results = await asyncio.gather(
    client.db_read(1, 0, 4),
    client.db_read(1, 10, 4),
)

API reference

Native async S7 client implementation.

Uses asyncio streams for non-blocking I/O with an asyncio.Lock() to serialize send/receive cycles, ensuring safe concurrent use via asyncio.gather().

class snap7.async_client.AsyncClient[source]

Native async S7 client implementation.

Uses asyncio streams for non-blocking I/O. An internal asyncio.Lock serializes each send+receive cycle so that concurrent coroutines (e.g. via asyncio.gather) never interleave on the same TCP socket.

Examples

>>> import snap7
>>> async with snap7.AsyncClient() as client:
...     await client.connect("192.168.1.10", 0, 1)
...     data = await client.db_read(1, 0, 4)
async __aenter__() AsyncClient[source]

Async context manager entry.

async __aexit__(exc_type: Any, exc_val: Any, exc_tb: Any) None[source]

Async context manager exit.

async ab_read(start: int, size: int) bytearray[source]

Read from process output area (PA).

async ab_write(start: int, data: bytearray) int[source]

Write to process output area (PA).

async compress(timeout: int) int[source]

Compress PLC memory.

async connect(address: str, rack: int, slot: int, tcp_port: int = 102) AsyncClient[source]

Connect to S7 PLC.

Parameters:
  • address – PLC IP address

  • rack – Rack number

  • slot – Slot number

  • tcp_port – TCP port (default 102)

Returns:

Self for method chaining

async copy_ram_to_rom(timeout: int = 0) int[source]

Copy RAM to ROM.

async ct_read(start: int, size: int) bytearray[source]

Read from counter area (CT).

async ct_write(start: int, size: int, data: bytearray) int[source]

Write to counter area (CT).

async db_fill(db_number: int, filler: int, size: int = 0) int[source]

Fill a DB with a filler byte.

Parameters:
  • db_number – DB number to fill

  • filler – Byte value to fill with

  • size – DB size in bytes. If 0, determined via get_block_info().

Returns:

0 on success

async db_get(db_number: int, size: int = 0) bytearray[source]

Get entire DB.

Parameters:
  • db_number – DB number to read

  • size – DB size in bytes. If 0, determined via get_block_info().

Returns:

Entire DB contents

async db_read(db_number: int, start: int, size: int) bytearray[source]

Read data from DB.

Parameters:
  • db_number – DB number to read from

  • start – Start byte offset

  • size – Number of bytes to read

Returns:

Data read from DB

async db_write(db_number: int, start: int, data: bytearray) int[source]

Write data to DB.

Parameters:
  • db_number – DB number to write to

  • start – Start byte offset

  • data – Data to write

Returns:

0 on success

async delete(block_type: Block, block_num: int) int[source]

Delete a block from PLC.

async disconnect() int[source]

Disconnect from S7 PLC.

Returns:

0 on success

async download(data: bytearray, block_num: int = -1) int[source]

Download block to PLC.

async eb_read(start: int, size: int) bytearray[source]

Read from process input area (PE).

async eb_write(start: int, size: int, data: bytearray) int[source]

Write to process input area (PE).

async full_upload(block_type: Block, block_num: int) Tuple[bytearray, int][source]

Upload a block from PLC with header and footer info.

async get_block_info(block_type: Block, db_number: int) TS7BlockInfo[source]

Get block information.

get_connected() bool[source]

Check if client is connected.

async get_cp_info() S7CpInfo[source]

Get communication processor info (SZL 0x0131).

async get_cpu_info() S7CpuInfo[source]

Get CPU component identification (SZL 0x001C).

async get_cpu_state() str[source]

Get CPU state (running/stopped).

async get_order_code() S7OrderCode[source]

Get module order code and firmware version (SZL 0x0011).

async get_plc_datetime() datetime[source]

Get PLC date/time.

async get_protection() S7Protection[source]

Get protection settings (SZL 0x0232).

async iso_exchange_buffer(data: bytearray) bytearray[source]

Exchange raw ISO PDU.

async list_blocks() BlocksList[source]

List blocks available in PLC.

async list_blocks_of_type(block_type: Block, max_count: int) List[int][source]

List blocks of a specific type.

Supports multi-packet responses.

async mb_read(start: int, size: int) bytearray[source]

Read from marker/flag area (MK).

async mb_write(start: int, size: int, data: bytearray) int[source]

Write to marker/flag area (MK).

async plc_cold_start() int[source]

Cold start PLC CPU.

async plc_hot_start() int[source]

Hot start PLC CPU.

async plc_stop() int[source]

Stop PLC CPU.

async read_area(area: Area, db_number: int, start: int, size: int) bytearray[source]

Read data from memory area.

Automatically splits into multiple requests if size exceeds PDU capacity.

async read_multi_vars(items: List[dict[str, Any]]) Tuple[int, list[bytearray]][source]

Read multiple variables (sequentially, one read_area per item).

Parameters:

items – List of item dicts with keys: area, db_number, start, size

Returns:

Tuple of (result_code, list_of_bytearrays)

async read_szl(ssl_id: int, index: int = 0) S7SZL[source]

Read SZL (System Status List).

Supports multi-packet responses.

async read_szl_list() bytes[source]

Read list of available SZL IDs.

async set_plc_datetime(dt: datetime) int[source]

Set PLC date/time.

async set_plc_system_datetime() int[source]

Set PLC time to system time.

async tm_read(start: int, size: int) bytearray[source]

Read from timer area (TM).

async tm_write(start: int, size: int, data: bytearray) int[source]

Write to timer area (TM).

async upload(block_num: int) bytearray[source]

Upload block from PLC (3-step: START_UPLOAD, UPLOAD, END_UPLOAD).

async write_area(area: Area, db_number: int, start: int, data: bytearray) int[source]

Write data to memory area.

Automatically splits into multiple requests if data exceeds PDU capacity.

async write_multi_vars(items: List[dict[str, Any]]) int[source]

Write multiple variables (sequentially, one write_area per item).

Parameters:

items – List of item dicts with keys: area, db_number, start, data

Returns:

0 on success