调用过程中出现一个问题,当调用
typedef struct {
unsigned int busType;
union {
struct {
unsigned int bitRate;
unsigned char sjw;
unsigned char tseg1;
unsigned char tseg2;
unsigned char sam; // 1 or 3
unsigned char outputMode;
unsigned char reserved[7];
unsigned char canOpMode;
} can;
struct {
unsigned int arbitrationBitRate;
unsigned char sjwAbr;
unsigned char tseg1Abr;
unsigned char tseg2Abr;
unsigned char samAbr;
unsigned char outputMode;
unsigned char sjwDbr;
unsigned char tseg1Dbr;
unsigned char tseg2Dbr;
unsigned int dataBitRate;
unsigned char canOpMode;
} canFD;
struct {
unsigned int activeSpeedGrade;
unsigned int compatibleSpeedGrade;
unsigned int inicFwVersion;
} most;
struct {
unsigned int status;
unsigned int cfgMode;
unsigned int baudrate;
} flexray;
struct {
unsigned char macAddr[6];
unsigned char connector;
unsigned char phy;
unsigned char link;
unsigned char speed;
unsigned char clockMode;
unsigned char bypass;
} ethernet;
struct {
unsigned short channelDirection;
unsigned short res1;
union {
struct {
unsigned int bitrate;
unsigned int parity;
unsigned int minGap;
} tx;
struct {
unsigned int bitrate;
unsigned int minBitrate;
unsigned int maxBitrate;
unsigned int parity;
unsigned int minGap;
unsigned int autoBaudrate;
} rx;
unsigned char raw[24];
} dir;
} a429;
unsigned char raw[28];
} data;
} XLbusParams;
typedef struct s_xl_channel_config {
char name [XL_MAX_LENGTH + 1];
unsigned cha hwType;
unsigned char hwIndex;
unsigned char hwChannel;
unsigned short transceiverType;
unsigned int transceiverState;
unsigned char channelIndex;
XLuint64 channelMask;
unsigned int channelCapabilities;
unsigned int channelBusCapabilities;
unsigned char isOnBus;
unsigned int connectedBusType;
XLbusParams busParams;
unsigned int driverVersion;
unsigned int interfaceVersion;
unsigned int raw_data[10];
unsigned int serialNumber;
unsigned int articleNumber;
char transceiverName [XL_MAX_LENGTH + 1];
unsigned int specialCabFlags;
unsigned int dominantTimeout;
unsigned in reserved[8];
} XLchannelConfig;
typedef struct s_xl_driver_config {
unsigned int dllVersion;
unsigned int channelCount;
unsigned int reserved[10];
XLchannelConfig channel[XL_CONFIG_MAX_CHANNELS];
} XLdriverConfig;
XLstatus xlGetDriverConfig(XLdriverConfig *pDriverConfig)
转换Python可以调用的函数
# Load Windows DLL
DLL_NAME = "vxlapi64" if platform.architecture()[0] == "64bit" else "vxlapi"
_xlapi_dll = ctypes.windll.LoadLibrary(DLL_NAME)
xlGetDriverConfig = _xlapi_dll.xlGetDriverConfig
xlGetDriverConfig.argtypes = [ctypes.POINTER(xlclass.XLdriverConfig)]
xlGetDriverConfig.restype = xlclass.XLstatus
xlGetDriverConfig.errcheck = check_status_operation
xlOpenDriver = _xlapi_dll.xlOpenDriver
xlOpenDriver.argtypes = []
xlOpenDriver.restype = xlclass.XLstatus
xlOpenDriver.errcheck = check_status_initialization
xlCloseDriver = _xlapi_dll.xlCloseDriver
xlCloseDriver.argtypes = []
xlCloseDriver.restype = xlclass.XLstatus
xlCloseDriver.errcheck = check_status_operation
class s_xl_bus_params_data(ctypes.Union):
_fields_ = [
# ("can", s_xl_bus_params_data_can),
# ("canFD", s_xl_bus_params_data_canfd),
("most", ctypes.c_ubyte * 12),
("flexray", ctypes.c_ubyte * 12),
("ethernet", ctypes.c_ubyte * 12),
("a429", ctypes.c_ubyte * 28),
]
class XLlinStatPar(ctypes.Structure):
_fields_ = [
("LINMode",ctypes.c_uint),
("baudrate",ctypes.c_int),
("LINVersion",ctypes.c_uint),
("reserved",ctypes.c_uint)
]
class XLbusParams(ctypes.Structure):
_fields_ = [("busType", ctypes.c_uint), ("data", s_xl_bus_params_data)]
class XLchannelConfig(ctypes.Structure):
_pack_ = 1
_fields_ = [
("name", ctypes.c_char * 32),
("hwType", ctypes.c_ubyte),
("hwIndex", ctypes.c_ubyte),
("hwChannel", ctypes.c_ubyte),
("transceiverType", ctypes.c_ushort),
("transceiverState", ctypes.c_ushort),
("configError", ctypes.c_ushort),
("channelIndex", ctypes.c_ubyte),
("channelMask", XLuint64),
("channelCapabilities", ctypes.c_uint),
("channelBusCapabilities", ctypes.c_uint),
("isOnBus", ctypes.c_ubyte),
("connectedBusType", ctypes.c_uint),
("busParams", XLbusParams),
("_doNotUse", ctypes.c_uint),
("driverVersion", ctypes.c_uint),
("interfaceVersion", ctypes.c_uint),
("raw_data", ctypes.c_uint * 10),
("serialNumber", ctypes.c_uint),
("articleNumber", ctypes.c_uint),
("transceiverName", ctypes.c_char * 32),
("specialCabFlags", ctypes.c_uint),
("dominantTimeout", ctypes.c_uint),
("dominantRecessiveDelay", ctypes.c_ubyte),
("recessiveDominantDelay", ctypes.c_ubyte),
("connectionInfo", ctypes.c_ubyte),
("currentlyAvailableTimestamps", ctypes.c_ubyte),
("minimalSupplyVoltage", ctypes.c_ushort),
("maximalSupplyVoltage", ctypes.c_ushort),
("maximalBaudrate", ctypes.c_uint),
("fpgaCoreCapabilities", ctypes.c_ubyte),
("specialDeviceStatus", ctypes.c_ubyte),
("channelBusActiveCapabilities", ctypes.c_ushort),
("breakOffset", ctypes.c_ushort),
("delimiterOffset", ctypes.c_ushort),
("reserved", ctypes.c_uint * 3),
]
class XLdriverConfig(ctypes.Structure):
_fields_ = [
("dllVersion", ctypes.c_uint),
("channelCount", ctypes.c_uint),
("reserved", ctypes.c_uint * 10),
("channel", XLchannelConfig * 64),
]
调用函数会报错,发现此字段("busParams", XLbusParams)定义错误或者没有定义的情况都会报错,故推断是在执行的时候序列化了参数,导致找不到会报错,所以在做转换的时候一定要注意参数是否一致
driver_config = xlclass.XLdriverConfig()
xldriver.xlOpenDriver()
xldriver.xlGetDriverConfig(driver_config)
xldriver.xlCloseDriver()
数组转换
C/C++/C#: char reserved[X];
PYTHON结构体:("reserved", ctypes.c_char * X )
PYTHON函数参数:ctypes.POINTER(ctypes.c_char)
XLstatus xlLinSetDLC(XLportHandle portHandle,XLaccess accessMask,unsigned char DLC[60])
xlLinSetDLC = _xlapi_dll.xlLinSetDLC
xlLinSetDLC.argtypes = [
xlclass.XLportHandle,
xlclass.XLaccess,
ctypes.POINTER(ctypes.c_char),
]
xlLinSetDLC.restype = xlclass.XLstatus
xlLinSetDLC.errcheck = check_status_operation
dlc = (ctypes.c_char * 60)(*(8 for i in range(60)))
xlLinSetDLC(port_handle,channel_masks,dlc)