python 2.7 讀寫 opc數據


運行環境 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 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM