在好例子网,分享、交流、成长!
您当前所在位置:首页Others 开发实例一般编程问题 → OpenOPC指南

OpenOPC指南

一般编程问题

下载此实例
  • 开发语言:Others
  • 实例大小:0.16M
  • 下载次数:9
  • 浏览次数:520
  • 发布时间:2020-10-14
  • 实例类别:一般编程问题
  • 发 布 人:robot666
  • 文件格式:.pdf
  • 所需积分:2
 

实例介绍

【实例简介】
OpenOPC使用教程,详细描述了如何通过python等命令方式操纵OPC Server
2 INSTALLATION ON TEST MACHINE 2.2 Install OpenoPC software To install the OpenoPc software, siInply run the installer. When the installer asks which components to install select Command Line Client: This installs the command line client(opc.exe)which is a powerful command line tool for retrieving OPc data OPC Gateway Service: This is the service that allows non-windows machines to access OPc data. Basically this service accesses the opc data and passes it on lo Che client asking for the dala. This service is also what allows you lo eliminate DCOM configuration issues as remote hosts can gct the data using the service and not through DCOM Development Library: This library is needed when developing your own OPC appli cations. Examples of this will be shown in section 4 Source Code: This option installs the source code for the opc client and the gate server. Note that the installer will complain if python is not installed on the machinc, but the files will still be placed in thC SrC directory for inspection. This document does not discuss the source code. The source code takes very little spac A reboot is not required, but if you insta.ll the soft ware on nt 4.0 it may be a good idea. The OpenOPC software is typically installed in C:\OpenOPC. If you install the software on a machine running NT 4.0, the installer will place msvcr71 dll(Microsoft C Runtime Library) in the C: WINNT\ SYSTEM32 directory. This DLL is generally always present on machines running Windows xp. The DlL is not present on a machine with Windows 2000 freshly installed, but may have been insta.lled by other applications 2.3 Quick test of software on local machine Several default setting for the program opc. exe has been set using environment variables You can see these settings by typing SET from the command prompt and looking for lines that start with OPC OPC_CLASS=Matrikon OPC Automation; Graybox OPC. DAWrapper; HSCOPC Automation; OPC_CLIENT=OpenOPC OPC GATE HOST=localhost OPC_GATE PORT=7766 OPC_HOST=localhost 2 INSTALLATION ON TEST MACHINE OPC MODE=dcom OPC-SERVER=Hci. TPNServer; Matrikon OPC Simulation; Prosys OPC. Simulation To read one tag from the Matrikon OPC server using the default settings simply type opc Random Int4 You can get some information about the default oPc server like this OpC You can list the oPc servers on the local machine like this ODC q If the OPC server supports browsing(the Matrikon OPC Server does) you can search for tags matching certain patterns using wild cards. The following command list all tags having the extension Real8 opc -f * Real8 Which will producc a list of tags from the Matrikon Simulation Scrvcr like this Bucket Brigade. Real8 Random. Real8 Read error. real8 Saw-toothed waves. Real8 Square Waves. Real8 Triangle Waves. Real8 Write error. real8 Write Only Real8 The prograln opc. exe has good supporl for using pipes. Thus it is possible to read the values of these tags in a command like this: °Pc-f*.Rea18|°pc Which will display the following output On Windows Nt4.0 it is sometimes necessary to specify the hostname instead of the default (localhost)to get a list of OPC servers: opc -h testbox -q(assuming host name of computer is tests 2 INSTALLATION ON TEST MACHINE Bucket Brigade. Real8 0.0000 Good10/26/072:41:16 Random Real8 164994963Good10/26/072:41:16 Read error real8 1073479676Good10/26/072:116 Saw-toothed Waves. Real8 3.1416 Good10/26/072241:16 Square Waves. Reals 0.0000 Good10/26/0722:41:16 Triangle Waves. Real8 3.1416 Good10/26/072:41:16 Write Error Real8 0.0000 Good10/26/072:116 Write Only Real8 0.0000 Bad10/26/0722:40:1 2. 4 Verifying Installation of Gateway Service The gateway server will be used by remote clients to get oPC data. This sub-section explains how to verify the gateway server is working properly before we try to access it from a remote client computer First look in Task-Manager to see if the process"openopeservice"is running. If not, it can be startcd like this NET START zzzopenopcservice If errors like: the service name is invalid"are encountered, the service was likely not installed correctly. Verify that "OpenOPCService exe"is located in C:OpenOPC\ bin and install the service like this openopcservice -install -aut NET START zzzopenopcservice Try to list the current connections to the gateway server-there should be none C-S Try to read one tag using the gateway server opc -m open RandomInt4 You can easily convince yourself that the gateway server was used for retrieving the data. Simply stop the gateway server and try to read the tag again. You will recieve an error message 3In OpenOPC version 1. 1. 5 the name of the windows service was changed from openopcservice to zzzopenopcservice to make sure the OpenoPC Gateway Service would start after all other services was started If this does not work, the SYSTEM account may not have the correct permissions. One thing I have found that works is to change the startup of the service to usc an administrator account 3 MANUAL INSTALLATION ON CLIENT MACHINE ET STOP zzzopenopcservice opc -m open Random Int4 Rcmcmbcr to start the gateway scrvicc again bcforc procccding with the next scction 3 Manual installation on client machine This section covers the manual installation of the OpenOPC software on a separate machine- the client. The purpose of explaining the manual installation is to give the user a more detailed understanding of how the software works, The data will be retrieved through thc gateway service on thc rcmotc machinc, this climinates DCOM configuration issues. No OPC server will be installed on the client machine. The install procedure involves nothing more than copying" opc. exe"to a new machine. Preferably you place "opc. exe!"in the PATH (like a tools directory), but this is entirely up to you. If you are using NT 4.0 you may need to place a copy of msvcr71 dll(Microsoft C Runtimc Library)in the C: WINNT\SYSTEM32 directory if it is not thcrc 3.1 Reading a Value from the remote host using gateway The environment variables will not be set since we did not run the installer on the client machine. This means we will have to specify more options when making the call Assume that the machine(the server) we installed the Matrikon OPC Simulation and OpenoPc software on before is called"TESTBOX To quickly test that opc. exe can talk to the gateway service on the server, execute the following command on the client machine opc -S-H TESTBOX This command will show the remote connections to the gateway service-there should be no remote connections. If errors are encountered, you need to ensure that the gateway service is running on the remote computer as described in section 2. 4. If name resolution is not working properly, the IP-address of the remote computer name can be used in the command opc -S-H 10.36.16.100(replace with IP address of TESTBOX Try reading a value from the Matrikon OPC Server on TESTBOX by typing(note the case sensitivity of the options) the following on one line at the command prompt 3 MANUAL INSTALLATION ON CLIENT MACHINE opc -m open -H TESTBOX -h TESTBOX -s Matrikon OPC Simulation -r Random Int4 The”-H” option means we want to use the gateway service on TESTBOX.The”-h” option specifies the host name of computer the running the opc server. The-s option specified the name of the OPC server.'The2-r"option means read and is optional (i.e not needed) You can sct the following environment variables to avoid having to typc the long com mand line. To temporary set these environment variables, simply type the following from the command line SET OPC GATE HOST=TESTBOX SET OPC HOST-TESTBOX SET OPC=open set OPC SERVER=Matrikon opc. simulation These environment variables should probably be set permanently once you have the configuration working properly. With the environment variables set, the command for retrieving data remotely becomes much simpler opc- r Random.Int4( Note the”-r” is optional) 3.2 Gateway Port The gateway service uses port. 7766 by default for TCP/IP communication. If you change this port number(set by environment variable OPC_GATE PORT) you will need to do so on both the server machine and the client machine. firewalls on either the remote or client conputer can cause connection proble. Make sure that the gateway port is open on both the server and the client To verify that the gateway port is being used try the following command from the client machine OpC -r Random. Int4 -l1 This command assumes that the environment variables shown in section 3. 1 are set such that there is no need to specify hostname and opc server name in the command. The -L12option tells the opc. exe program to retrieve the data every 1 second continuously Now from a second command prompt on the remote machine type netstat to see TCP/IP connections. You will clearly see that port 7766 is being used for TCP/IP communication between client machine(laptop) and the server running the gateway service(textbox) 4 DEVELOPING OPC APPLICATIONS WITH PYTHON Proto Local Address Foreign Address State TCP laptop: 1089 testbOx: 7766 TIME WAIT TCP laptop: 1090 textbox: 7766 ESTABLISHED The gateway service on the server can be queried to show open connections. Execute the following command on the client computer ○Pc-s- H TESTBOX The output will look similar to this if the continuous data request is still running at another command prompt on the client computer Remote Client Start time Last Transaction 10.36.16.58( atop)06/27/0814:53:0106/27/0814:53:07 4 Developing OPC applications with Python The real power of OpenOPc is the ability to use it within Python. Python offers easy connectivity to many databases and makes it very easy to develop web applications Python runs on many platforms including Windows, Linux and possibly your PDA. In addition to Python, the following Python add-on packages are required (see readme. tct included with OpenOPC package) Python Remote Objects(Pyro Python for Windows Extensions(pywin32 This document is not a comprehensive tutorial on using Python with OpenOPC. The main purpose this section is to make the user aware that it is possible to develop powerful applications accessing opc data in Python 4. 1 Simple example Below is a simple listing of Python code for reading two OPC values from the remote gateway server on TESTBOX import OpenOPC gateway='testbox' opchost=?testbox 4 DEVELOPING OPC APPLICATIONS WITH PYTHON opcserv=,Matrikon OPC. simulation' taglist =L'Random. Int4,'Random. Real4'J print ' Connecting to Gateway Server on: ,,gateway enOPC. open_client(gateway) opc connect(opcserv, opchost) v opC read(taglist) opc. close for i in range(len(v)): (name, val, qual, time)=vLi] print 7-15s7-15s7-15s 7-15s'% (name, val, qual, time) The Python program above produces the following output Connecting to Gateway Server on: testbox Random Int4 9961.0Good10/29/0704:23:52 Random Real 491.0 10/29/0704:23:52 4.2 Writing opc data to database Below is another simple example. opc data is retrieved and written to a sQlite database. The Python program is made simple on purpose without proper error check- ing. A real prograIn should perforn proper error checking for failed OPC connections etc. The purpose is to illustrate the power of OpenoPC and Python. It would be ver simple to develop a web application that displayed the values stored in the database OpenOPC and sQLite Python example import sqlite3 import os Import time import OpenOPC dbfile=)test. db) remo七 ehost=tes七box opcserver=,Matrikon OPC Simulation> tagname=?Random Int4 sametime dbexist =os path exists(bfile) db =sqlite3 connect(dbfile) 5 USING OPC CLIENT IN DCOM MODE cur db cursor o create table first time new database is accessed if not dbexist sqlstring = CREATE TABLE opcdata V (id INTEGER PRIMARY KEY, tag TEXT,\ val TEXT, stat TEXt, datetime TEXt, atime TEXT), cur.execute (sqlstring db. commit while 1>0: infinite loop kill with Ctrl-C opc=OpenOPC client( opc connect(opcserver, remotehost) (opcvalue, opcstat, opctime)= opc. read(tagname) close o atime =time. asctime)# current computer time as float sqlstring =>INSERT INTO opcdata (id, tag, val, stat, datetime, atime)\ VALUES(NULL s","%f,"s",is",s , ( le, opcvalue, opcstat, opctime, atime) cur.execute(sqlstring) db. commit cur. execute('SELECT FROM opcdata') dbvalues cur. fetchallo dalen len(devalues) print dbvalues [ablen-1] print newest value in database time. sleep(sampletime) end of while loop 5 Using OPc client in DCOM mode If the OpenoPC command line client opc eae is used to read values locally(Com)things works well. However, if the client opc. ece are used to read values remotely(DCOm) things gets more complicated. First DCOM security needs to be configured, which can be quitc complicatcd. A trick that often works is do be logged in to the two computors that need to talk with the same account that preferably should have administrative rights on both computers. Once a connection is made, accounts with provileges less than administrator can be created DCOM Security is outside the scope of this writeup 【实例截图】
【核心代码】

标签:

实例下载地址

OpenOPC指南

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警