欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python周立功CAN接口卡接口库函数

程序员文章站 2022-04-01 18:43:34
...

周立功CAN相关资源下载
首先,把下载的库函数文件(由上面链接下载)都放在工作目录下。库函数文件总共有三个文件:ControlCAN.h、
ControlCAN.lib、ControlCAN.dll 和一个文件夹 kerneldlls。

from ctypes import *
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_byte),
                ('SendType', c_byte),
                ('RemoteFlag', c_byte),
                ('ExternFlag', c_byte),
                ('DataLen', c_byte),
                ('Data', c_byte*8),
                ('Reserved', c_byte*3)]

class _RX_CAN_OBJ(Structure):
    _fields_ = [('ID', c_uint),
                ('TimeStamp', c_uint),
                ('TimeFlag', c_byte),
                ('SendType', c_byte),
                ('RemoteFlag', c_byte),
                ('ExternFlag', c_byte),
                ('DataLen', c_byte),
                ('Data', c_byte*8),
                ('Reserved', c_byte*3)]

vic = _VCI_INIT_CONFIG()
vic.AccCode = 0x00000000
vic.AccMask = 0xffffffff
vic.Filter =1
vic.Timing0 = 0x01
vic.Timing1 = 0x1c
vic.Mode = 0

vco = _VCI_CAN_OBJ()
vco.ID = 0x18ff3010
vco.SendType = 0
vco.RemoteFlag = 0
vco.ExternFlag = 1
vco.DataLen = 8
vco.Data = (5, 2, 3, 4, 5, 6, 7, 8)

rxdata=_RX_CAN_OBJ()

canLib = windll.LoadLibrary('./ControlCAN.dll')
print("下面执行操作返回“1”表示操作成功!")
print('打开设备: %d' % (canLib.VCI_OpenDevice(3, 0, 0)))
print('设置波特率: %d' % (canLib.VCI_SetReference(3, 0, 0, 0, pointer(c_int(0x1C0008)))))
print('初始化: %d' % (canLib.VCI_InitCAN(3, 0, 0, pointer(vic))))
print('启动: %d' % (canLib.VCI_StartCAN(3, 0, 0)))
print('清空缓冲区: %d' % (canLib.VCI_ClearBuffer(3, 0, 0)))
print('发送: %d' % (canLib.VCI_Transmit(3, 0, 0, pointer(vco), 1)))

# print('接收: %d' % (canLib.VCI_Receive(3, 0, 0, pointer(rxdata),100,400)))
while canLib.VCI_GetReceiveNum(3,0,0)==0 :
    continue
print("接收缓存数量:",canLib.VCI_GetReceiveNum(3,0,0))
if(canLib.VCI_Receive(3, 0, 0, pointer(rxdata),100,400)):
    print('从缓存读取一帧数据:', bytearray(rxdata.Data).hex())
else:
    print("接收缓存区为空")
print("接收缓存数量:",canLib.VCI_GetReceiveNum(3,0,0))

运行结果如下:
Python周立功CAN接口卡接口库函数

相关标签: ❤CAN