python-snap7使用说明

本文介绍了如何使用Python的snap7库连接和操作Siemens PLC。首先,将snap7.dll和snap7.lib文件放入Python环境路径。接着,安装python-snap7库。在程序中,需正确设置connect的机架号和槽号,并利用read_area或db_read方法读取数据。返回的数据是字节流,需通过get_int和get_real解析。注意区域定义如'PE'、'PA'等,以及read_area方法的参数含义。
摘要由CSDN通过智能技术生成

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
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值