我有這個python代碼:
import ctypes
lib = ctypes.CDLL('/home/pi/serial_communication/crc.so')
C=0
def spi_process(gpio,level,tick):
print("Detected")
data = [0]*1024
with open(output_file_path(), 'w') as f:
for x in range(1):
spi.xfer2(data)
values = struct.unpack("<" "I"*256, bytes(data))
C = lib.crc32Word(0xffffffff,data,1024)
f.write("\n")
f.write("\n".join([str(x) for x in values]))
print(C)
input("Press Enter to start the process ")
cb1=pi.callback(INTERRUPT_GPIO, pigpio.RISING_EDGE, spi_process)
while True:
lib.crc32Word.restype=ctypes.c_int
lib.crc32Word.argtypes=[ctypes.c_int, ctypes.POINTER(ctypes.c_void_p), ctypes.c_int]
time.sleep(1)
但是當我運行它時,我收到了這個錯誤:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_innerself.run()
File "/usr/lib/python3/dist-packages/pigpio.py", line 1213, in run cb.func(cb.gpio, newLevel, tick)
File "2crc_spi.py", line 47, in spi_process
C=lib.crc32Word(0xffffffff,data,1024)
ctypes.ArgumentError: argument 2: <class 'TypeError'>: LP_c_long instance instead of list
我正在嘗試從 ac 庫中呼叫此 C 函式:
uint32_t crc32Word(uint32_t crc, const void *buffer, uint32_t size)
有人可以幫忙嗎?謝謝。
uj5u.com熱心網友回復:
問題可以簡化為以下代碼。第二個引數不正確,因為ctypes.POINTER(ctypes.c_void_p)將用于 C void**not void*。其他引數使用有符號與無符號型別,因此也已更正。data必須是bytes或者一個陣列,ctypes例如(c_uint8 * 1024)()適合c_void_p。
lib = ctypes.CDLL('/home/pi/serial_communication/crc.so')
lib.crc32Word.argtypes = ctypes.c_uint32, ctypes.c_void_p, ctypes.c_uint32
lib.crc32Word.restype = ctypes.c_uint32
data = bytes([0] * 1024)
crc = lib.crc32Word(0xffffffff, data, len(data))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/434833.html
標籤:Python C python-3.x 类型
