开发者

Can't retieve a value from a C function in a Python script but can in console

I am using python (Pydev) to communicate with stepper motors with a DLL using ctypes. I am trying to retrieve the position of the motor. The thing is that if I type the script in line by line in the console it works but if I run the script it always gives me 0. This is my code:

impor开发者_StackOverflow社区t ctypes

AC=windll.hvpositionerv2
adr=addressof
class PositionerInfo (Structure):
    _fields_=[("id",c_int),("locked",c_bool)]

ptr=POINTER(PositionerInfo)()
devCount=AC.PositionerCheck(adr(ptr))
print("Devices found: "+repr(devCount))
for i in range(min(adr(ptr),devCount)):
     print("ID: "+repr(ptr[i].id)+" Locked? "+repr(ptr[i].locked))

handle=POINTER(c_int)()
AC.PositionerConnect(0,adr(handle))

So far the program has only connected to the device. Now, the problem is this:

ypos=c_int()
AC.PositionerGetPosition(handle,1,adr(ypos)

print(ypos.value)

This always prints 0 if I run the script (in Eclipse) but works in the console.


My best guess: this is a timing issue. After connecting to the hardware, it takes a certain amount of time before it reports the motor position, and before that first response, you will always get 0. Try adding a 1 second sleep before AC.PositionerGetPosition.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜