Python Calling An SDK Function In DLL Of A Sensor That Takes Pointer IP And Port And Return A Void* (void* A Handle To The Profile Sensor.)
Solution 1:
I want to start by pointing out [Python 3.Docs]: ctypes - A foreign function library for Python.
I'm going to post an answer based on some assumptions:
- The sensor you're talking about is the same one as described in [FoxControls]: acuity-ap820-users-manual.pdf. If this isn't true, the answer is useless
According to above doc, EthernetScanner_GetConnectStatus's possible return statuses are (emphasis is mine):
Status is a unsigned integer used as a buffer to contain the status code when the function returns.
The status codes are:- 0: Disconnected
- 1: Disconnecting
- 2: Connecting
- 3: Connected
Based on the above, and the fact that the connect timeout is 0, I assumed (nothing in the doc to support it, though) that it tries to connect in non blocking mode:
- EthernetScanner_Connect returns immediately (meaning that before attempting to connect - it "schedules" a connection operation if you will) a non NULL pointer. For a non blocking (async) IO example, check [MS.Docs]: connect function
- The (above) pointer should be queried (periodically) using EthernetScanner_GetConnectStatus. That's why it makes sense for the pointer to be non NULL (as when EthernetScanner_Connect returns, the connection attempt result is not yet known)
Here's a piece of code that is supposed to deal with the problem (needless to say that it's blind coding - meaning that I didn't actually test it).
As a consequence of the 2 assumption, if passing a timeout greater than 0, EthernetScanner_Connect should return NULL.
code.py:
#!/usr/bin/env python3
import sys
import ctypes
import time
DLL_NAME = "c:\\EthernetScanner.dll"
CONNECT_STATUS_DISCONNECTED = 0
CONNECT_STATUS_DISCONNECTING = 1
CONNECT_STATUS_CONNECTING = 2
CONNECT_STATUS_CONNECTED = 3
CONNECT_STATUSES_PENDING = [
CONNECT_STATUS_CONNECTING,
CONNECT_STATUS_DISCONNECTING,
]
def main():
connect_timeout_msec = 0
nb_mode = connect_timeout_msec == 0
dll = ctypes.WinDLL(DLL_NAME)
EthernetScanner_Connect = dll.EthernetScanner_Connect
EthernetScanner_Connect.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_uint32]
EthernetScanner_Connect.restype = ctypes.c_void_p
EthernetScanner_GetConnectStatus = dll.EthernetScanner_GetConnectStatus
EthernetScanner_GetConnectStatus.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_uint32)]
EthernetScanner_Disconnect = dll.EthernetScanner_Disconnect
EthernetScanner_Disconnect.argtypes = [ctypes.c_void_p]
EthernetScanner_Disconnect.restype = ctypes.c_uint32
ptr = EthernetScanner_Connect(b"192.168.100.1", b"32001", connect_timeout_msec)
if nb_mode:
print("Non blocking mode: EthernetScanner_Connect returned {:}".format(ptr))
poll_timeout_sec = 0.5
connect_status = ctypes.c_int(CONNECT_STATUS_CONNECTING)
while connect_status.value in CONNECT_STATUSES_PENDING:
time.sleep(poll_timeout_sec)
EthernetScanner_GetConnectStatus(ptr, ctypes.byref(connect_status))
if connect_status.value != CONNECT_STATUS_CONNECTED:
print("Couldn't connect (status: {:})!".format(connect_status.value))
return
else:
print("Blocking mode: EthernetScanner_Connect returned {:}".format(ptr))
if not ptr:
print("Couldn't connect!")
return
# Do something with the sensor (pointer)
EthernetScanner_Disconnect(ptr)
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
print("Done.")
Post a Comment for "Python Calling An SDK Function In DLL Of A Sensor That Takes Pointer IP And Port And Return A Void* (void* A Handle To The Profile Sensor.)"