Python中的dll调用-ni.845x驱动编写

ctypes 是 Python 的外部函数库。它提供了与 C 兼容的数据类型,并允许调用 DLL 或共享库中的函数。可使用该模块以纯 Python 形式对这些库进行封装。

 

 

ctypes.byref(obj[, offset])--传递引用数据

返回指向 obj 的轻量指针,该对象必须为一个 ctypes 类型的实例。 offset 默认值为零,且必须为一个将被添加到内部指针值的整数。

ctypes.create_string_buffer(init_or_sizesize=None)--传递块数据(数组,字符串等)

此函数会创建一个可变的字符缓冲区。 返回的对象是一个 c_char 的 ctypes 数组。

init_or_size 必须是一个指明数组大小的整数,或者是一个将被用来初始化数组条目的字节串对象。

如果将一个字节串对象指定为第一个参数,则将使缓冲区大小比其长度多一项以便数组的最后一项为一个 NUL 终结符。 可以传入一个整数作为第二个参数以允许在不使用字节串长度的情况下指定数组大小。

参考代码如下:

'''
作者:***
功能:ni845x驱动程序
日期:2021/6/3
版本:V0
'''
from tkinter import messagebox
import ctypes
ni845x = ctypes.WinDLL('Ni845x.dll')

Address = 0xb6
ClockRate = 100
errorStr = ''

pFirstDevice = ctypes.create_string_buffer(260, '\0')
pErrorStr = ctypes.create_string_buffer(1024,'\0')

pDeviceHandle = ctypes.c_uint32(0)
pConfigHandle = ctypes.c_uint32(0)
pNumberFound = ctypes.c_uint32(0)

pWriteData = ctypes.create_string_buffer(256, '\0')
pReadData = ctypes.create_string_buffer(512, '\0')
readSize = ctypes.c_uint32(0)
writeLength = 0
readData = []

def openDevice(address):
    errorStr=''
    checkError(ni845x.ni845xFindDevice(pFirstDevice, ctypes.byref(pDeviceHandle), ctypes.byref(pNumberFound)))
    checkError(ni845x.ni845xCloseFindDeviceHandle(pDeviceHandle))
    checkError(ni845x.ni845xOpen(pFirstDevice, ctypes.byref(pDeviceHandle)))

    checkError(ni845x.ni845xI2cConfigurationOpen(ctypes.byref(pConfigHandle)))
    setAddress(address)
    setClock(ClockRate)

def closedDevice():
    ni845x.ni845xI2cConfigurationClose(pConfigHandle)
    ni845x.ni845xClose(pDeviceHandle)

def write(writeData):
    writeLength = len(writeData)
    for index in range(writeLength):
        pWriteData[index] = writeData[index]

    if writeLength > 0:
        checkError(ni845x.ni845xI2cWrite(pDeviceHandle, pConfigHandle, writeLength, pWriteData))

def read(readLength):
    readData=[]
    if readLength > 0:
        checkError(ni845x.ni845xI2cRead(pDeviceHandle, pConfigHandle, readLength, ctypes.byref(readSize), pReadData))
        if readSize.value > 0:
            readData = [pReadData[index] for index in range(readSize.value)]
    return readData

def writeRead(writeData, readLength):
    readData = []
    writeLength = len(writeData)

    for index in range(writeLength):
        pWriteData[index] = writeData[index]

    if readLength > 0:
        checkError(ni845x.ni845xI2cWriteRead(pDeviceHandle, pConfigHandle, writeLength, pWriteData, readLength,
                                             ctypes.byref(readSize), pReadData))
        if readSize.value > 0:
            readData = [pReadData[index] for index in range(readSize.value)]
    return readData

def setAddress(address):
    checkError(ni845x.ni845xI2cConfigurationSetAddress(pConfigHandle, address >> 1))

def setClock(clockRate):
    checkError(ni845x.ni845xI2cConfigurationSetClockRate(pConfigHandle, clockRate))

def checkError(errorCode):
    if errorCode != 0:
        ni845x.ni845xStatusToString(errorCode, 1024, pErrorStr)
        errorStr = pErrorStr.value
        closedDevice()

 

参考资料:https://docs.python.org/zh-cn/3.9/library/ctypes.html

posted @ 2021-06-05 17:09  *雷子*  阅读(365)  评论(0编辑  收藏  举报