snap7 and pyS7 test files for s300 and s400 plcs

This commit is contained in:
Igor Barcik 2024-12-11 08:51:23 +01:00
parent a2d6cf2f9a
commit 21f6e58e2c
2 changed files with 12 additions and 9 deletions

View File

@ -2,21 +2,20 @@ import time
from pyS7 import S7Client from pyS7 import S7Client
import pprint import pprint
LOOPS = 5 LOOPS = 3
counter = 0 counter = 0
client = S7Client(address="172.16.3.231", rack=0, slot=2)
while counter < LOOPS: while counter < LOOPS:
try: try:
# Create a new 'S7Client' object to connect to S7-300/400/1200/1500 PLC. # Create a new 'S7Client' object to connect to S7-300/400/1200/1500 PLC.
# Provide the PLC's IP address and slot/rack information # Provide the PLC's IP address and slot/rack information
client = S7Client(address="172.16.3.231", rack=0, slot=1)
# client = S7Client(address="172.16.4.220", rack=0, slot=2)
# Establish connection with the PLC # Establish connection with the PLC
client.connect() client.connect()
# Define area tags to read # Define area tags to read
tags = [ tags = ["DB9,DBD0"]
"DB9,DBD0",
]
# Read the data from the PLC using the specified tag list # Read the data from the PLC using the specified tag list
data = client.read(tags=tags) data = client.read(tags=tags)

View File

@ -1,17 +1,21 @@
import time import time
import snap7 import snap7
from snap7.util.getters import get_lreal, get_dint
import pprint import pprint
LOOPS = 5 LOOPS = 20
counter = 0 counter = 0
while counter < LOOPS: while counter < LOOPS:
try: try:
plc = snap7.client.Client() plc = snap7.client.Client()
plc.connect("172.16.3.231", 0, 1) plc.connect("172.16.3.231", 0, 2)
pprint.pprint(plc.get_cpu_state()) # pprint.pprint(plc.get_cpu_state())
data = plc.db_read(9, 0, 4) # Define range of bytes to read
energy_value = get_dint(data, 0) # Read energy value
pprint.pprint(energy_value)
except Exception as e: except Exception as e:
pprint.pprint(e) pprint.pprint(e)
finally: finally:
plc.disconnect() plc.disconnect()
counter += 1 counter += 1
time.sleep(1) time.sleep(3)