Python Forum
"<class 'typeerror'>: don't know how to convert" error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"<class 'typeerror'>: don't know how to convert" error
#1
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.
Reply
#2
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.
Reply
#3
Post the entire traceback please. Why would you post a screenshot of text, rather than just posting the text itself?
Reply
#4
(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))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in class: TypeError: 'str' object is not callable akbarza 2 501 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,227 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,932 Oct-17-2022, 06:29 PM
Last Post: paulo79
  KivyMD App to APK Convert Error Nick_MC 0 2,312 Jul-18-2022, 08:59 AM
Last Post: Nick_MC
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,497 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  TypeError: sequence item 0: expected str instance, float found Error Query eddywinch82 1 5,098 Sep-04-2021, 09:16 PM
Last Post: eddywinch82
  error in class non_name092 1 1,911 Sep-02-2020, 05:42 PM
Last Post: bowlofred
  Python Error- TypeError: ('Params must be in a list, tuple, or Row', 'HY000') DarkCoder2020 3 5,562 Jul-29-2020, 12:02 AM
Last Post: Larz60+
  Getting error "TypeError: 'int' object is not callable" while using ylim maxprime_bhisham 2 3,821 Apr-26-2020, 10:55 AM
Last Post: DataAnalyticsIreland
  Error in the code ->ValueError: could not convert string to float: ' ' eagleboom 1 3,212 Nov-29-2019, 06:19 AM
Last Post: ThomasL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020