python 2.7 读写 opc数据

运行环境 python2.7+window server2008+keep serverhtml

一、安装OpenOPCnode

   a> 下载 OpenOPC-1.3.1.win32-py2.7 (1).exe 并安装python

   b> pip安装依赖包  Pywin32 + Pyroapi

   c>  将OpenOPC安装目录下src文件夹下的OpenOPC.py复制到python安装目录下的Lib\site-packages目录下服务器

  d> 修改环境变量dom

       OPC_MODE = openpython2.7

二、链接opc服务器ui

# 导入包
import OpenOPC # 生成OpenOPC实例(Open mode) # In Open mode a connection is made to the OpenOPC Gateway Service running on the specified node. This mode is available to both Windows and non-Windows clients.
opc = OpenOPC.open_client('localhost)
# 显示可链接的opc服务器
print opc.servers() # If the OPC server is running on a different node, you can include the optional host parameter...
opc.connect('Matrikon.OPC.Simulation', 'localhost')

三、读取opc服务器数据url

taglist=['Channel_4.Device_6.Word_1','Channel_4.Device_6.Word_2', 'Channel_2.Device_3.Tag_1','Channel_2.Device_3.Tag_2', 'Channel_2.Device_3.Tag_3','Channel_4.Device_5.Tag_1', 'Channel_3.Device_4.Word_1','Channel_3.Device_4.Word_2',] # 读取一系列数据
opc_datas = opc.read(taglist) datas = [i[1] for i in opc_data] # 读取一个点
opc_data = opc.read(taglist[0]) data = opc_data[1]

四、写入opc服务器spa

# 写入一个点 # 方式1
opc.write( ('Triangle Waves.Real8', 100.0) ) # 方式2
opc['Triangle Waves.Real8'] = 100.0

# 写入多个点
opc.write( [('Triangle Waves.Real4', 10.0), ('Random.String', 20.0)] )

五、其余

# 列出可获取的opc目录
>>> opc.list() ['Simulation Items', 'Configured Aliases'] >>> opc.list('Simulation Items') ['Bucket Brigade', 'Random', 'Read Error', 'Saw-toothed Waves', 'Square Waves', 'Triangle Waves', 'Write Error', 'Write Only'] # 模糊查询
>>> opc.list('Simulation Items.Random.*Real*') ['Random.ArrayOfReal8', 'Random.Real4', 'Random.Real8'] # opc服务器信息
>>> opc.info() [('Host', 'localhost'), ('Server', 'Matrikon.OPC.Simulation'), ('State', 'Running'), ('Version', '1.1 (Build 307)'), ('Browser', 'Hierarchical'), ('Start Time', '06/24/07 13:50:54'), ('Current Time', '06/24/07 18:30:11'), ('Vendor', 'Matrikon Consulting Inc (780) 448-1010 http://www.matrikon.com')] # 关闭opc链接
opc.close()

 

官方文档地址

opc模拟服务器下载

网盘下载连接: https://pan.baidu.com/s/17r1WllxDMzpijajGG3RUvw 提取码: wq6q 

相关文章
相关标签/搜索