SkillAgentSearch skills...

OpenOPC

This is a clone of http://openopc.sourceforge.net, with modifications to make it use distutils

Install / Use

/learn @sightmachine/OpenOPC
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

OpenOPC

This is a clone of http://openopc.sourceforge.net, with modifications to make it use distutils

About 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...

Easy to use

Because the OpenOPC library implements a minimal number of Python functions which may be chained together in a variety of ways, the library is simple to learn and easy to remember. In its simplest form, you can read and write OPC items as easily as any variable in your Python program... print opc['Square Waves.Int4'] opc['Square Waves.Real8'] = 100.0 Cross platform support

OpenOPC works with both Windows and non-Windows platforms. It has been tested with Windows, Linux, and Mac OS X. Functional programming style

OpenOPC allows OPC calls to be chained together in an elegant, functional programming style. For example, you can read the values of all items matching a wildcard pattern using a single line of Python code! opc.read(opc.list('Square Waves.*')) Designed for dynamic languages

Most OPC toolkits today are designed for use with static system languages (such as C++ or C#), providing a close mapping to the underlying Win32 COM methods. OpenOPC discards this cumbersome model and instead attempts to take advantage of the dynamic language features provided by Python.

OpenOPC is also one of the very few OPC-DA toolkits available for any dynamic language, and future support is planned for Ruby. EXAMPLE: Minimal working program

import OpenOPC
opc = OpenOPC.client()
opc.connect('Matrikon.OPC.Simulation')
print opc['Square Waves.Real8']
opc.close()

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 the PyOPC project.

OpenOPC Gateway Service

The Gateway Service is an optional Windows service which handles all the Win32 COM/DCOM calls used by the OpenOPC library module. This offers the following potential advantages:

Avoidance of DCOM security headaches and firewall issues when running the OPC client and OPC server on different nodes. The ability to write OPC client applications that can run on non-Windows platforms such as Linux, Unix, and Mac OS X. Installing the service

C:\OpenOPC\bin> OpenOPCService.exe -install
Installing service OpenOpcService
Service installed
Starting the service

C:\OpenOPC\bin> net start zzzOpenOpcService
Stopping the service

C:\OpenOPC\bin> net stop zzzOpenOpcService

NOTE: If you want to use the OpenOPC Gateway service from another machine as a client and are using OpenOPC 1.2 then you must set the environment variable in windows and restart the gateway service:

OPC_GATE_HOST=x.x.x.x

Reference: http://sourceforge.net/p/openopc/discussion/709251/thread/b0216f58/

OpenOPC Library Tutorial

The best way to learn the OpenOPC library is by trying it interactively from the Python Shell. The following examples use the Matrikon OPC Simulation Server which you can download for free from the company's website. We recommended you use a simulation server such as this one while learning OpenOPC as opposed to testing using a "live" OPC server.

1. Import the OpenOPC module

Start by making the OpenOPC library available to your application. This imports the OpenOPC.py module file located in the lib/site-packages/ directory.

>>> import OpenOPC

2. Create OpenOPC instance (DCOM mode)

DCOM mode is used to talk directly to OPC servers without the need for the OpenOPC Gateway Service. This mode is only available to Windows clients.

>>> opc = OpenOPC.client()

2. Create OpenOPC instance (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')

3. Getting a list of available OPC servers

>>> opc.servers()
['Matrikon.OPC.Simulation.1', 'Kepware.KEPServerEX.V4']

4. Connect to OPC Server

Connect to the specified OPC server. The function returns True if the connection was successful, False on failure.

>>> opc.connect('Matrikon.OPC.Simulation')
True

If the OPC server is running on a different node, you can include the optional host parameter...

>>> opc.connect('Matrikon.OPC.Simulation', 'localhost')
True

5. Reading a single item

Read the specified OPC item. The function returns a (value, quality, timestamp) tuple. If the call fails, the quality will be set to 'Error'.

>>> opc.read('Random.Int4')
(19169, 'Good', '06/24/07 15:56:11')
>>> value, quality, time = opc.read('Random.Int4')

When using the special short form of the function, only the value portion is returned. If any problems are encountered, value will be set to None.

>>> value = opc['Random.Int4']

6. Reading multiple items

Multiple values may be read with a single call by passing a list of item names. Whenever a list is provided as input, the read function returns back a list of (name, value, quality, time) tuples.

>>> opc.read( ['Random.Int2', 'Random.Real4', 'Random.String'] )
[('Random.Int2', 28145, 'Good', '06/24/07 17:44:43'), ('Random.Real4', 19025.2324, 'Good', '06/24/07 17:44:43'), ('Random.String', 'your', 'Good', '06/24/07 17:44:43')]

There is a special version of read function called iread (Iterative Read). iread returns a Python generator which can be used to iterate through the returned results, item by item.

for name, value, quality, time in opc.iread( ['Random.Int2', 'Random.Int4'] ):
   print name, value

7. Reading items using OPC Groups

For best performance it is often necessary to place the items into a named group, then repeatedly request the group's values be updated. Including both the item list along with a group name will cause a new group to be defined and an initial read to be preformed.

>>> tags = ['Random.String', 'Random.Int4', 'Random.Real4']
>>> opc.read(tags, group='test')
[('Random.String', 'options', 'Good', '06/24/07 23:38:24'), ('Random.Int4', 31101, 'Good', '06/24/07 23:38:24'), ('Random.Real4', 19933.958984375, 'Good', '06/24/07 23:38:24')]

Once the group has been defined, you can re-read the items in the group by supplying only the group name. You can repeat this call as often as necessary.

>>> opc.read(group='test')
[('Random.String', 'clients', 'Good', '06/24/07 23:38:30'), ('Random.Int4', 26308, 'Good', '06/24/07 23:38:30'), ('Random.Real4', 13846.63671875, 'Good', '06/24/07 23:38:30')]

When you are done using the group, be sure to remove it. This will free up any allocated resources. If the removal was successful True will be returned, otherwise False.

>>> opc.remove('test')
True

8. Writing a single item

Writing a single item can be accomplished by submitting a (name, value) tuple to the write function. If the write was successful True is returned, or False on failure.

>>> opc.write( ('Triangle Waves.Real8', 100.0) )
'Success'
You can also use the short form...
>>> opc['Triangle Waves.Real8'] = 100.0

9. Writing multiple items

To write multiple items at once, submit a list of (name, value) tuples. The function returns a list of (name, status) tuples letting you know for each item name if the write attempt was successful or not.

>>> opc.write( [('Triangle Waves.Real4', 10.0), ('Random.String', 20.0)] )
[('Triangle Waves.Real4', 'Success'), ('Random.String', 'Error')]

The iwrite function returns a generator designed for iterating through the return statuses item by item...

for item, status in opc.iwrite( [('Triangle Waves.Real4', 10.0), ('Random.String', 20.0)] ):
   print item, status
   

10. Getting error message strings

Including the optional include_error=True parameter will cause many of the OpenOPC functions to append a descriptive error message to the end of each item tuple. In the case of the write function, it will return (name, status, error) tuples.

>>> opc.write( [('Triangle Waves.Real4', 10.0), ('Random.Int4', 20.0)], include_error=True)
[('Triangle Waves.Real4', 'Success', 'The operation completed successfully'), ('Random.Int4', 'Error', "The item's access rights do not allow the operation")]

11. Retrieving item properties

Requesting properties for a single item returns a list of (id, description, value) tuples. Each tuple in the list represents a single property. >>> opc.properties('Random.Int4') [(1, 'Item Canonical DataType', 'VT_I4'), (2, 'Item Value', 491), (3, 'Item Quality', 'Good'), (4, 'Item Timestamp', '06/25/07 02:24:44'), (5, 'Item Access Rights', 'Read'), (6, 'Server Scan Rate', 100.0), (7, 'Item EU Type', 0), (8, 'Item EUInfo', None), (101, 'Item Description', 'Random value.')]

If a list of items is submitted, the item name will be appended to the beginning of each tuple to produce a list of (name, id, description, value) tuples.

>>> opc.properties( ['Random.Int2', 'Random.Int4', 'Random.String'] )
[('Random.Int2', 1, 'Item Canonical DataType', 'VT_I2'), ('Random.Int2', 2, 'Item Value', 4827), ('Random.Int2', 3, 'Item Quality', 'Good'), ('Random.Int2', 4, 'Item Timestamp', '06/25/07 02:35:28'), ('Random.Int2', 5, 'Item Access Rights', 'Read'), ('Random.Int2', 6, 'Server Scan Rate', 100.0), ('Random.Int2', 7, 'Item EU Type', 0), ('Random.Int2', 8, 'Item EUInfo', None), ('Random.Int2', 101, 'Item Description', 'Random value.'), ('Random.Int4', 1, 'Item Canonical DataType', 'VT_I4'), ('Random.Int4', 2, 'Item Value', 14604), ('Random.Int4', 3, 'Item Qu

Related Skills

View on GitHub
GitHub Stars105
CategoryDevelopment
Updated2mo ago
Forks62

Languages

Python

Security Score

80/100

Audited on Jan 22, 2026

No findings