Hello,
I'm getting in a bit over my head here and need some help. I'm running a
simple wxpython application that runs a thread in the background. The thread is
supposed to read and write data to/from an OPC server - which I use to
communicate to a PLC (programmable logic controller, for process automation).
My OPC module works well for just reading and writing to the OPC server;
however when I try to read and write to the OPC server from within the thread
it doesn't work. I get this error:
Here's the snippet from my thread:
# connect to server
opcServerName = 'the server name goes here'
opcItemName = 'the opc item goes here'
(server, group) = myOPC.connectServer(opcServerName)
item = myOPC.addItem(opcItemName, group)
value = myOPC.readItemValue(item)
# the value return a tuple of info including a value and timestamp and quality
Here's the error that's returned:
line 124, in readItemValue
return opcItem.Read(0x1)
File "<COMObject AddItem>", line 3, in Read
File "C:\Python25\lib\site-packages\win32com\client\dynamic.py", line 258, in
_ApplyTypes_
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType,
argTypes) + args)
com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0,
-2147221008), None)
I attached my OPC module, if its important to include the wxapp I can do that
to, but it's pretty apparent that the problem only happens when I call my OPC
funcitons from within a thread...
I suppose its also worthwhile to mention that I need to access the OPC module
from within a thread (thats not the main applications thread) because I'm
trying to aquire data from a PLC and I want to do that continuously and not
lock up my program.
Any ideas on how I might get this thing to work within the thread? Thanks!
Jeff
---------------------------------
Get easy, one-click access to your favorites. Make Yahoo! your homepage.
######################################################################################
# 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)
_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32