سؤال

Python newbie here. So, please excuse if this has been asked before in a different format.

I am trying to replicate the following perl snippet in Python using the win32com module. This snippet is provided by Qualcomm for easier automation of their tools.

use Win32::OLE;
use Win32::OLE::Variant;
$prod_id = "QPSTAtmnServer.Application"; # AppId for the Automation server.
eval{ $qpst = Win32::OLE->GetActiveObject($prod_id)}; # Attempt to use a running instance.
die "$prod_id not installed" if $@;
unless (defined $qpst) { $qpst = Win32::OLE->new($prod_id, sub {$_[0]->Quit;}) or die "Cannot start $prod_id";} # Start a new instance. Call Quit when $qpst set to undef or script exits.
if (defined $qpst)
{
    $port = $qpst->GetPort("COM30001");
}

The block of python code I have till now is as follows:

import win32com.client
import time
import os

cmd = 'cls'
os.system(cmd)
cmd = 'start C:\\LAB\\exe\\pskill.exe QPSTConfig'
os.system(cmd)
cmd = 'start C:\\LAB\\exe\\pskill.exe QPSTServer'
os.system(cmd)
cmd = 'start C:\\LAB\\exe\\pskill.exe AtmnServer'
os.system(cmd)
time.sleep(2)
_path = os.getcwd()
qpst = win32com.client.Dispatch('QPSTAtmnServer.Application')
time.sleep(5)
if (qpst is None):
    print('Darn!')
else:
    port = qpst.GetPort('30001')
    print(port)

and it throws the following error:

Traceback (most recent call last):

File "xxxx.py", line 20, in module

port = qpst.GetPort('30001')

TypeError: 'NoneType' object is not callable

After reading a couple of posts it seems like the method (GetPort) is not registering as a method after all.

Is that correct analysis?

If yes, how do I make Python interpret it as a method?

If not, what is going on here with the error?

Thanks in advance for the help!

هل كانت مفيدة؟

المحلول

It looks like I had to do couple of things to solve the issue.

  1. Use the makepy command on the "AtmnServer" OLE TypeLibrary file to create a *.py file in:

    ...\Python27\Lib\site-packages\win32com\gen_py\
    
  2. Add an extra line to actually interpret the required Method as a method (instead of as a property/attribute or something) :

    qpst._FlagAsMethod("GetPort")
    

    before the line:

    port = qpst.GetPort("COM30001")
    

Thanks again for offering to help!

نصائح أخرى

Correct, it is saying that GetPort does not exist. Have you checked that the Perl version works? If you don't have Perl, you could try through Excel's VBA (open its VBA console -- you may have to enable it by following the steps here). If you can dispath the QPST from Excel VBA and do the GetPort, then something is very odd.

It could be that QPST COM interface changed since this script was written. You could try

qpst = win32com.client.gencache.EnsureDispatch(
           'QPSTAtmnServer.Application')

which will attempt to create the type library for QPST. Sometimes it finds extra objects, but if not at very least you can then browse the QPST COM from python using combrowse.py (which is part of pywin32) and try to find where that function is. Combrowse is a basic COM browser, just run \Lib\site-packages\win32com\client\combrowse.py, if need more powerful the one from visual studio is probably better.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top