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 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 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 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.
- async get_order_code() S7OrderCode[source]
Get module order code and firmware version (SZL 0x0011).
- async get_protection() S7Protection[source]
Get protection settings (SZL 0x0232).
- 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 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 upload(block_num: int) bytearray[source]
Upload block from PLC (3-step: START_UPLOAD, UPLOAD, END_UPLOAD).