4.USB-CAN Bus Interface Adapter Interface Function Library User Instruction
4.USB-CAN Bus Interface Adapter Interface Function Library User Instruction
User Instruction
Instruction Version:V2.03
Version Update Date:2017.06.30
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
Contents
Part One Overview .....................................................................................................................1
Part Two Compatible ZLG Function Library and Data Structure ..............................................2
2.1.3 VCI_CAN_OBJ............................................................................................................ 3
2.2.8 VCI_ResetCAN......................................................................................................... 16
3.3.4 VCI_UsbDeviceReset............................................................................................... 23
3.3.5 VCI_FindUsbDevice................................................................................................. 23
I
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
and then he can directly use the supplied USB-CAN Tool software for sending and receiving data
of the test.
If the user intends to write software program for his own products. Please carefully read the
following instructions and take reference from the sample code we provide:
①LabVIEW②C++Builder③VC④VB⑤Delphi.
Page 1
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
Type
Type Definition Description
value
DEV_USBCAN2 4 USBCAN-2A/USBCAN-2C/CANalyst-II
2.1.2 VCI_BOARD_INFO
USHORT hw_Version;
USHORT fw_Version;
USHORT dr_Version;
USHORT in_Version;
USHORT irq_Num;
BYTE can_Num;
CHAR str_Serial_Num[20];
CHAR str_hw_Type[40];
USHORT Reserved[4];
} VCI_BOARD_INFO, *PVCI_BOARD_INFO;
Member:
hw_Version
fw_Version
dr_Version
Page 2
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
in_Version
Interface library version number, hexadecimal notation. E.g. 0x0100 represents V1.00.
irq_Num
System reserved.
can_Num
str_Serial_Num
str_hw_Type
Hardware type, such as “USBCAN V1.00” (Note: Includes string terminator ’\0’).
Reserved
System reserved.
2.1.3 VCI_CAN_OBJ
UINT ID;
UINT TimeStamp;
BYTE TimeFlag;
BYTE SendType;
BYTE RemoteFlag;
BYTE ExternFlag;
BYTE DataLen;
BYTE Data[8];
BYTE Reserved[3];
}VCI_CAN_OBJ, *PVCI_CAN_OBJ;
Member:
Page 3
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
ID
Message identifier. Direct ID format, right-aligned, please refer to: Annex One:
ID Alignment Details.
TimeStamp
Receiving the stamp information of the time frame, start timing when the CAN controller is
TimeFlag
In terms of whether to use the time stamp, 1is the effective TimeStamp. TimeFlag and
SendType
RemoteFlag
ExternFlag
DataLen
Data
Packet data.
Reserved
System reserved.
2.1.4 VCI_INIT_CONFIG
DWORD AccCode;
DWORD AccMask;
DWORD Reserved;
Page 4
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
UCHAR Filter; //0,1 receives all frames. 2 standard frame filtering, 3 is an extended frame
filtering.
} VCI_INIT_CONFIG, *PVCI_INIT_CONFIG;
Member:
AccCode
AccMask
Reserved
Reserved.
Filter
Filtering method, allowing setting range 0-3, refer to section 2.2.3 of the filter mode table
for details.
Timing0
Timing1
Mode
Remarks:
About the filter settings please refer to: Annex II: CAN parameter setup instructions.
CAN Timing0 and Timing1 are used to set baud rate, these two parameters are only used at the
initialization stage.
Page 5
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
Note:
1. Users only need to follow SJA1000 (16MHz) to set the Baud rate parameter.
2. The adapter does not support temporarily Baud rate below 10K.
Page 6
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
2.2.1 VCI_OpenDevice
Parameters:
DevType
DevIndex
Device Index, for example, when there is only one USB-CAN adapter, the index number is 0,
when there are multiple USB-CAN adapters, the index numbers in an ascending order starting
from 0.
Reserved
Returns:
Return value = 1, which means that the operation is successful; = 0 indicates that the operation
E.g.:
#include "ControlCan.h"
DWORD dwRel;
if(dwRel != 1)
return FALSE;
Page 7
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
2.2.2 VCI_CloseDevice
Parameters:
DevType
DevIndex
Device Index, for example, when there is only one USB-CAN adapter, the index number is 0,
when there are multiple USB-CAN adapters, the index numbers in an ascending order starting
from 0.
Returns:
Return value = 1, which means that the operation is successful; = 0 indicates that the operation
E.g.:
#include "ControlCan.h"
DWORD dwRel;
if(dwRel != 1)
return FALSE;
2.2.3 VCI_InitCan
Page 8
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
PVCI_INIT_CONFIG pInitConfig);
Parameters:
DevType
DevIndex
Device Index, for example, when there is only one USB-CAN adapter, the index number is 0,
when there are multiple USB-CAN adapters, the index numbers in an ascending order starting
from 0.
CANIndex
CAN channel index, such as when there is only one CAN channel, the index number is 0, if
pInitConfig
value.
alignment details.
Page 9
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
pInitConfig->Reserved reserved
Operating mode:
0-normal operation
1-Listen-only mode
pInitConfig->Mode
2-spontaneous admission and sending test mode
library)
Page 10
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
directly! 。
Returns:
Return value = 1, which means that the operation is successful; = 0 indicates that the
E.g.:
#include "ControlCan.h"
DWORD dwRel;
VCI_INIT_CONFIG vic;
if(dwRel != 1)
return FALSE;
vic .AccCode=0x80000008;
vic .AccMask=0xFFFFFFFF;
vic.Filter=1;
vic .Timing0=0x00;
vic .Timing1=0x14;
vic .Mode=0;
if(dwRel !=1)
Page 11
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
VCI_CloseDevice(nDeviceType, nDeviceInd);
return FALSE;
2.2.4 VCI_ReadBoardInfo
This function is used to read the adapter hardware information. Generally speaking, it can
be ignored.
DevIndex,PVCI_BOARD_INFO pInfo);
Parameters:
DevType
DevIndex
Device Index, for example, when there is only one USB-CAN adapter, the index number is 0,
when there are multiple USB-CAN adapters, the index numbers in an ascending order starting
from 0.
pInfo
Returns:
Return value = 1, which means that the operation is successful; = 0 indicates that the
E.g.:
#include "ControlCan.h"
int nCANInd = 0;
VCI_BOARD_INFO vbi;
DWORD dwRel;
Page 12
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
if(dwRel != 1)
return FALSE;
2.2.5 VCI_GetReceiveNum
This function is used to specify the received but has not been read frames in the designated
receiving buffer.
Parameters:
DevType
DevIndex
Device Index, for example, when there is only one USB-CAN adapter, the index number is 0,
when there are multiple USB-CAN adapters, the index numbers in an ascending order starting
from 0.
CANIndex
Returns:
E.g.:
#include "ControlCan.h"
int ret=VCI_GetReceiveNum(2,0,0);
2.2.6 VCI_ClearBuffer
This function is used to clear the receive and send buffer of the designated channel specified by
USB-CAN adapter.
Page 13
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
Parameters:
DevType
DevIndex
Device Index, for example, when there is only one USB-CAN adapter, the index number is 0,
when there are multiple USB-CAN adapters, the index numbers in an ascending order starting
from 0.
CANIndex
Returns:
Return value = 1, which means that the operation is successful; = 0 indicates that the operation
E.g.:
#include "ControlCan.h"
DWORD dwRel;
2.2.7 VCI_StartCAN
This function is used to start the CAN controller and the internal interrupt reception function of
the adapter.
Parameters:
DevType
DevIndex
Page 14
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
Device Index, for example, when there is only one USB-CAN adapter, the index number is 0,
when there are multiple USB-CAN adapters, the index numbers in an ascending order starting
from 0.
CANIndex
Returns:
Return value = 1, which means that the operation is successful; = 0 indicates that the
E.g.:
#include "ControlCan.h"
DWORD dwRel;
VCI_INIT_CONFIG vic;
if(VCI_OpenDevice(nDeviceType, nDeviceInd, 0) != 1)
return FALSE;
VCI_CloseDevice(nDeviceType, nDeviceInd);
return FALSE;
VCI_CloseDevice(nDeviceType, nDeviceInd);
Page 15
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
return FALSE;
2.2.8 VCI_ResetCAN
Parameters:
DevType
DevIndex
Device Index, for example, when there is only one USB-CAN adapter, the index number is 0,
when there are multiple USB-CAN adapters, the index numbers in an ascending order starting
from 0.
CANIndex
Returns:
Return value = 1, which means that the operation is successful; = 0 indicates that the
E.g.:
#include "ControlCan.h"
int nCANInd = 0;
DWORD dwRel;
if(dwRel != 1)
Page 16
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
return FALSE;
2.2.9 VCI_Transmit
Parameters:
DevType
DevIndex
Device Index, for example, when there is only one USB-CAN adapter, the index number is 0,
when there are multiple USB-CAN adapters, the index numbers in an ascending order starting
from 0.
CANIndex
pSend
The first address of the data frame arrays that have to be sent.
Length
The number of the data frames that have to be sent, the maximum number is 1000, the
Returns:
Return the actual number of frames already sent, the return value = -1 indicates a device
error.
E.g.:
#include "ControlCan.h"
Page 17
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
int nCANInd = 0;
DWORD dwRel;
VCI_CAN_OBJ vco[48];
ZeroMemory(&vco, sizeof(VCI_CAN_OBJ)*48);
for(int i=0;i<48;i++)
vco[i].ID = i;
vco[i].RemoteFlag = 0;
vco[i].ExternFlag = 0;
vco[i].DataLen = 8;
for(int j = 0;j<8;j++)
vco.Data[j] = j;
2.2.10 VCI_Receive
Parameters:
DevType
DevIndex
Device Index, for example, when there is only one USB-CAN adapter, the index number is 0,
when there are multiple USB-CAN adapters, the index numbers in an ascending order starting
from 0.
CANIndex
pReceive
Page 18
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
Len
The array length of the data frame must be more than 48 to return normal message.
Otherwise, the return length will be zero whether the message is received or not.the adapter set
a 2000-frame buffer for every channel. Based on his own system and working environment, the
WaitTime
Wait timeout, in milliseconds, generally take 10 ~ 50ms, -1 means unlimited, infinite wait.
Returns:
Return the number of frames that actually have been read, -1 indicates device errors.
E.g.:
#include "ControlCan.h"
int nCANInd = 0;
long lRel;
VCI_CAN_OBJ vco[100];
if(lRel > 0)
… /*data processing */
Page 19
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
library contained in USB-CAN adapter interface library ControlCAN.dll. Please do not call these
functions if use a compatible ZLG model for secondary development so as not to affect
compatibility.
3.1.1 VCI_BOARD_INFO1
information on your computer while inserting the first four series of USB-CAN interface card
USHORT hw_Version;
USHORT fw_Version;
USHORT dr_Version;
USHORT in_Version;
USHORT irq_Num;
BYTE can_Num;
BYTE Reserved;
CHAR str_Serial_Num[8];
CHAR str_hw_Type[16];
} VCI_BOARD_INFO1, *PVCI_BOARD_INFO1;
Member:
hw_Version
fw_Version
Page 20
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
dr_Version
in_Version
Interface library version number, hexadecimal notation. E.g. 0x0100 represents V1.00.
irq_Num
can_Num
Reserved
System reserved.
str_Serial_Num
str_hw_Type
str_Usb_Serial
USB-CAN serial number, return four groups of four characters long ASCII string, each string
represents a device serial number, the same computer can support four USB-CAN device.
3.3.1 VCI_ConnectDevice
Check whether the USB-CAN device and your computer can communicate properly.
USB-CAN operation may be subject to outside interference or abnormal operation,which
will cause USB-CAN device dropped from the operating system. This function is used to detect
communication connection status such as relative abnormal situations. If there is any abnormality,
relative tips can be brought up and relative problems will be dealt with accordingly.
DWORD __stdcall VCI_ConnectDevice(DWORD DevType,DWORD DevIndex);
Parameters:
DevType
DevIndex
Page 21
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
Device Index, for example, when there is only one USB-CAN adapter, the index number is 0,
when there are multiple USB-CAN adapters, the index numbers in an ascending order starting
from 0.
Returns:
E.g.:
#include "ControlCan.h"
int ret=VCI_ConnectDevice(2,0);
if(ret==1)
SetWindowText("normal communication");
else if(ret==0)
SetWindowText("dropped");
StopFlag=1;
for(int i=0;i<10;i++)
Sleep(1000);
VCI_BOARD_INFO vbi;
if(temp==1)
else
Page 22
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
3.3.2 VCI_UsbDeviceReset
Reset USB-CAN adapter, need to re-open the device after reset by using VCI_OpenDevice.
Parameters:
DevType
DevIndex
Device Index, for example, when there is only one USB-CAN adapter, the index number is 0,
when there are multiple USB-CAN adapters, the index numbers in an ascending order starting
from 0.
Reserved
Reserved.
Returns:
Return value = 1, which means that the operation is successful; = 0 indicates that the
E.g.:
#include "ControlCan.h"
int nCANInd = 0;
DWORD dwRel;
3.3.3 VCI_FindUsbDevice
When the same PC using multiple USB-CAN, user can use this function to find the current
device.
Parameters:
Page 23
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
pInfo
pInfo is used to store the parameters of the first data buffer address pointer.
Returns:
Return the number of the USB-CAN adapter plugged into the computer.
E.g.:
#include "ControlCan.h"
CString ProductSn[5];
VCI_BOARD_INFO1 vbi;
int num=VCI_FindUsbDevice(&vbi);
CString strtemp,str;
for(int i=0;i<num;i++)
str="";
for(int j=0;j<4;j++)
strtemp.Format("%c",pData->str_Usb_Serial[i][j]);
str+=strtemp;
ProductSn[i]="USBCAN-"+str;
Page 24
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com
4. USB-CAN Bus Interface Adapter Interface Function Library User Instruction
second development, these functions are not necessarily to be invoked. Even these functions are
VCI_FindUsbDevice
VCI_OpenDevice
VCI_InitCAN
VCI_UsbDeviceReset
VCI_ConnectDevice
VCI_GetReceiveNum VCI_ClearBuffer
VCI_ReadBoardInfo
VCI_SetReference
VCI_GetReference2 VCI_SetReference2
VCI_StartCAN
VCI_ResetCAN
VCI_Transmit VCI_Recive
VCI_CloseDevice
Page 25
Zhuhai Chuangxin Technology Co., Ltd Email: zhcxgd@163.com