Python Forum
"<class 'typeerror'>: don't know how to convert" error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: "<class 'typeerror'>: don't know how to convert" error (/thread-36513.html)



"<class 'typeerror'>: don't know how to convert" error - GiggsB - Feb-28-2022

Hi,
I have a .c library which I want to use through my python code. The c function that I am calling is: crc32Word(uint32_t crc, const void *buffer, uint32_t size).

But when I write in my python code as follows:
from ctypes import *
so_file = "/home/pi/serial_communication/crc.so"
crc = CDLL(so_file)

INTERRUPT_GPIO=12

#We only have SPI bus 0 available to us on the Pi
bus = 0
#Device is the chip select pin. Set to 0 or 1, depending on the connections
device = 0
# Enable SPI
spi = spidev.SpiDev()
# Open a connection to a specific bus and device (chip select pin)
spi.open(bus, device)
# Set SPI speed and mode
spi.max_speed_hz = 4000000
spi.mode = 0
 
pi = pigpio.pi()
if not pi.connected:
   exit()

pi.set_mode(INTERRUPT_GPIO, pigpio.INPUT)

C=0
 
def output_file_path():
    return os.path.join(os.path.dirname(__file__),
               datetime.datetime.now().strftime("%dT%H.%M.%S") + ".csv")
 
def spi_process(gpio,level,tick):
    print("Detected")
    data = [0]*1024
    #spi.xfer([0x02])
    with open(output_file_path(), 'w') as f:
        t1=datetime.datetime.now()
        for x in range(1):
            spi.xfer2(data)
            values = struct.unpack("<" +"I"*256, bytes(data))
            C = crc.crc32Word(0xffffffff,data,1024)
            f.write("\n")
            f.write("\n".join([str(x) for x in values]))
        t2=datetime.datetime.now()
        print(t2-t1)
        print(C)

input("Press Enter to start the process ")
print("SM1 Process started...")
spi.xfer2([0x01])

cb1=pi.callback(INTERRUPT_GPIO, pigpio.RISING_EDGE, spi_process)

while True:
   time.sleep(1)
I get error as <class 'typeerror'>: don't know how to convert parameter 2. Can anybody help how can I solve this issue? Thank you.


RE: "<class 'typeerror'>: don't know how to convert" error - GiggsB - Feb-28-2022

Hello,

I updated my code to this:
import ctypes

lib = ctypes.CDLL('/home/pi/serial_communication/crc.so')

INTERRUPT_GPIO=12

#We only have SPI bus 0 available to us on the Pi
bus = 0
#Device is the chip select pin. Set to 0 or 1, depending on the connections
device = 0
# Enable SPI
spi = spidev.SpiDev()
# Open a connection to a specific bus and device (chip select pin)
spi.open(bus, device)
# Set SPI speed and mode
spi.max_speed_hz = 4000000
spi.mode = 0
 
pi = pigpio.pi()
if not pi.connected:
   exit()

pi.set_mode(INTERRUPT_GPIO, pigpio.INPUT)

C=0
 
def output_file_path():
    return os.path.join(os.path.dirname(__file__),
               datetime.datetime.now().strftime("%dT%H.%M.%S") + ".csv")
 
def spi_process(gpio,level,tick):
    print("Detected")
    data = [0]*1024
    #spi.xfer([0x02])
    with open(output_file_path(), 'w') as f:
        t1=datetime.datetime.now()
        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]))
        t2=datetime.datetime.now()
        print(t2-t1)
        print(C)

input("Press Enter to start the process ")
print("SM1 Process started...")
spi.xfer2([0x01])

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)
And now I get this error, I am unable to find the solution for this error. Any help? please.
Error:
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
Thanks.


RE: "<class 'typeerror'>: don't know how to convert" error - ndc85430 - Feb-28-2022

Post the entire traceback please. Why would you post a screenshot of text, rather than just posting the text itself?


RE: "<class 'typeerror'>: don't know how to convert" error - GiggsB - Feb-28-2022

(Feb-28-2022, 06:11 PM)ndc85430 Wrote: Post the entire traceback please. Why would you post a screenshot of text, rather than just posting the text itself?

Hi ndc85430,
Ok, noted...I will keep that in mind next time.
However, I was able to fix the issue, just a couple of minutes back. But thanks for the help!
The issue was resolved by changing to the following code. The 2nd parameter was incorrect as ctypes.POINTER(ctypes.c_void_p) would be used for C void** not void*. The other parameters were using signed vs. unsigned types so that has been corrected as well. The data should be either bytes or a ctypes array such as (c_uint8 * 1024)() to be suitable for 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))