OPC reading and writing not working withing thread
Jeff Peery
jeffpeery at yahoo.com
Sun Dec 2 16:43:04 PST 2007
Skipped content of type multipart/alternative-------------- next part --------------
######################################################################################
# myOPC.py #
######################################################################################
''' autor : Katarzyna Gebal
Narrow Gate Logic
www.nglogic.com'''
######################################################################################
''' OPC Client project file'''
######################################################################################
import win32com.client
import win32api
import pythoncom
import time
import sys
""" OPC FUNCTIONS """
# get items from OPC server
def getOPCItems(server):
# Get OPC servers and set the selected value
serverList = getServerList()
if serverList == None:
Error('No servers available')
elif server not in serverList:
Error('Requested server not available')
else:
# connect to server
(opcServer,group) = connectServer(self.main.deviceList[self.deviceText].server)
# get the items available in OPC server
try:
itemList = getOPCItemsList(opcServer)
except Exception:
Error('Error obtaining server items')
# disconnect
try:
disconnectServer(opcServer)
except:
self.Error('Error disconnecting server')
return itemList
# The function gets access to the the OPC automation object.
# computerName parameter is given when we want to make remote connection by using DCOM.
# The function returns OPCServer object.
def connectCOMOPC(computerName=None):
if computerName == None:
opcServer = win32com.client.Dispatch('OPC.Automation.1')
else:
# can connect to remote computer
opcServer = win32com.client.DispatchEx('OPC.Automation.1',computerName,clsctx=pythoncom.CLSCTX_ALL)
return opcServer
# The function returns the tuple of names (ProgIDs) of the registered OPC Servers.
# This names are used when we connect to the server.
# computerName parameter is given when we want to make remote connection by using DCOM.
def getServerList(computerName=None):
try:
return connectCOMOPC(computerName).GetOPCServers()
except Exception:
return None
# The function connects to an opc server.
# It creates one private OPCGroup. This group will be used to to access items.
# It returns the tuple of OPCServer object and the OPCGroup object.
def connectServer(serverName,computerName=None):
try:
opcServer = connectCOMOPC(computerName)
opcServer.Connect(serverName)
groups = opcServer.OPCGroups
group = groups.Add()
group.IsActive = 1
group.IsSubscribed = 1
return (opcServer,group)
except Exception:
return (None,None)
# The function disconects from the OPC Server.
def disconnectServer(opcServer):
opcServer.OPCGroups.RemoveAll() # cleaning up
opcServer.Disconnect()
opcServer = None
# The function traverses recursively the hierarchical structure of item's namespaces.
# It appends all found items to the list.
def opcAppendLeafs(browser,list):
browser.ShowLeafs()
for leaf in browser:
list.append(browser.GetItemID(leaf))
browser.ShowBranches()
for branch in browser:
browser.MoveDown(branch)
opcAppendLeafs(browser,list)
browser.MoveUp()
# Function tries to get the items that exist in the opcServer.
# It uses OPCbrowser object.
# Sometimes it is not possible to get all existing item names.
# The ability of browsing depends on the server configuration.
def getOPCItemsList(opcServer):
try:
list = []
browser = opcServer.CreateBrowser()
browser.AccessRights = 3
browser.MoveToRoot()
opcAppendLeafs(browser,list)
return list
except Exception:
return None
# Adds item to the private group group
def addItem(itemID,group):
try:
return group.OPCItems.AddItem(itemID,1)
except Exception:
return None
# Function reads value quality and timestamp of the item
# 0x1 means that values are read from the OPCcache
# The second possible option (0x2) is reading directly from the device.
# Function returns the tuple of the value, quality and the time stamp
def readItemValue(opcItem):
return opcItem.Read(0x1)
# Function writes value to the opc server
# An error appears if the conversion of value type to the required
# item type is not possible.
def writeItemValue(opcItem,value):
opcItem.Write(value)
More information about the wxpython-users
mailing list