c++生成动态dll,python调用该dll如何传参
这是dll链接
double* disp11(unsigned char* rev)
{
union gyro
{
char ch[4];
float f;
int si;
};
union gyro Gyrox[4];
unsigned short int data_temp1;
int i, j;
static double angleRT[16];
unsigned short int YearMonth; //年
unsigned short int Product_ID_T; //产品编号
i = 2;
Product_ID_T = rev[2] + rev[3] * 256; //产品编号
YearMonth = rev[4] + rev[5] * 256; //年
i = 11;
j = 0;
Gyrox[0].ch[0] = rev[i++]; //经度
Gyrox[0].ch[1] = rev[i++];
Gyrox[0].ch[2] = rev[i++];
Gyrox[0].ch[3] = rev[i++];
angleRT[0] = Gyrox[0].f;
i = 15;
j = 0;
Gyrox[0].ch[j++] = rev[i++]; //纬度
Gyrox[0].ch[j++] = rev[i++];
Gyrox[0].ch[j++] = rev[i++];
Gyrox[0].ch[j++] = rev[i++];
angleRT[1] = Gyrox[0].f;
i = 19;
j = 0;
Gyrox[0].ch[j++] = rev[i++]; //高度,m
Gyrox[0].ch[j++] = rev[i++];
Gyrox[0].ch[j++] = rev[i++];
Gyrox[0].ch[j++] = rev[i++];
angleRT[2] = Gyrox[0].f;
i = 23;
j = 0;
Gyrox[0].ch[j++] = rev[i++]; //航向
Gyrox[0].ch[j++] = rev[i++];
Gyrox[0].ch[j++] = rev[i++];
Gyrox[0].ch[j++] = rev[i++];
angleRT[3] = Gyrox[0].f;
i = 27;
j = 0;
Gyrox[0].ch[j++] = rev[i++]; //步数
Gyrox[0].ch[j++] = rev[i++];
Gyrox[0].ch[j++] = rev[i++];
Gyrox[0].ch[j++] = rev[i++];
angleRT[4] = Gyrox[0].f;
//步幅度,低字节
data_temp1 = rev[32] * 256 + rev[31];
angleRT[5] = (double)data_temp1;
angleRT[6] = (double)rev[33]; //卫星数
angleRT[7] = (double)rev[34]; //状态字1
angleRT[8] = (double)rev[35]; //状态字2
angleRT[9] = (double)Product_ID_T; //状态字2
angleRT[10] = (double)YearMonth; //年
angleRT[11] = (double)rev[6]; //月
angleRT[12] = (double)rev[7]; //日
angleRT[13] = (double)rev[8]; //时
angleRT[14] = (double)rev[9]; //分
angleRT[15] = (double)rev[10]; //秒
return angleRT;
}
这是我使用python调用的代码
from ctypes import *
dll = CDLL('./Dll1.dll')
print(dir(dll))
print(dll.disp11([0xAA,0x55,0xAD,0x02,0xBC,0x08,0x01,0x06,0x08,0x00,0x00,0x08,0x33,0xD6,0x42,0xF0,0x97,0xEF,0x41,0x24,0x18,0xB7,0x43,0xEF,0x69,0x56,0x43,0x00,0x00,0x50,0x41,0xA8,0x02,0x00,0x00,0x01,0xEF]))
返回应该是个数组,但是接受到却是一个整数,我该如何取出dll返回参数
没有环境,没测试过,试试下面代码:
dll = CDLL('./Dll1.dll')
print(dir(dll))
dll.disp11.argtypes = [POINTER(c_char)]
dll.disp11.restype = POINTER(c_double)
res = dll.disp11([0xAA,0x55,0xAD,0x02,0xBC,0x08,0x01,0x06,0x08,0x00,0x00,0x08,0x33,0xD6,0x42,0xF0,0x97,0xEF,0x41,0x24,0x18,0xB7,0x43,0xEF,0x69,0x56,0x43,0x00,0x00,0x50,0x41,0xA8,0x02,0x00,0x00,0x01,0xEF])
print(res[0])
print(res[1])
print(res[2])
...
print(res[15])
先用argtypes确定各个参数的类型,然后传的时候再指定编码方式(encode写到参数项里就好)
array=dll.displl([0xAA,0x55,0xAD,0x02,0xBC,0x08,0x01,0x06,0x08,0x00,0x00,0x08,0x33,0xD6,0x42,0xF0,0x97,0xEF,0x41,0x24,0x18,0xB7,0x43,0xEF,0x69,0x56,0x43,0x00,0x00,0x50,0x41,0xA8,0x02,0x00,0x00,0x01,0xEF])
你需要先检查一下cdll内函数返回值类型,看起来类型不对
dll.disp11.restype
如果上面的类型是int,说明有问题,需要手动修改成指针
dll.disp11.restype=POINTER(c_double)
dll.disp11.argtypes=[POINTER(c_double)]
然后按照正常索引即可
result = dll.disp11((c_double*37)(xx,xx,xx))
result[0]
result[1]
from ctypes import *
ten = c_char*37#构造数组对象
a=CDLL(r"D:\Users\Desktop\Dll1.dll")
arg1=(
0xAA,0x55,0xAD,0x02,0xBC,0x08,0x01,0x06,
0x08,0x00,0x00,0x08,0x33,0xD6,0x42,0xF0,
0x97,0xEF,0x41,0x24,0x18,0xB7,0x43,0xEF,
0x69,0x56,0x43,0x00,0x00,0x50,0x41,0xA8,
0x02,0x00,0x00,0x01,0xEF)
arg2=(170, 85, 173, 2, 188, 8, 1, 6,
8, 0, 0, 8, 51, 214, 66, 240,
151, 239, 65, 36, 24, 183, 67,
239, 105, 86, 67, 0, 0, 80, 65,
168, 2, 0, 0, 1, 239)
li=ten(*arg1)
a.disp11.restype = c_long*15
x=a.disp11(li)
print(list(x))
返回的数据是数组,如解决了问题,请采纳
from ctypes import *
dll = CDLL('./Dll1.dll')
print(dir(dll))
#print(dll.disp11([0xAA,0x55,0xAD,0x02,0xBC,0x08,0x01,0x06,0x08,0x00,0x00,0x08,0x33,0xD6,0x42,0xF0,0x97,0xEF,0x41,0x24,0x18,0xB7,0x43,0xEF,0x69,0x56,0x43,0x00,0x00,0x50,0x41,0xA8,0x02,0x00,0x00,0x01,0xEF]))
pfloat = dll.disp11([0xAA,0x55,0xAD,0x02,0xBC,0x08,0x01,0x06,0x08,0x00,0x00,0x08,0x33,0xD6,0x42,0xF0,0x97,0xEF,0x41,0x24,0x18,0xB7,0x43,0xEF,0x69,0x56,0x43,0x00,0x00,0x50,0x41,0xA8,0x02,0x00,0x00,0x01,0xEF])
i = 0
while i < 16:
print(pfloat.contents[i])
i += 1
类中声明一个__str__函数,用来返回类的字符串输出格式
class face():
featurename = none
index = 1
instancename ='a-bushing_s-20151111-1'
isreferencerep = false
pointon = ((-1.268087, -19.208438, -16.0),)
def __str__(self):
#这里你可以根据需要拼装s变量
s = "({'featurename': none, 'index': 1, 'instancename': 'a-bushing_s-20151111-1', 'isreferencerep': false, 'pointon': ((-1.268087, -19.208438, -16.0),)})"
return s
a = [face(),face()]
print(a[1])