如何通过python使用ActiveX控件来控制德州仪器DDC4100数字控制器?

时间:2012-07-06 18:42:55

标签: python activex

我的任务是弄清楚如何使用python与DDC4100数字控制器进行通信。 DDC4100是DLP Discovery 4100开发套件的一个组件。如果您对这是什么感到好奇,可以点击here.

我一直在使用PythonWin和pywin32软件包尝试与控制器通信。

我想使用早期绑定自动化,因此我使用makepy为DDC4100 ActiveX控件模块生成包装器。

# -*- coding: mbcs -*-
# Created by makepy.py version 0.5.01
# By python version 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
# From type library 'DDC4100.tlb'
# On Fri Jul 06 10:52:42 2012
'DDC4100 ActiveX Control module'
makepy_version = '0.5.01'
python_version = 0x20703f0

import win32com.client.CLSIDToClass, pythoncom, pywintypes
import win32com.client.util
from pywintypes import IID
from win32com.client import Dispatch

# The following 3 lines may need tweaking for the particular server
# Candidates are pythoncom.Missing, .Empty and .ArgNotFound
defaultNamedOptArg=pythoncom.Empty
defaultNamedNotOptArg=pythoncom.Empty
defaultUnnamedArg=pythoncom.Empty

CLSID = IID('{4AB30830-EFC0-496D-8D38-ACFE2518ECEB}')
MajorVersion = 1
MinorVersion = 0
LibraryFlags = 10
LCID = 0x0

from win32com.client import DispatchBaseClass
class _DDDC4100(DispatchBaseClass):
    'Dispatch interface for DDC4100 Control'
    CLSID = IID('{C4374661-BB24-410F-9BEB-5DED735F4985}')
    coclass_clsid = IID('{5C75C113-9AFC-42AC-8CED-1ABFE27763B5}')

    def AboutBox(self):
        return self._oleobj_.InvokeTypes(-552, LCID, 1, (24, 0), (),)

    def AllowMessages(self, value=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(11, LCID, 1, (2, 0), ((2, 0),),value
            )

    def Clear(self, BlockNum=defaultNamedNotOptArg, DoReset=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(3, LCID, 1, (2, 0), ((2, 0), (2, 0)),BlockNum
            , DoReset)

    def ConnectDevice(self, DeviceNum=defaultNamedNotOptArg, SrcFile=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(14, LCID, 1, (2, 0), ((2, 0), (8, 0)),DeviceNum
            , SrcFile)

    def ConvertImage(self, SrcFile=defaultNamedNotOptArg, DestFile=defaultNamedNotOptArg, MirrorImage=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(4, LCID, 1, (2, 0), ((8, 0), (8, 0), (2, 0)),SrcFile
            , DestFile, MirrorImage)

    def DownloadAppsFPGACode(self, FileName=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(12, LCID, 1, (2, 0), ((8, 0),),FileName
            )

    def FileToFrameBuffer(self, ImageFile=defaultNamedNotOptArg, MirrorImage=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(6, LCID, 1, (2, 0), ((8, 0), (2, 0)),ImageFile
            , MirrorImage)

    def FloatMirrors(self):
        return self._oleobj_.InvokeTypes(5, LCID, 1, (2, 0), (),)

    def GetActiveXRev(self):
        return self._oleobj_.InvokeTypes(13, LCID, 1, (3, 0), (),)

    def GetBlkAd(self):
        return self._oleobj_.InvokeTypes(22, LCID, 1, (2, 0), (),)

    def GetBlkMd(self):
        return self._oleobj_.InvokeTypes(20, LCID, 1, (2, 0), (),)

    def GetCOMPDATA(self):
        return self._oleobj_.InvokeTypes(32, LCID, 1, (2, 0), (),)

    def GetDDCVERSION(self):
        return self._oleobj_.InvokeTypes(2, LCID, 1, (2, 0), (),)

    def GetDLLRev(self):
        return self._oleobj_.InvokeTypes(17, LCID, 1, (3, 0), (),)

    def GetDMDTYPE(self):
        return self._oleobj_.InvokeTypes(1, LCID, 1, (2, 0), (),)

    def GetDriverRev(self):
        return self._oleobj_.InvokeTypes(15, LCID, 1, (3, 0), (),)

    def GetEXTRESETENBL(self):
        return self._oleobj_.InvokeTypes(40, LCID, 1, (2, 0), (),)

    def GetFPGARev(self):
        return self._oleobj_.InvokeTypes(47, LCID, 1, (3, 0), (),)

    def GetFirmwareRev(self):
        return self._oleobj_.InvokeTypes(16, LCID, 1, (2, 0), (),)

    def GetGPIO(self):
        return self._oleobj_.InvokeTypes(42, LCID, 1, (2, 0), (),)

    def GetNSFLIP(self):
        return self._oleobj_.InvokeTypes(34, LCID, 1, (2, 0), (),)

    def GetNumDevices(self):
        return self._oleobj_.InvokeTypes(46, LCID, 1, (2, 0), (),)

    def GetPWRFLOAT(self):
        return self._oleobj_.InvokeTypes(38, LCID, 1, (2, 0), (),)

    def GetRST2BLKZ(self):
        return self._oleobj_.InvokeTypes(24, LCID, 1, (2, 0), (),)

    def GetResetComplete(self, waittime=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(49, LCID, 1, (2, 0), ((3, 0),),waittime
            )

    def GetRowAddr(self):
        return self._oleobj_.InvokeTypes(28, LCID, 1, (2, 0), (),)

    def GetRowMd(self):
        return self._oleobj_.InvokeTypes(26, LCID, 1, (2, 0), (),)

    def GetSTEPVCC(self):
        return self._oleobj_.InvokeTypes(30, LCID, 1, (2, 0), (),)

    def GetSpeedMode(self):
        return self._oleobj_.InvokeTypes(18, LCID, 1, (2, 0), (),)

    def GetWDT(self):
        return self._oleobj_.InvokeTypes(36, LCID, 1, (2, 0), (),)

    def IsDeviceAttached(self):
        return self._oleobj_.InvokeTypes(43, LCID, 1, (11, 0), (),)

    def LoadControl(self):
        return self._oleobj_.InvokeTypes(45, LCID, 1, (2, 0), (),)

    def LoadData(self, data=defaultNamedNotOptArg, length=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(44, LCID, 1, (2, 0), ((16386, 0), (2, 0)),data
            , length)

    def LoadImageFileToBuffer(self, FileName=defaultNamedNotOptArg, ImageBufferPtr=defaultNamedNotOptArg, MirrorImage=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(9, LCID, 1, (2, 0), ((8, 0), (16386, 0), (2, 0)),FileName
            , ImageBufferPtr, MirrorImage)

    def LoadToDMD(self, BlockNum=defaultNamedNotOptArg, DoReset=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(7, LCID, 1, (2, 0), ((2, 0), (2, 0)),BlockNum
            , DoReset)

    def MemToFrameBuffer(self, ImageBufferPtr=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(8, LCID, 1, (2, 0), ((16386, 0),),ImageBufferPtr
            )

    def Reset(self, BlockNum=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(10, LCID, 1, (2, 0), ((2, 0),),BlockNum
            )

    def SetBlkAd(self, value=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(21, LCID, 1, (2, 0), ((2, 0),),value
            )

    def SetBlkMd(self, value=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(19, LCID, 1, (2, 0), ((2, 0),),value
            )

    def SetCOMPDATA(self, value=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(31, LCID, 1, (2, 0), ((2, 0),),value
            )

    def SetConversionThreshold(self, threshold=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(48, LCID, 1, (2, 0), ((2, 0),),threshold
            )

    def SetEXTRESETENBL(self, value=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(39, LCID, 1, (2, 0), ((2, 0),),value
            )

    def SetGPIO(self, value=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(41, LCID, 1, (2, 0), ((2, 0),),value
            )

    def SetNSFLIP(self, value=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(33, LCID, 1, (2, 0), ((2, 0),),value
            )

    def SetPWRFLOAT(self, value=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(37, LCID, 1, (2, 0), ((2, 0),),value
            )

    def SetRST2BLKZ(self, value=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(23, LCID, 1, (2, 0), ((2, 0),),value
            )

    def SetRowAddr(self, value=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(27, LCID, 1, (2, 0), ((2, 0),),value
            )

    def SetRowMd(self, value=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(25, LCID, 1, (2, 0), ((2, 0),),value
            )

    def SetSTEPVCC(self, value=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(29, LCID, 1, (2, 0), ((2, 0),),value
            )

    def SetWDT(self, value=defaultNamedNotOptArg):
        return self._oleobj_.InvokeTypes(35, LCID, 1, (2, 0), ((2, 0),),value
            )

    _prop_map_get_ = {
    }
    _prop_map_put_ = {
    }

class _DDDC4100Events:
    'Event interface for DDC4100 Control'
    CLSID = CLSID_Sink = IID('{0CC72AA0-956F-48BD-9118-4446866ECBE3}')
    coclass_clsid = IID('{5C75C113-9AFC-42AC-8CED-1ABFE27763B5}')
    _public_methods_ = [] # For COM Server support
    _dispid_to_func_ = {
    }

    def __init__(self, oobj = None):
        if oobj is None:
            self._olecp = None
        else:
            import win32com.server.util
            from win32com.server.policy import EventHandlerPolicy
            cpc=oobj._oleobj_.QueryInterface(pythoncom.IID_IConnectionPointContainer)
            cp=cpc.FindConnectionPoint(self.CLSID_Sink)
            cookie=cp.Advise(win32com.server.util.wrap(self, usePolicy=EventHandlerPolicy))
            self._olecp,self._olecp_cookie = cp,cookie
    def __del__(self):
        try:
            self.close()
        except pythoncom.com_error:
            pass
    def close(self):
        if self._olecp is not None:
            cp,cookie,self._olecp,self._olecp_cookie = self._olecp,self._olecp_cookie,None,None
            cp.Unadvise(cookie)
    def _query_interface_(self, iid):
        import win32com.server.util
        if iid==self.CLSID_Sink: return win32com.server.util.wrap(self)

    # Event Handlers
    # If you create handlers, they should have the following prototypes:


from win32com.client import CoClassBaseClass
# This CoClass is known by the name 'DDC4100.DDC4100Ctrl.1'
class DDC4100(CoClassBaseClass): # A CoClass
    # DDC4100 Control
    CLSID = IID('{5C75C113-9AFC-42AC-8CED-1ABFE27763B5}')
    coclass_sources = [
        _DDDC4100Events,
    ]
    default_source = _DDDC4100Events
    coclass_interfaces = [
        _DDDC4100,
    ]
    default_interface = _DDDC4100

RecordMap = {
}

CLSIDToClassMap = {
    '{C4374661-BB24-410F-9BEB-5DED735F4985}' : _DDDC4100,
    '{5C75C113-9AFC-42AC-8CED-1ABFE27763B5}' : DDC4100,
    '{0CC72AA0-956F-48BD-9118-4446866ECBE3}' : _DDDC4100Events,
}
CLSIDToPackageMap = {}
win32com.client.CLSIDToClass.RegisterCLSIDsFromDict( CLSIDToClassMap )
VTablesToPackageMap = {}
VTablesToClassMap = {
}


NamesToIIDMap = {
    '_DDDC4100Events' : '{0CC72AA0-956F-48BD-9118-4446866ECBE3}',
    '_DDDC4100' : '{C4374661-BB24-410F-9BEB-5DED735F4985}',
}

然后我使用Dispatch来创建控件对象。

from win32com.client import Dispatch
DDC4100 = Dispatch('DDC4100.DDC4100Ctrl.1')

现在,当我从包装器中调用其中一个函数时,我遇到了一个错误。

DDC4100.AboutBox()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 505, in   __getattr__
ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
com_error: (-2147418113, 'Catastrophic failure', None, None)

似乎makepy包装器没有自动加载,所以我尝试使用形式gencache的EnsureModule()函数,我仍然收到相同的错误。此时我不知道该怎么做,因为我已经通过注册类型库中的其他几个程序完成了相同的过程并且没有任何问题。有没有人在我的方法或逻辑中看到任何错误,或者知道使用ActiveX控件的不同方法?

1 个答案:

答案 0 :(得分:0)

通常,该错误意味着ActiveX控件需要一个完整的容器。 您可以尝试将其嵌入在IE中运行的网页中,该网页充当ActiveX 容器