I have the below python script which I am using to collect data from a microcontroller using SPI communication. That part is working fine, however, now I am trying to implement CRC function from a c file.
import
ctypes
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
INTERRUPT_GPIO
=
12
pi.set_mode(INTERRUPT_GPIO, pigpio.
INPUT
)
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):
data
=
[
0
]
*
1024
spi.xfer([
0x02
])
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,
len
(data))
f.write(
"\n"
)
f.write(
"\n"
.join([
str
(x)
for
x
in
values]))
print
(C)
cb1
=
pi.callback(INTERRUPT_GPIO, pigpio.RISING_EDGE, spi_process)
while
True
:
time.sleep(
1
)
The C function that I am trying to invoke from the python script is
crc32Word(uint32_t crc, const void
*
buffer
, uint32_t size)
But when I run the above script, I receive error as:
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
50
,
in
spi_process
C
=
lib.crc32Word(
0xffffffff
,data,
len
(data))
ctypes.ArgumentError: argument
2
: <
class
'TypeError'
>: wrong
type
One way to solve this is to use data = bytes([0]*1024)
. But if I use this method, the file stores 0s even if I am actually receiving data correctly (checked using logic analyzer).
Can someone please help? Thanks.