python can通信_使用python来调用CAN通讯的DLL实现方法

本文介绍了如何使用Python3.6通过ControlCAN.dll进行CAN通讯,详细展示了调用DLL函数进行CAN接口卡初始化、数据发送与接收的步骤,以珠海创芯科技的USBCAN接口卡为例。
摘要由CSDN通过智能技术生成

由于工作上的需要,经常要与USBCAN打交道,但厂家一般不会提供PYTHON的例子,于是自己摸索地写一个例子出来,以便在工作上随时可以使用PYTHON来测试CAN的功能。这里的例子是使用珠海创芯科技有限公司的USBCAN接口卡,他们提供一个ControlCAN.dll,也提供了一个.h文件,如下:

#ifndef CONTROLCAN_H

#define CONTROLCAN_H

文件版本:v2.00 20150920

//#include //使用CVI平台开发,请使用该语句。

//接口卡类型定义

#define VCI_USBCAN13

#define VCI_USBCAN24

#define VCI_USBCAN2A4

#define VCI_USBCAN_E_U 20

#define VCI_USBCAN_2E_U 21

//函数调用返回状态值

#defineSTATUS_OK1

#define STATUS_ERR0

/*------------------------------------------------兼容ZLG的函数及数据类型------------------------------------------------*/

//1.ZLGCAN系列接口卡信息的数据类型。

typedef struct _VCI_BOARD_INFO{

unsigned shorthw_Version;

unsigned shortfw_Version;

unsigned shortdr_Version;

unsigned shortin_Version;

unsigned shortirq_Num;

unsigned charcan_Num;

charstr_Serial_Num[20];

charstr_hw_Type[40];

unsigned shortReserved[4];

} VCI_BOARD_INFO,*PVCI_BOARD_INFO;

//2.定义CAN信息帧的数据类型。

typedef struct _VCI_CAN_OBJ{

unsigned intID;

unsigned intTimeStamp;

unsigned charTimeFlag;

unsigned charSendType;

unsigned charRemoteFlag;//是否是远程帧

unsigned charExternFlag;//是否是扩展帧

unsigned charDataLen;

unsigned charData[8];

unsigned charReserved[3];

}VCI_CAN_OBJ,*PVCI_CAN_OBJ;

//3.定义初始化CAN的数据类型

typedef struct _VCI_INIT_CONFIG{

unsigned longAccCode;

unsigned longAccMask;

unsigned longReserved;

unsigned charFilter;

unsigned charTiming0;

unsigned charTiming1;

unsigned charMode;

}VCI_INIT_CONFIG,*PVCI_INIT_CONFIG;

/ new add struct for filter /

typedef struct _VCI_FILTER_RECORD{

unsigned longExtFrame;//是否为扩展帧

unsigned longStart;

unsigned longEnd;

}VCI_FILTER_RECORD,*PVCI_FILTER_RECORD;

#define EXTERNCextern "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 longhw_Version;

unsigned longfw_Version;

unsigned longdr_Version;

unsigned longin_Version;

unsigned longirq_Num;

unsigned charcan_Num;

unsigned charReserved;

charstr_Serial_Num[8];

charstr_hw_Type[16];

charstr_Usb_Serial[4][4];

} VCI_BOARD_INFO1,*PVCI_BOARD_INFO1;

//USB-CAN总线适配器板卡信息的数据类型2,该类型为VCI_FindUsbDevice函数的返回参数。为扩展更多的设备

typedef struct _VCI_BOARD_INFO2{

unsigned longhw_Version;

unsigned longfw_Version;

unsigned longdr_Version;

unsigned longin_Version;

unsigned longirq_Num;

unsigned charcan_Num;

unsigned charReserved;

charstr_Serial_Num[8];

charstr_hw_Type[16];

charstr_Usb_Serial[10][4];

} VCI_BOARD_INFO2,*PVCI_BOARD_INFO2;

#define EXTERNCextern "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实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

时间: 2019-07-01

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值