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


Posted in Python onJuly 03, 2019

由于工作上的需要,经常要与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实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python统计文本文件内单词数量的方法
May 30 Python
用Python将IP地址在整型和字符串之间轻松转换
Mar 22 Python
详解Python中的Numpy、SciPy、MatPlotLib安装与配置
Nov 17 Python
Python快速查找list中相同部分的方法
Jun 27 Python
解决pycharm py文件运行后停止按钮变成了灰色的问题
Nov 29 Python
tensorflow-gpu安装的常见问题及解决方案
Jan 20 Python
python GUI库图形界面开发之PyQt5信号与槽基础使用方法与实例
Mar 06 Python
Window版下在Jupyter中编写TensorFlow的环境搭建
Apr 10 Python
利用PyTorch实现VGG16教程
Jun 24 Python
python 图像插值 最近邻、双线性、双三次实例
Jul 05 Python
Python描述数据结构学习之哈夫曼树篇
Sep 07 Python
详解pandas中利用DataFrame对象的.loc[]、.iloc[]方法抽取数据
Dec 13 Python
opencv python 图像轮廓/检测轮廓/绘制轮廓的方法
Jul 03 #Python
在cmd中查看python的安装路径方法
Jul 03 #Python
python动态进度条的实现代码
Jul 03 #Python
OpenCV 轮廓检测的实现方法
Jul 03 #Python
python实现文本进度条 程序进度条 加载进度条 单行刷新功能
Jul 03 #Python
Python Pandas数据结构简单介绍
Jul 03 #Python
如何通过雪花算法用Python实现一个简单的发号器
Jul 03 #Python
You might like
php代码书写习惯优化小结
2013/06/20 PHP
php+mysql查询优化简单实例
2015/01/13 PHP
PHP中$_SERVER使用说明
2015/07/05 PHP
四个PHP非常实用的功能
2015/09/29 PHP
PHP中Notice错误常见解决方法
2017/04/28 PHP
php实现JWT验证的实例教程
2020/11/26 PHP
获取div编辑框,textarea,input text的光标位置 兼容IE,FF和Chrome的方法介绍
2012/11/08 Javascript
JavaScript全排列的六种算法 具体实现
2013/06/29 Javascript
js冒泡法和数组转换成字符串示例代码
2013/08/14 Javascript
javascript验证只能输入数字和一个小数点示例
2013/10/21 Javascript
如何让你的Lightbox支持滚轮缩放及Base64图片
2014/12/04 Javascript
JS JQUERY实现滚动条自动滚到底的方法
2015/01/09 Javascript
纯javascript实现简单下拉刷新功能
2015/03/13 Javascript
JQuery异步提交表单与文件上传功能示例
2017/01/12 Javascript
利用ES6语法重构React组件详解
2017/03/02 Javascript
Angular.JS中指令ng-if、ng-show/ng-hide和ng-switch的使用教程
2017/05/07 Javascript
JavaScript实现提交模式窗口后刷新父窗口数据的方法
2017/06/16 Javascript
vue-ajax小封装实例
2017/09/18 Javascript
浅谈JsonObject中的key-value数据解析排序问题
2017/12/06 Javascript
vue-lazyload图片延迟加载插件的实例讲解
2018/02/09 Javascript
element-ui 中的table的列隐藏问题解决
2018/08/24 Javascript
解决vue2中使用elementUi打包报错的问题
2020/09/22 Javascript
[02:38]DOTA2英雄基础教程 噬魂鬼
2014/01/03 DOTA
[47:03]Ti4第二日主赛事败者组 LGD vs iG 2
2014/07/21 DOTA
Python while 循环使用的简单实例
2016/06/08 Python
利用Python代码实现数据可视化的5种方法详解
2018/03/25 Python
利用Python+阿里云实现DDNS动态域名解析的方法
2019/04/01 Python
Python基础学习之类与实例基本用法与注意事项详解
2019/06/17 Python
Django Rest framework认证组件详细用法
2019/07/25 Python
Python列表删除元素del、pop()和remove()的区别小结
2019/09/11 Python
浅谈django channels 路由误导
2020/05/28 Python
django使用channels实现通信的示例
2020/10/19 Python
GetYourGuide台湾:预订旅游活动、景点和旅游项目
2019/06/10 全球购物
美国体育用品商店:Academy Sports + Outdoors
2020/01/04 全球购物
初中国旗下的演讲稿
2014/08/28 职场文书
狮子林导游词
2015/02/03 职场文书