我正在尝试使用 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)