파이썬 및 ctypes를 사용하여 모터 제어 시스템을 함께 패치하려고하는데 필자가해야 할 일 중 하나는 텍스트 입력을 받아이를 8로 변환하는 것입니다. 비트 부호있는 정수.파이썬에서 문자열을 8 비트 부호있는 정수로 변환
다음은 내가 전화하려고 해요 함수에 대한 설명서입니다. 프로그램에 입력해야 할 텍스트가 'EPOS2'
이다아래와 같이 데이터 타입 정의 ("숯 * '는 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'에 대한 포인터이지만 일반적으로 Null Byte 또는 String으로 끝나는'chars '의 배열을 나타냅니다. 어떻게이 함수를 호출하려고합니까? 예제 코드는 정확히 무엇이 누락되었는지 알려줍니다. – FrankieTheKneeMan
여기서'ctypes '의 사용법을 명확하게 설명해 주시겠습니까? 다른 라이브러리를 호출하고 그것을 사용하고 싶습니까? 아니면 실제로'struct' 모듈을 사용하여 구조체를 에뮬레이트 할 수 있습니까? –
나는 현재 내가 가진 것을 넣는다. 바라기를 이것은 내가하려고하는 것에 대해 약간의 빛을 비춰줍니다. – Chris