Connecting to PLCs

This page shows how to connect to different Siemens PLC models using python-snap7.

Rack/Slot Reference

PLC Model

Rack

Slot

Notes

S7-300

0

2

S7-400

0

3

May vary with multi-rack configurations

S7-1200

0

1

PUT/GET access must be enabled in TIA Portal

S7-1500

0

1

PUT/GET access must be enabled in TIA Portal

S7-200 / Logo

Use set_connection_params with TSAP addressing

Warning

S7-1200 and S7-1500 PLCs ship with PUT/GET communication disabled by default. You must enable PUT/GET in TIA Portal under the CPU properties to use the legacy S7 protocol.

S7-300

from snap7 import Client

client = Client()
client.connect("192.168.1.10", 0, 2)

S7-400

from snap7 import Client

client = Client()
client.connect("192.168.1.10", 0, 3)

S7-1200 / S7-1500

from snap7 import Client

client = Client()
client.connect("192.168.1.10", 0, 1)

S7-200 / Logo (TSAP Connection)

S7-200 and Logo PLCs require TSAP addressing:

from snap7 import Client

client = Client()
client.set_connection_params("192.168.1.10", 0x1000, 0x2000)
client.connect("192.168.1.10", 0, 0)

Using a Non-Standard Port

from snap7 import Client

client = Client()
client.connect("192.168.1.10", 0, 1, tcp_port=1102)

Routing (Multi-Subnet Access)

Warning

Routing support is experimental and may change in future versions.

When the target PLC sits on a different subnet behind a gateway PLC, use connect_routed to let the gateway forward the connection:

from snap7 import Client

client = Client()
client.connect_routed(
    host="192.168.1.1",       # gateway PLC address
    router_rack=0,            # gateway rack
    router_slot=2,            # gateway slot
    subnet=0x0001,            # target subnet ID
    dest_rack=0,              # target PLC rack
    dest_slot=3,              # target PLC slot
)
data = client.db_read(1, 0, 4)
client.disconnect()