您好,登錄后才能下訂單哦!
由于工作上的需要,經常要與USBCAN打交道,但廠家一般不會提供PYTHON的例子,于是自己摸索地寫一個例子出來,以便在工作上隨時可以使用PYTHON來測試CAN的功能。這里的例子是使用珠海創芯科技有限公司的USBCAN接口卡,他們提供一個ControlCAN.dll,也提供了一個.h文件,如下:
#ifndef CONTROLCAN_H #define CONTROLCAN_H ////文件版本:v2.00 20150920 //#include <cvidef.h> //使用CVI平臺開發,請使用該語句。 //接口卡類型定義 #define VCI_USBCAN1 3 #define VCI_USBCAN2 4 #define VCI_USBCAN2A 4 #define VCI_USBCAN_E_U 20 #define VCI_USBCAN_2E_U 21 //函數調用返回狀態值 #define STATUS_OK 1 #define STATUS_ERR 0 /*------------------------------------------------兼容ZLG的函數及數據類型------------------------------------------------*/ //1.ZLGCAN系列接口卡信息的數據類型。 typedef struct _VCI_BOARD_INFO{ unsigned short hw_Version; unsigned short fw_Version; unsigned short dr_Version; unsigned short in_Version; unsigned short irq_Num; unsigned char can_Num; char str_Serial_Num[20]; char str_hw_Type[40]; unsigned short Reserved[4]; } VCI_BOARD_INFO,*PVCI_BOARD_INFO; //2.定義CAN信息幀的數據類型。 typedef struct _VCI_CAN_OBJ{ unsigned int ID; unsigned int TimeStamp; unsigned char TimeFlag; unsigned char SendType; unsigned char RemoteFlag;//是否是遠程幀 unsigned char ExternFlag;//是否是擴展幀 unsigned char DataLen; unsigned char Data[8]; unsigned char Reserved[3]; }VCI_CAN_OBJ,*PVCI_CAN_OBJ; //3.定義初始化CAN的數據類型 typedef struct _VCI_INIT_CONFIG{ unsigned long AccCode; unsigned long AccMask; unsigned long Reserved; unsigned char Filter; unsigned char Timing0; unsigned char Timing1; unsigned char Mode; }VCI_INIT_CONFIG,*PVCI_INIT_CONFIG; ///////// new add struct for filter ///////// typedef struct _VCI_FILTER_RECORD{ unsigned long ExtFrame; //是否為擴展幀 unsigned long Start; unsigned long End; }VCI_FILTER_RECORD,*PVCI_FILTER_RECORD; #define EXTERNC extern "C" EXTERNC unsigned long __stdcall VCI_OpenDevice(unsigned long DeviceType,unsigned long DeviceInd,unsigned long Reserved); EXTERNC unsigned long __stdcall VCI_CloseDevice(unsigned long DeviceType,unsigned long DeviceInd); EXTERNC unsigned long __stdcall VCI_InitCAN(unsigned long DeviceType, unsigned long DeviceInd, unsigned long CANInd, PVCI_INIT_CONFIG pInitConfig); EXTERNC unsigned long __stdcall VCI_ReadBoardInfo(unsigned long DeviceType,unsigned long DeviceInd,PVCI_BOARD_INFO pInfo); EXTERNC unsigned long __stdcall VCI_SetReference(unsigned long DeviceType,unsigned long DeviceInd,unsigned long CANInd,unsigned long RefType,void* pData); EXTERNC unsigned long __stdcall VCI_GetReceiveNum(unsigned long DeviceType,unsigned long DeviceInd,unsigned long CANInd); EXTERNC unsigned long __stdcall VCI_ClearBuffer(unsigned long DeviceType,unsigned long DeviceInd,unsigned long CANInd); EXTERNC unsigned long __stdcall VCI_StartCAN(unsigned long DeviceType,unsigned long DeviceInd,unsigned long CANInd); EXTERNC unsigned long __stdcall VCI_ResetCAN(unsigned long DeviceType,unsigned long DeviceInd,unsigned long CANInd); EXTERNC unsigned long __stdcall VCI_Transmit(unsigned long DeviceType,unsigned long DeviceInd,unsigned long CANInd,PVCI_CAN_OBJ pSend,unsigned long Len); EXTERNC unsigned long __stdcall VCI_Receive(unsigned long DeviceType,unsigned long DeviceInd,unsigned long CANInd,PVCI_CAN_OBJ pReceive,unsigned long Len,int WaitTime); /*------------------------------------------------其他補充函數及數據結構描述------------------------------------------------*/ //USB-CAN總線適配器板卡信息的數據類型1,該類型為VCI_FindUsbDevice函數的返回參數。 typedef struct _VCI_BOARD_INFO1{ unsigned long hw_Version; unsigned long fw_Version; unsigned long dr_Version; unsigned long in_Version; unsigned long irq_Num; unsigned char can_Num; unsigned char Reserved; char str_Serial_Num[8]; char str_hw_Type[16]; char str_Usb_Serial[4][4]; } VCI_BOARD_INFO1,*PVCI_BOARD_INFO1; //USB-CAN總線適配器板卡信息的數據類型2,該類型為VCI_FindUsbDevice函數的返回參數。為擴展更多的設備 typedef struct _VCI_BOARD_INFO2{ unsigned long hw_Version; unsigned long fw_Version; unsigned long dr_Version; unsigned long in_Version; unsigned long irq_Num; unsigned char can_Num; unsigned char Reserved; char str_Serial_Num[8]; char str_hw_Type[16]; char str_Usb_Serial[10][4]; } VCI_BOARD_INFO2,*PVCI_BOARD_INFO2; #define EXTERNC extern "C" EXTERNC unsigned long __stdcall VCI_GetReference2(unsigned long DevType,unsigned long DevIndex,unsigned long CANIndex,unsigned long Reserved,unsigned char *pData); EXTERNC unsigned long __stdcall VCI_SetReference2(unsigned long DevType,unsigned long DevIndex,unsigned long CANIndex,unsigned long RefType,unsigned char *pData); EXTERNC unsigned long __stdcall VCI_ConnectDevice(unsigned long DevType,unsigned long DevIndex); EXTERNC unsigned long __stdcall VCI_UsbDeviceReset(unsigned long DevType,unsigned long DevIndex,unsigned long Reserved); EXTERNC unsigned long __stdcall VCI_FindUsbDevice(PVCI_BOARD_INFO1 pInfo); EXTERNC unsigned long __stdcall VCI_FindUsbDevice2(PVCI_BOARD_INFO2 pInfo); #endif
要調用這些函數才可以完成工作,下面就來創建一個例子,從CAN的通道0向通道1來發送一幀CAN數據,例子代碼如下:
#python3.6 32位 #https://blog.csdn.net/caimouse/article/details/51749579 #開發人員:蔡軍生(QQ:9073204) 深圳 2018-3-25 # from ctypes import * VCI_USBCAN2A = 4 STATUS_OK = 1 class VCI_INIT_CONFIG(Structure): _fields_ = [("AccCode", c_ulong), ("AccMask", c_ulong), ("Reserved", c_ulong), ("Filter", c_ubyte), ("Timing0", c_ubyte), ("Timing1", c_ubyte), ("Mode", c_ubyte) ] class VCI_CAN_OBJ(Structure): _fields_ = [("ID", c_uint), ("TimeStamp", c_uint), ("TimeFlag", c_ubyte), ("SendType", c_ubyte), ("RemoteFlag", c_ubyte), ("ExternFlag", c_ubyte), ("DataLen", c_ubyte), ("Data", c_ubyte*8), ("Reserved", c_ubyte*3) ] CanDLLName = 'ControlCAN.dll' #DLL是32位的,必須使用32位的PYTHON canDLL = windll.LoadLibrary(CanDLLName) print(CanDLLName) ret = canDLL.VCI_OpenDevice(VCI_USBCAN2A, 0, 0) print(ret) if ret != STATUS_OK: print('調用 VCI_OpenDevice出錯\r\n') #初始0通道 vci_initconfig = VCI_INIT_CONFIG(0x80000008, 0xFFFFFFFF, 0, 2, 0x00, 0x1C, 0) ret = canDLL.VCI_InitCAN(VCI_USBCAN2A, 0, 0, byref(vci_initconfig)) if ret != STATUS_OK: print('調用 VCI_InitCAN出錯\r\n') ret = canDLL.VCI_StartCAN(VCI_USBCAN2A, 0, 0) if ret != STATUS_OK: print('調用 VCI_StartCAN出錯\r\n') #初始1通道 ret = canDLL.VCI_InitCAN(VCI_USBCAN2A, 0, 1, byref(vci_initconfig)) if ret != STATUS_OK: print('調用 VCI_InitCAN 1 出錯\r\n') ret = canDLL.VCI_StartCAN(VCI_USBCAN2A, 0, 1) if ret != STATUS_OK: print('調用 VCI_StartCAN 1 出錯\r\n') #通道0發送數據 ubyte_array = c_ubyte*8 a = ubyte_array(1,2,3,4, 5, 6, 7, 64) ubyte_3array = c_ubyte*3 b = ubyte_3array(0, 0 , 0) vci_can_obj = VCI_CAN_OBJ(0x0, 0, 0, 1, 0, 0, 8, a, b) ret = canDLL.VCI_Transmit(VCI_USBCAN2A, 0, 0, byref(vci_can_obj), 1) if ret != STATUS_OK: print('調用 VCI_Transmit 出錯\r\n') #通道1接收數據 a = ubyte_array(0, 0, 0, 0, 0, 0, 0, 0) vci_can_obj = VCI_CAN_OBJ(0x0, 0, 0, 1, 0, 0, 8, a, b) ret = canDLL.VCI_Receive(VCI_USBCAN2A, 0, 1, byref(vci_can_obj), 1, 0) print(ret) while ret <= 0: print('調用 VCI_Receive 出錯\r\n') ret = canDLL.VCI_Receive(VCI_USBCAN2A, 0, 1, byref(vci_can_obj), 1, 0) if ret > 0: print(vci_can_obj.DataLen) print(list(vci_can_obj.Data)) #關閉 canDLL.VCI_CloseDevice(VCI_USBCAN2A, 0)
運行結果輸出如下:
ControlCAN.dll
1
1
8
[1, 2, 3, 4, 5, 6, 7, 64]
可以看到從通道1里收通道0發過來的數據,達到這個程序的目的。
以上這篇使用python來調用CAN通訊的DLL實現方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。