天天看点

python连接S7-1200python连接S7-1200

python连接S7-1200

python连接S7-1200

先安装snap7 pip install snap7

python连接S7-1200python连接S7-1200
python连接S7-1200python连接S7-1200

import snap7

from snap7.util import *

import time

PLC= snap7.client.Client()

PLC.connect(‘192.168.1.120’,0,1)

#‘PE’: 0x81, #input 输入区

#‘PA’: 0x82, #output 输出区

#‘MK’: 0x83, #bit memory 中间存储区(M区)

#‘DB’: 0x84, #DB区

#‘CT’: 0x1C, #counters

#‘TM’: 0x1D, #Timers

if PLC.get_connected():

print(‘连接OK’)

else:

print(‘连接失败’)

a=bytearray(100)

b=bytearray(100)

i=0

while i<99:

time.sleep (0.5)

i=i+1

a[i]=0x0+i

print(i)

PLC.write_area(0x84,1,0,a)

data=PLC.read_area(0x84,1,i,2)

print (data)

python连接S7-1200python连接S7-1200