我想從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