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

1、安装OpenOPC

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

   b> pip安装依赖包  Pywin32 + Pyro

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

  d> 修改环境变量

       OPC_MODE = open

2、连接opc服务器

# 导入包
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\')

3、读取opc服务器数据

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]

4、写入opc服务器

# 写入一个点
# 方式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)] )

5、其他

# 列出可获取的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 

 

版权声明:本文为shuangpang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/shuangpang/p/10653663.html