Python command: OPC

The OPC commands in Python allow you to interface with OPC servers.

The OPC Action can generate a basic script to connect to most OPC Classic and OPC DA servers. To connect to OPC UA servers, you must use the commands in this topic.

Important: To interface with OPC servers via Python, you must install additional components that are not included in the GainSeeker installation.

Instructions for installing the OPC components can be found in an OPCSetup sub-folder of the GainSeeker program folder – typically C:\Program files (x86)\Hertzler Systems\GainSeeker.

For situations where a script is constantly running - such as polling a data source for new information every few minutes - you can use the GS Console Manager module to launch and/or monitor the progress of such scripts.

See Python commands for other commands you can use with GainSeeker.

 

Contents  [Hide]

 

Commands

Syntax

Example

Description/Remarks

New or changed in GainSeeker version

opc.host

opc.host = "localhost"

Sets the computer name, IP address, or URL of the OPC server.

 

opc.isunifiedarchitecture

opc.isunifiedarchitecture = False

OPC server uses the DA specification, not the UA specification.

Gets or sets whether the OPC server uses the Unified Architecture (UA) specification. For an OPC Classic or OPC DA server, this should be set to False.

This is detected and set automatically from the host name, but can be manually set if detected incorrectly. If used, must be set after the opc.host command.

 

opc.namespaceindex

opc.namespaceindex = 1

May be any number.

Defaults to 0 (0 and negative values are ignored).

Sets the namespace index of the tag to be read. When specifying an index number, the index may be any positive integer (values of 0 or below are ignored).

Alternately, you may use this command to set the namespace URI.

Only used for OPC UA servers.

 

opc.namespaceuri

opc.namespaceuri = "quality"

Sets the namespace URI of the tag to be read.

Alternately, you may use this command to set the namespace index.

Only used for OPC UA servers.

 

opc.progid

opc.progid = "Kepware.KEPServer.V5"

For OPC Classic and OPC DA servers, this identifies the OPC server on the host computer.

Not used for OPC UA.

 

opc.readvalue(tag)

tag1 = opc.readvalue("Machine3.value1")

Returns the value of the specified tag name on the OPC server.

 

opc.readvalueandquality(tag)

tag1 = opc.readvalueandquality("Machine3.value1")

For the specified tag name, this returns a list of the value for that tag and the quality of this tag's value.

In the returned list, the first element is the value and the second is the quality of that value.

 

opc.readvalueandqualitylist(taglist)

taglist = ["Machine3.value1","Machine3.value2"]

returnlist = opc.readvalueandqualitylist(taglist)

For the list of specified tag names, this returns a list of value/quality pairs for each tag (in order).

In this returned list, each value/quality pair is a list where the first element is the value and the second is the quality of that value.

 

opc.readvaluelist(taglist)

taglist = ["Machine3.value1","Machine3.value2"]

returnlist = opc.readvaluelist(taglist)

For the list of specified tag names, this returns a list of the value for each tag (in order).

 

opc.timeout

opc.timeout = 10000

Gets or sets the amount of time (in milliseconds) that GainSeeker will wait for a response from the OPC server before giving up and displaying an error.

9.4

opc.writevalue(tag, value)

opc.writevalue("Machine3.value1", "0")

Writes the specified value to the specified tag name on the OPC server.

 

Sample script to connect to an OPC DA Server:

opc.host = "localhost"  #set host computer name
opc.progid = "Kepware.KEPServer.V5"  #set the OPC server on the host computer
opc.isunifiedarchitecture = False  #manually set to a DA server

tag1 = opc.readvalue("Machine3.value1")  #read a value from the tag "Machine3.value1" and place value in a variable called tag1
tag2 = opc.readvalue("Machine3.value2")  #read a value from the tag "Machine3.value2" and place value in a variable called tag2

Quality codes

A quality code of 192 is good. Other quality codes are:

0 = Bad 

4 = Bad: Configuration error on server 

8 = Bad: Input not connected on server 

12 = Bad: Device failure detected at server 

16 = Bad: Sensor failure detected at server 

20 = Bad: Last known value 

24 = Bad: Comm failure 

28 = Bad: Out of service 

64 = Uncertain 

68 = Uncertain: Last usable value shown 

80 = Uncertain: Sensor has pegged out at limits or is otherwise out of calibration 

84 = Uncertain: Engineering units exceeded 

88 = Uncertain: The value is derived from multiple sources and has less than the required number of Good sources 

192 = Good

216 = Good: Local override - value forced 

Anything else is an unknown quality code.

Troubleshooting

If OPC communication is not functioning correctly, perform these steps:

  1. Run DCOMCNFG.exe .

  2. Browse to Component Services\Computers\My Computer.

  3. Right-click on My Computer and then click Properties.

  4. Click the Default Properties tab and change the Default Authentication Level to None. Then click OK.