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 

相關文章
相關標籤/搜索