Using Python to invoke the Dll__python of can communication

Source: Internet
Author: User
Tags reserved

Because of the need for work, often to deal with Usbcan, but manufacturers generally do not provide examples of python, so they grope to write an example, so that at work at any time can use Python to test can function. The example here is using the Usbcan interface card of Zhuhai Core Technology Co., Ltd., they provide a ControlCAN.dll, also provides a. h file, as follows:

#ifndef controlcan_h #define CONTROLCAN_H////file version: v2.00 20150920//#include <cvidef.h>//Use CVI platform development, use this statement. interface card type definition #define VCI_USBCAN1 3 #define VCI_USBCAN2 4 #define VCI_USBCAN2A 4 #define VCI_USBCAN_E_U #define Vci_ Usbcan_2e_u 21//Function call return status value #define STATUS_OK 1 #define STATUS_ERR 0/*--------------------------------------
----------compatible ZLG functions and data types------------------------------------------------///1.zlgcan Series interface card information data type.
		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. Defines the data type of the can information frame.
	typedef struct _vci_can_obj{unsigned int ID;
	unsigned int TimeStamp;
	unsigned char timeflag;
	unsigned char sendtype; Whether the unsigned char remoteflag;//is a remote frame unsigned char externflag;//is an extension frame unsignedChar Datalen;
	unsigned char data[8];
unsigned char reserved[3];

}vci_can_obj,*pvci_can_obj;
	3. Defines the data type that can be initialized the 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;//is an extended frame unsign
	Ed 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 devicei
nd,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);
EXTERNC unsigned long __stdcall vci_startcan (unsigned long devicetype,unsigned long deviceind,unsigned);

EXTERNC unsigned long __stdcall vci_resetcan (unsigned long devicetype,unsigned long deviceind,unsigned); 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); /*------------------------------------------------Other complementary functions and data structure description------------------------------------------------
*///usb-can Bus Adapter Board information data Type 1, which is the return parameter of the Vci_findusbdevice function.
	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 Bus Adapter Board information data Type 2, which is the return parameter of the Vci_findusbdevice function.
	To extend more devices 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);
EXTERNC unsigned long __stdcall vci_findusbdevice (Pvci_board_info1 pinfo);



EXTERNC unsigned long __stdcall vci_findusbdevice2 (Pvci_board_info2 pinfo);

 #endif

To call these functions to complete the work, the following is to create an example, from the can channel 0 to the channel to send a frame can data, the example code is as follows:

#python3.6 32-#https://blog.csdn.net/caimouse/article/details/51749579 #开发人员: Cai Junsheng (qq:9073204) Shenzhen 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), ("T Imeflag ", C_ubyte), (" Sendtype ", C_ubyte), (" Remoteflag ", C_ubyte), (" Exte  Rnflag ", C_ubyte), (" Datalen ", C_ubyte), (" Data ", C_ubyte*8), (" Reserved ", C_UBYTE*3)] Candllname = ' ControlCAN.dll ' #DLL是32位的, you must use the 32-bit Python Candll = Windll. LoadLibrary (candllname) print (candllname) ret = cAndll.vci_opendevice (vci_usbcan2a, 0, 0) print (ret) if RET!= status_ok:print (' Invoke Vci_opendevice error \ r \ n ') #初始0通道 vci_ Initconfig = Vci_init_config (0x80000008, 0xFFFFFFFF, 0, 2, 0x00, 0x1c, 0) ret = CANDLL.VC I_initcan (vci_usbcan2a, 0, 0, ByRef (vci_initconfig)) if RET!= status_ok:print (' Invoke Vci_initcan error \ r \ n ') ret = Candll. Vci_startcan (vci_usbcan2a, 0, 0) if ret!= status_ok:print (' Invoke Vci_startcan error \ r \ n ') #初始1通道 ret = Candll.vci_initcan ( VCI_USBCAN2A, 0, 1, ByRef (vci_initconfig)) if RET!= status_ok:print (' invoke Vci_initcan 1 error \ r \ n ') ret = Candll.vci_sta  Rtcan (vci_usbcan2a, 0, 1) if RET!= status_ok:print (' invoke Vci_startcan 1 error \ r \ n ') #通道0发送数据 Ubyte_array = c_ubyte*8 A = Ubyte_array (1,2,3,4, 5, 6, 7,) 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 ( ' Call Vci_transmit error \ 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.V
    Ci_receive (vci_usbcan2a, 0, 1, ByRef (vci_can_obj), 1, 0) print (ret) while RET <= 0:print (' Invoke vci_receive error \ 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)

The output of the run results is as follows:

ControlCAN.dll
1
1
8

[1, 2, 3, 4, 5, 6, 7, 64]

You can see the data from Channel 1 in Channel 0, to achieve the purpose of this program.

bit-Currency source starter Tutorial
https://edu.csdn.net/course/detail/6998
Simple Matplotlib
https://edu.csdn.net/course/detail/6859
Simple NumPy
http://edu.csdn.net/course/detail/6149

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.