Hen*_*ngy 5 c python arrays cython raspberry-pi
我正在使用 Raspberry Pi 与连接到 GPIO 的自定义硬件接口。控制软件是用 Python 编写的,自定义硬件的接口是用 C 编写的,因为它是一种更快的 C 实现。我现在需要开始从我的 Python 调用我的 C 函数,并且最近一直在学习如何在 Cython 中包装 C。除了将 Python 列表传递给 C 函数之外,我已经完成了所有工作。
我的自定义硬件需要发送 1 到 32 个字节的任何地方,因此使用数组。
Cython 教程和我在网上阅读的其他参考资料非常简单,不包括如何将列表传递给 C、使用 numpy(我没有使用),或者使用非常复杂的代码示例,这些示例缺乏足够的文档让我理解它适当地。
我现在拥有的是:
测试.c
#include <stdio.h>
#include "test.h"
void pop(void) {
a[0] = 0x55;
a[1] = 0x66;
a[2] = 0x77;
a[3] = '\0';
}
void putAll(int n, char c[]) {
memcpy(a, c, n);
}
char *getAll(void) {
return &a[0];
}
Run Code Online (Sandbox Code Playgroud)
测试.h
char a[4];
void putAll(int n, char[]);
char *getAll(void);
Run Code Online (Sandbox Code Playgroud)
pytest.pyx
cimport defns
# Populate C array with values
def pypop():
defns.pop()
# Pass python list to C
def pyPutAll(int n, char[:] pyc):
cdef char* c = pyc
defns.putAll(n, c)
# Get array from C
def pyGetAll():
cdef char* c = defns.getAll()
cdef bytes pyc = c
print pyc
Run Code Online (Sandbox Code Playgroud)
定义文件
cdef extern from "test.h":
char a[4]
void pop()
void putAll(int n, char c[])
char *getAll()
Run Code Online (Sandbox Code Playgroud)
使用cython.org 上的教程,我的 getAll() 和 pop() 函数可以工作,但是当我包含 putAll() 函数时(取自链接上的 process_byte_data 示例代码,在 Unicode 和传递字符串下 > 从 Python 接受字符串代码),我收到此错误:
python setup.py build_ext -i
Error compiling Cython file:
------------------------------------------------------------
...
def pyputAll(int n, char[:] pyc):
^
------------------------------------------------------------
pytest.pyx:13:25: Expected an identifier or literal
Run Code Online (Sandbox Code Playgroud)
现在,我有办法解决这个问题 - 将最多 32 个字节组合成一个 int 并作为一个 long int 传递,然后在 C 中将其分开 - 但它非常难看。
此外,我不需要 Cython 来获得任何性能提升,除了使用 C 实现的库来连接我的自定义硬件与 Python 实现的硬件。
任何帮助将不胜感激。
(编辑)解决方案
我设法让这个工作。这是我现在为需要它的任何人提供的代码。
pytest.pyx
...
def pyPutAll(int n, c):
cdef int *ptr
ptr = <int *>malloc(n*cython.sizeof(int))
if ptr is NULL:
raise MemoryError()
for i in xrange(n):
ptr[i] = c[i]
defns.putAll(n, ptr)
free(ptr)
...
Run Code Online (Sandbox Code Playgroud)
测试.c
void putAll(int n, int c[])
{
char d[n];
int i;
for (i=0;i<n;i++) {
d[i] = c[i];
}
memcpy(addr, d, n);
}
Run Code Online (Sandbox Code Playgroud)
这段代码不是最优的,因为它在 python/cython 代码中使用了 int,然后在 C 函数中将其转换为 char。pyPutAll()pytest.pyc 中的函数接受一个普通的python 列表。然后它创建一个 C 指针并分配内存。遍历列表,将每个值放入一个 C 数组,最后将指针传递给 C 函数。
它完成了工作,但我相信其他人可以提供更有效的解决方案。
马特
Dun*_*nes -1
ctypes更适合您正在尝试做的事情。
例如:(测试.py)
from ctypes import create_string_buffer, c_char_p, c_int, CDLL
libtest = CDLL("./libtest.so")
_putAll = libtest.putAll
_putAll.restype = None
_putAll.argtypes = [c_int, c_char_p]
def putAll(values):
"""Expects a bytes object, bytearray, or a list of ints."""
char_buffer = create_string_buffer(bytes(values))
_putAll(len(char_buffer), char_buffer)
getAll = libtest.getAll
getAll.restype = c_char_p
getAll.argtypes = None
Run Code Online (Sandbox Code Playgroud)
用法:
import test
test.putAll(b"hello world")
assert test.getAll() == b"hello world"
test.putAll(bytearray(b"another string"))
test.putAll([1, 2, 3, 255])
Run Code Online (Sandbox Code Playgroud)
以上是针对 python 3 的。如果您运行的是 python 2,则可以替换bytes,str但该功能不太灵活。此外,请注意create_string_buffer创建一个 C 字符串(在字符串末尾添加一个额外的 NUL 字符)。
要编译共享库,您需要执行以下操作:
gcc -fPIC -g -c -Wall test.c
gcc -shared -Wl,-soname,libtest.so.1 -o libtest.so test.o
Run Code Online (Sandbox Code Playgroud)