python网课观后感_Python OpenOPC的学习观后感
Python OpenOPC的學習觀感
最近由于公司項目讓我研究工業(yè)標準協(xié)議OPC,開始的設計是在Linux環(huán)境用C/C++進行編寫。但是經過幾天的努力也沒有發(fā)現(xiàn)可用的免費開發(fā)包,網上的收費開發(fā)包如:Softing公司的Toolkit和Matrikon公司的SDK是可以進行Linux環(huán)境下的開發(fā)。由于公司為了節(jié)省成本,又轉向其他方向----需求免費開源的開發(fā)工具。網上免費開放的開發(fā)都是基于Windows下的VB、VC和C#,因為opc需要調用windows下的DCOM組件,由于這個原因成為了Linux環(huán)境下開發(fā)的主要障礙。最后我找到了OpenOPC:
OpenOPC for Python is a free, open source OPC (OLE for Process Control) toolkit designed for use with the popular Python programming language. The unique features that set
it apart from the many commercially available OPC toolkits include...
This project utilizes the de facto OPC-DA (Win32 COM-based) industrial automation standard.
If you are looking for an OPC XML-DA library for Python, then please visit thePyOPCproject.
下面我就介紹一下OpenOPC開發(fā)工具的使用,因為我發(fā)現(xiàn)網上這方面的資料太少了(當然你也可以看OpenOPC的官網說明http://openopc.sourceforge.net/about.html,不過內容很少)。
首先,我們來談談OpenOPC-1.3.1.win32-py2.7.exe:
當你安裝完畢OpenOPC-1.3.1.win32-py2.7.exe時,在你的安裝路徑可以看到如下目錄:
bin目錄下有兩個文件:opc.exe和OpenOPCService.exe,這兩個文件是src目錄下的opc.py和OpenOPCService.py生成的,他們是可以在命令提示符下直接運行的,具體操作可以查看README.txt文件;
doc目錄下是OpenOPC的說明文件,通過它你可以了解到OpenOPC的具體使用方法,主要是測試工具opc.py或opc.exe文件的使用;
lib目錄下主要是OpenOPC所要調用的庫文件gbda_aut.dll;
src目錄下包括opc.py、OpenOPCService.py、OpenOPC.py、SystemHealth.py;
其次,我們來談談測試OPC
server-----MatrikonOPCSimulation
點擊MatrikonOPCSimulation.exe文件對其進行安裝,安裝完畢后需要將其服務器的名字設置到環(huán)境變量中:OPC_SERVER=matrikon.OPC.simulation
啟動MatrikonOPC
server of simulation軟件設置其屬性就OK了,具體操作請查看軟件文檔;
我們再來談談OpenOPC的原理:
從測試demo---opc.py中我們可以看到它的啟動方式有兩種:docm和open。dcom方式是直接調用本機上的DCOM組件,而open方式是通過zzzopenopcservice(OpenOPC
Gateway Service on Windows)來調用windows上的DCOM組件,因此你在調用時要認真區(qū)分;
zzzopenopcservice的啟動,詳情請查看OpenOPC/doc/OpenOPC.pdf文檔;
你還可以通過opc.py上的不同命令測試拉取MatrikonOPC
server of simulation上的數(shù)據(jù)信息;
最后,我們談談MACSV系統(tǒng)OPC
Server通信軟件的使用和測試代碼的應用實例:
安裝MACSV系統(tǒng)OPC
Server通信軟件,并將其服務器的名字設置到環(huán)境變量中:OPC_SERVER=Hollysys.MACSV5OPCServer;
啟動MACSV5OPCServer,設置其屬性;
測試代碼opctest1.py如下:
from sys import *
from getopt import *
from os import *
import signal
import sys
import os
import types
import datetime
import re, time, csv
import OpenOPC
import Pyro
opc_class='Matrikon.OPC.Automation;Graybox.OPC.DAWrapper;HSCOPC.Automation;RSI.OPCAutomation;OPC.Automation'
client_name='OpenOPC'
opc_server='Hollysys.MACSV5OPCServer'
opc_host='127.0.0.1'
taglist=['Server1.Group0.opc_01','Server1.Group0.opc_02','Server1.Group1.rule1','Server1.Group1.rule2','Server2.Group0.sg1','Server2.Group0.sg2','Server2.Group1.sg2','Server2.Group1.sg1']
class SigHandler:
def __init__(self):
self.signaled = 0
self.sn = None
def __call__(self, sn, sf):
self.sn = sn
self.signaled += 1
# Establish signal handler for keyboard interrupts
def signalhandle():
sh = SigHandler()
signal.signal(signal.SIGINT,sh)
if os.name == 'nt':
signal.signal(signal.SIGBREAK,sh)
signal.signal(signal.SIGTERM,sh)
return sh
def test01():
opc = OpenOPC.client(opc_class, client_name)
print "### create opc!"
opc.connect(opc_server, opc_host)
print "### connect opc server:", opc_server
test02(opc)
taglist_opc = opc.read(taglist)
info_opc = opc.info()
servers_opc = opc.servers(opc_host)
tags = []
data = opc.list(tags, flat=True)
list_opc = opc.list
list_data = list_opc(tags)
opc.close()
print "### close opc!"
for i in range(len(taglist_opc)):
(name, val, qual, time) = taglist_opc[i]
print 'name:', name
print 'val:', val
print 'qual:', qual
print 'time:', time
print "value:", str(list(taglist_opc))
print "info:", str(list(info_opc))
print "servers:", str(list(servers_opc))
print "data:", str(list(data))
print "list:", str(list(list_data))
def test02(opc):
count0 = 0
sh = signalhandle()
while not sh.signaled:
print "test02... ", count0
data_opc = opc.read(tags=['Server1.Group0.opc_01'],
#group='test',
#size=group_size,
#pause=tx_pause,
#source=data_source,
update=1000,
#timeout=timeout,
#sync=sync,
#include_error=include_err_msg)
)
print "[test02] data:", str(list(data_opc))
try:
time.sleep(1)
except IOError:
break
count0 += 1
def main():
test01()
if __name__ == '__main__':
main()
具體開發(fā)包和測試代碼可以到http://download.csdn.net/detail/cl185303590/8821585進行下載。
總結
以上是生活随笔為你收集整理的python网课观后感_Python OpenOPC的学习观后感的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用html通过python调用mysql
- 下一篇: 插入顶部_声屏障顶部斜插式安装可获得10