interactive mode in python with ctypes???

D

digitnctu

Dear all:

I am coming with problem, to apply ctypes under interactive mode
in python.

libdll.dll is a third-party library. The below code segment will
run well under the batch mode(ie. python test.py 11060)
but when I type sequencially it doesn't work as usual. Can any
give me a hand??

# file test.py begin
from ctypes import *
lib = CDLL("libdll.dll")
def evCb(ev, clData, caData):
print "event: ", ev,"=>" , string_at(caData)

def run(port):
libinf = lib.libCreate(0)
lib.libConnect(libinf,port,0,0)
evfptr = CFUNCTYPE(None,c_char_p, c_void_p,c_void_p)
lib.libCltAddEventCallback(libinf,"AllEvents",evfptr(evCb),0)
result = c_char_p()
libbuf = raw_input("lib>")
while libbuf != "quit":
lib.libCallCommand(libinf,libbuf,0,pointer(result))
print "result: ",result.value
if libbuf == "Exit":
break
libbuf = raw_input("lib>")
lib.libDestroy(libinf)
if __name__ == "__main__":
import sys,string
run(string.atoi(sys.argv[1]))
# file test.py end

I type in interactive mode of python as below:

from ctypes import *
lib = CDLL("libdll.dll")
def evCb(ev,clData,caData):
print "event: ", ev,"=>" , string_at(caData)
libinf = lib.libCreate(0)
lib.libConnect(libinf, 11060, 0,0)
evfptr = CFUNCTYPE(None,c_char_p, c_void_p,c_void_p)
lib.libCltAddEventCallback(libinf,"AllEvents",evfptr(evCb),0)
result = c_char_p()
lib.libCallCommand(libinf,"somecmd",0,pointer(result))
lib.libDestroy(libinf)
 
G

Gabriel Genellina

libdll.dll is a third-party library. The below code segment will
run well under the batch mode(ie. python test.py 11060)
but when I type sequencially it doesn't work as usual. Can any
give me a hand??

Define "doesn't work as usual"; at least describe what actually happens
and what you expected to happen instead. If you get an exception, post the
full traceback.
run(string.atoi(sys.argv[1]))

string.atoi is deprecated eons ago; use int() instead
 
D

digitnctu

libdll.dll is a third-party library. The below code segment will
run well under the batch mode(ie. python test.py 11060)
but when I type sequencially it doesn't work as usual. Can any
give me a hand??

Define "doesn't work as usual"; at least describe what actually happens
and what you expected to happen instead. If you get an exception, post the
full traceback.
run(string.atoi(sys.argv[1]))

string.atoi is deprecated eons ago; use int() instead

"doesn't work" means there is no result without exception. The python
interpretter still run without complains; but when running under batch
mode it run as the expectation. Are there difference for python
interpretter between batch mode and interactive mode ?

Thanks for Gabriel.
 
A

ajaksu

You should get it to work with this loop (from run()):
while libbuf != "quit":
lib.libCallCommand(libinf,libbuf,0,pointer(result))
print "result: ",result.value
if libbuf == "Exit":
break
libbuf = raw_input("lib>")
 
D

digitnctu

En Wed, 26 Dec 2007 12:57:44 -0300, <[email protected]> escribi¨®:
Define "doesn't work as usual"; at least describe what actually happens
and what you expected to happen instead. If you get an exception, post the
full traceback.
run(string.atoi(sys.argv[1]))

string.atoi is deprecated eons ago; use int() instead

"doesn't work" means there is no result without exception. The python
interpretter still run without complains; but when running under batch
mode it run as the expectation. Are there difference for python
interpretter between batch mode andinteractivemode ?

Thanks for Gabriel.
sorry, I explain more precisely.
"No result" means the callback function evCB will not be called as
it in batch mode.
For callback function, are there difference for python interpretter
between batch and interactive mode ?
 
D

Dennis Lee Bieber

sorry, I explain more precisely.
"No result" means the callback function evCB will not be called as
it in batch mode.
For callback function, are there difference for python interpretter
between batch and interactive mode ?
What interactive environment are you running under? Opening a plain
command line shell and typing "python"? Running under IDLE's interactive
window? PythonWin's interactive window?

Note that you state this is a /callback/ function; as such it should
be called when some underlying code detects the event for which the
function is registered. Could it be that this underlying code does not
get to run when using interactive mode (perhaps because the interpreter
is blocked on a console read statement?) OR, more likely, looking at the
original post -- YOU are not giving the system time to react to anything
-- your "batch" version will block on a raw_input() call, which probably
will free up whatever underlying code triggers the callback. Your
"interactive" version creates AND DESTROYS the entire linkage at
whatever speed you can type them... Try stuffing a few time.sleep(5)
calls between each of the input statements... Or typing in what should
have been the first try:

import test
test.run(11060)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top