我想从Python(3.2)向使用ctypes的C发送2个字符串。这是我的Raspberry Pi项目中的一小部分。为了测试C函数是否正确接收了字符串,我将其中的一个放置在文本文件中。使用ctypes将python字符串对象转换为c char *
Python代码
string1 = "my string 1"
string2 = "my string 2"
# create byte objects from the strings
b_string1 = string1.encode('utf-8')
b_string2 = string2.encode('utf-8')
# send strings to c function
my_c_function(ctypes.create_string_buffer(b_string1),
ctypes.create_string_buffer(b_string2))
C代码
void my_c_function(const char* str1, const char* str2)
{
// Test if string is correct
FILE *fp = fopen("//home//pi//Desktop//out.txt", "w");
if (fp != NULL)
{
fputs(str1, fp);
fclose(fp);
}
// Do something with strings..
}
问题
只有字符串的第一个字母出现在文本文件中。
我已经尝试了很多方法来转换python字符串对象与ctypes。
- ctypes.c_char_p
- ctypes.c_wchar_p
- ctypes.create_string_buffer
有了这些转换我不断收到错误 “错误类型” 或“预期而不是STR例如字节或整数地址”。
我希望有人能告诉我哪里出问题了。 在此先感谢。
设置'my_c_function.argtypes = [ctypes.c_char_p,ctypes.c_char_p]'。然后,因为参数是'const',所以简单地称之为'my_c_function(b_string1,b_string2)'。 – eryksun 2014-11-25 14:38:06
仅供参考,一个文字反斜线字符需要转义为'“\\”',但它不是正斜杠所必需的。它只是'“/home/pi/Desktop/out.txt”'。 – eryksun 2014-11-25 14:42:35
@eryksun感谢您的回复。它现在起作用了,我完全忘记了我的argtypes仍然设置在c_wchar_p上。关于斜杠,我总是让它们混淆起来。 – LittleOne 2014-11-25 15:03:12