我刚接触Python。我正在尝试通过其DLL库连接到激光传感器。该库中的SDK函数以C ++语言编译。
通过在python中使用ctypes,我的第一个尝试是调用EthernetScanner_Connect函数。功能参数如下:
void* EthernetScanner_Connect(char *chIP, char *chPort, int iTimeOut)
根据c ++中的函数说明,如果连接失败,则应返回NULL指针,否则应将其返回给配置文件传感器的句柄,该传感器将在其他函数中使用。
直到现在我在python上的代码:
from ctypes import *
lib=windll.LoadLibrary("c:\\EthernetScanner.dll")
if lib:
print("the Library is loaded")
EthernetScanner_Connect = getattr(lib,"EthernetScanner_Connect")
EthernetScanner_Connect.restype = c_void_p
EthernetScanner_Connect.argtypes =[c_char_p,c_char_p,c_int]
x = EthernetScanner_Connect(b"193.164.200.1\0",b"32407\0",0)
print(x)
当我期望此函数为NULL时,因为我尚未连接到传感器,但是它给了我以下信息:
the Library is loaded
45940800
答案 0 :(得分:0)
我想先指出[Python 3.Docs]: ctypes - A foreign function library for Python。
我将基于一些假设发布答案:
根据上述文档, EthernetScanner_GetConnectStatus 的可能返回状态为(强调是我的):
Status是一个无符号整数,用作缓冲区时包含状态码的状态 函数返回。
状态代码为:
- 0:已断开连接
- 1:断开连接
- 2:正在连接
- 3:已连接
基于上述以及连接超时为0的事实,我假设( doc 中没有任何内容支持)在非阻止模式下:
这里有一段应该用来解决该问题的代码(不必说这是盲编码,这意味着我实际上并未对其进行测试)。
由于2 nd 的假设,如果传递超时大于0 ,则 EthernetScanner_Connect 应该 >返回 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.")