【发布时间】:2013-01-08 23:35:10
【问题描述】:
我正在尝试使用 python 和 ctypes 将电机控制系统拼凑在一起,我需要做的一件事是获取文本输入并将其转换为 8 位有符号整数。
以下是我尝试调用的函数的文档。应该输入到程序中的文本是'EPOS2'
数据类型定义如下图(注意'char*'相当于一个8位有符号整数)
那么如何将“EPOS2”转换为介于 -128 和 127 之间的值?
最终我想要做的是这样的:
import ctypes #import the module
lib=ctypes.WinDLL(example.dll) #load the dll
VCS_OpenDevice=lib['VCS_OpenDevice'] #pull out the function
#per the parameters below, each input is expecting (as i understand it)
#an 8-bit signed integer (or pointer to an array of 8 bit signed integers,
#not sure how to implement that)
VCS_OpenDevice.argtypes=[ctypes.c_int8, ctypes.c_int8, ctypes.c_int8, ctypes.c_int8]
#create parameters for my inputs
DeviceName ='EPOS2'
ProtocolStackName = 'MAXON SERIAL V2'
InterfaceName = 'USB'
PortName = 'USB0'
#convert strings to signed 8-bit integers (or pointers to an array of signed 8-bit integers)
#code goes here
#code goes here
#code goes here
#print the function with my new converted input parameters
print VCS_OpenDevice(DeviceName,ProtocolStackName,InterfaceName,PortName)
【问题讨论】:
-
这不是
char,而是char*。从技术上讲,它是指向char的指针,但通常用于表示以空字节或字符串结尾的chars数组。您如何尝试调用此函数?示例代码会让我们知道究竟缺少什么。 -
你能澄清一下
ctypes的用法吗?您是在调用另一个库并希望使用它来执行此操作,还是可以直接使用struct模块模拟一个结构? -
我把我目前所拥有的。希望这对我正在尝试做的事情有所启发。
标签: python ctypes signed unsigned-integer