【发布时间】:2020-07-20 00:25:47
【问题描述】:
我正在尝试创建一个 Python 蓝牙包装器来包装 C++ 类。这是我的 SWIG 接口文件:
%module blsdk
%include "pyabc.i"
%include "std_vector.i"
%include "cstring.i"
%include "cpointer.i"
%include "typemaps.i"
%include serialport.i
%include exploresearch.i
这是我的serialport.i
%module serialport
%{
#include <string>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <termios.h>
#include <sys/poll.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <assert.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
#include <bluetooth/rfcomm.h>
#include "BTSerialPortBinding.h"
%}
%include "BTSerialPortBinding.h"
我的 BTSerialPortBinding.h 有这些功能:
static BTSerialPortBinding *Create(std::string address, int channelID);
int Connect();
void Close();
int Read(char *buffer, int length);
void Write(const char *write_buffer, int length);
bool IsDataAvailable();
如何包装 int Read(char* buffer, int length) 函数?我想将 char* 缓冲区作为输出,将长度作为输入。我试图将读取函数定义为 int Read(char* OUTPUT, int length) 但这会产生错误:TypeError: a bytes-like object is required, not 'str' 在我的程序中,因为我需要 Python 中的字节对象。任何帮助将不胜感激。
【问题讨论】: