python-snap7使用说明

1.首先从官网下载完整包,解压后从\snap7-full-1.4.2\snap7-full-1.4.2\release\Windows\Win64文件夹把snap7.dll文件和snap7.lib文件拷入已经注册环境变量Path的路径,可以是python.exe文件或库的路径。

2.安装python-snap7

3.安装完毕后,先导入库,常见的有

import snap7
from snap7.types import *
from snap7.util import *

4.完整程序如下:

import snap7
from snap7.types import *
from snap7.util import *

plc = snap7.client.Client()
plc.connect('192.168.1.108', rack=0,slot=0)

# In this example boolean in DB 31 at byte 120 and bit 5 is changed. = 120.5 

reading = plc.db_read(31, 120, 1)    # read 1 byte from db 31 staring from byte 120
snap7.util.set_bool(reading, 0, 5)   # set a value of fifth bit
plc.db_write(reading, 31, 120, 1)    # write back the bytearray and now the boolean value is changed
                                     #  in the PLC.

# NOTE you could also use the read_area and write_area fu

你可能感兴趣的:(python)