生活随笔
收集整理的這篇文章主要介紹了
Python周立功CAN接口卡接口库函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
周立功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
= 0vco
= _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)))
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接口卡接口库函数的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。