How to communicate Python with Siemens PLC using Python-snap7?

5.3k Views Asked by At

I am trying to communicate Python with the CPU1212C PLC (using PLCSIM), but an error always occurs.

Code:


IP = '192.168.100.100'
RACK = 0
SLOT = 1

plc = snap7.client.Client()
plc.connect(IP, RACK, SLOT)

print(plc.get_cpu_state()) 

No handlers could be found for logger "snap7.common"

Traceback (most recent call last):
File "C:/Python27/Teste_Snap7.py", line 8, in plc.connect(IP, RACK, SLOT)
File "C:\Python27\lib\site-packages\snap7\client.py", line 25, in f check_error(code, context="client")
File "C:\Python27\lib\site-packages\snap7\common.py", line 65, in check_error raise Snap7Exception(error)
Snap7Exception: TCP : Connection timed out 

Is this a problem with Windows10?

I am using Windows10 64 bit, Python 2.7.17, Snap7 1.1.0, Python-Snap7 0.10.

I copied and pasted the snap7.dll and snap7.lib file into the System32, Python27, Python27 / site-packages / snap7 folders. And I created for each folder a path in the environment variables in an attempt to work.

I followed this tutorial: https://www.youtube.com/watch?v=BKnK4AT_WKs

4

There are 4 best solutions below

0
On

It's not a problem related with Windows, in the error message you can see the problem:

    Snap7Exception: TCP : Connection timed out.        

Verify your physical connection to the machine and then verify the client IP.

0
On

Verify in tia portal: properties > Protection and security > connection mechanism > and check the "permit acess with PUT/GET.."

0
On

Verify:

  • if PLC actually ping
  • if Snap7 server is enabled on your PC
  • if rack and slot are correct, according to your PLC.

The handbook provided with snap7 is very exhaustive, please refer to it

0
On

Update your code checking by below lines and I have experience on this.

plc = snap7.client.Client()
plc.connect(plc_ip_address, rack, slot, 102)