Connection

The connection module implements the ISO on TCP transport layer (TPKT/COTP) used for S7 communication.

ISO on TCP connection management (RFC 1006).

Implements TPKT (Transport Service on top of TCP) and COTP (Connection Oriented Transport Protocol) layers for S7 communication.

class snap7.connection.ISOTCPConnection(host: str, port: int = 102, local_tsap: int = 256, remote_tsap: int = 258, tpdu_size: TPDUSize = TPDUSize.S_1024)[source]

ISO on TCP connection implementation.

Handles the transport layer for S7 communication including: - TCP socket management - TPKT framing (RFC 1006) - COTP connection setup and data transfer - PDU size negotiation

__enter__() ISOTCPConnection[source]

Context manager entry.

__exit__(exc_type: Type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None) None[source]

Context manager exit.

__init__(host: str, port: int = 102, local_tsap: int = 256, remote_tsap: int = 258, tpdu_size: TPDUSize = TPDUSize.S_1024)[source]

Initialize ISO TCP connection.

Parameters:
  • host – Target PLC IP address

  • port – TCP port (default 102 for S7)

  • local_tsap – Local Transport Service Access Point

  • remote_tsap – Remote Transport Service Access Point

  • tpdu_size – TPDU size to request during COTP negotiation

check_connection() bool[source]

Check if the TCP connection is still alive.

Uses a non-blocking socket peek to detect broken connections.

connect(timeout: float = 5.0) None[source]

Establish ISO on TCP connection.

Parameters:

timeout – Connection timeout in seconds

disconnect() None[source]

Disconnect from S7 device.

receive_data() bytes[source]

Receive data from ISO connection.

Returns:

S7 PDU data

send_data(data: bytes) None[source]

Send data over ISO connection.

Parameters:

data – S7 PDU data to send

class snap7.connection.TPDUSize(*values)[source]

TPDU sizes per ISO 8073 / RFC 905.

The value is the exponent: actual size = 2^value bytes.