【问题标题】:How to pass PyHANDLE object to CP2108 dll functions (CP210xRuntime.dll) in Python?如何在 Python 中将 PyHANDLE 对象传递给 CP2108 dll 函数(CP210xRuntime.dll)?
【发布时间】:2015-09-29 11:44:38
【问题描述】:

我需要使用 Python 控制 CP2108 芯片 Latch。 C# 示例代码(来自制造商示例源):

comString.Format(_T("\\\\.\\COM%d"), m_COMPort.GetCurSel() + 1);
//Open a handle the the device specified
HANDLE hDevice = CreateFile(comString,
                            GENERIC_READ | GENERIC_WRITE,
                            0,
                            0,
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
                            0);

//If the handle is valid, then it opened
if (hDevice != INVALID_HANDLE_VALUE)
{
    WORD latch = 0;
    //Read the latch
    if (CP210xRT_ReadLatch(hDevice, &latch) != CP210x_SUCCESS)
    {.....}
 }

我的 Python 代码:

import win32file
import win32con
import ctypes

cp201x_dll = ctypes.windll.LoadLibrary('CP210xRuntime.dll')

lp_latch = ctypes.c_int(0)

h_device = win32file.CreateFile(r'\\.\\COM37',
                               win32con.GENERIC_READ|win32con.GENERIC_WRITE,
                               0,
                               None,
                               win32con.OPEN_EXISTING,
                               win32con.FILE_ATTRIBUTE_NORMAL|win32con.FILE_FLAG_OVERLAPPED,
                               None)



cp201x_dll.CP210xRT_ReadLatch(h_device, ctypes.byref(lp_latch))

print 'VALUE: {}'.format(lp_latch)

但我得到一个错误:

Traceback (most recent call last):
  File "D:/Dropbox/ElgsisTests/src/lib/cp210x/practise.py", line 27, in <module>
    cp201x_dll.CP210xRT_ReadLatch(h_device, ctypes.byref(lp_latch))
ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1

我试图传递 ctypes 整数,但我得到的 Latch 值为零。我想这是因为这个 CP210xRuntime.dll 需要一个关于 COM 端口的信息。

来自 API 手册:

 3.1. CP210xRT_ReadLatch
    Description:

 Gets the current port latch value from the device.

    Supported Devices: CP2103, CP2104, CP2105, CP2108
    Location: CP210x Runtime DLL
    Prototype: CP210x_STATUS CP210xRT_ReadLatch(HANDLE Handle, LPWORD Latch)
    Parameters: 1. Handle—Handle to the Com port returned by CreateFile().
    2. Latch—Pointer for 4-byte return GPIO latch value [Logic High = 1, Logic Low = 0].
    Return Value: CP210x_STATUS = CP210x_SUCCESS,
    CP210x_INVALID_HANDLE,
    CP210x_DEVICE_IO_FAILED
    CP210x_FUNCTION_NOT_SUPPORTED

所以也许有人知道如何正确传递它?

【问题讨论】:

  • 让我们看看...&gt;&gt;&gt; dir(h_device) ....哦,它有一个handle属性并实现__int__,所以使用h_device.handleint(h_device),最好是后者@ 987654321@.
  • 是的。我确实发现它是一个 int... 而现在 CP210xRT_ReadLatch 返回错误代码 0xFF 而不是返回值列表...
  • 现在它的工作:)

标签: python dll ctypes


【解决方案1】:

更新了 Python 代码:

handle = ctypes.c_long(int(h_device))
status = cp201x_dll.CP210xRT_ReadLatch(handle, ctypes.byref(lp_latch))
print 'STATUS: {}'.format(status)
print 'VALUE: {}'.format(lp_latch)

并且确实收到了正确的值

【讨论】:

  • 您不需要在c_long 中包含文件句柄。内核对象句柄总是适合默认的c_int 类型,在Windows 上它总是和c_long 一样大小。另外,如果您想在技术上正确,Windows HANDLEvoid * 的 typedef,因此您将使用 c_void_p。这是因为 Windows 中有很多句柄类型,例如HMODULE,实际上是指针——只是不是文件句柄!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-07
  • 2011-04-02
  • 2021-11-26
  • 1970-01-01
  • 2022-12-05
相关资源
最近更新 更多