广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
i
在 LabVIEW下使用 ZLGCAN
接口函数库
广州周立功单片机发展有限公司
2005 年 3 月 18 日
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
ii
目 录
第 1章 概述 ..............................................................................................................1
第 2章 使用 VCI函数 .............................................................................................2
2.1 数据结构.......................................................................................................................2
2.1.1 VCI_BOARD_INFO结构................................................................................2
2.1.2 VCI_CAN_OBJ结构 .......................................................................................3
2.1.3 VCI_CAN_STATUS结构 ...............................................................................3
2.1.4 VCI_ERR_INFO结构 ......................................................................................4
2.1.5 VCI_INIT_CONFIG结构 ................................................................................4
2.2 调用 VCI库函数..........................................................................................................5
2.3 应用示例.......................................................................................................................8
第 3章 VCI函数调用参考 ....................................................................................11
3.1 VCI_OpenDevice........................................................................................................11
3.2 VCI_CloseDevice .......................................................................................................11
3.3 VCI_InitCan................................................................................................................11
3.4 VCI_ReadBoardInfo ...................................................................................................12
3.5 VCI_ReadErrInfo........................................................................................................12
3.6 VCI_ReadCanStatus ...................................................................................................13
3.7 VCI_GetReference......................................................................................................13
3.8 VCI_SetReference ......................................................................................................14
3.9 VCI_GetReceiveNum .................................................................................................14
3.10 VCI_ClearBuffer.........................................................................................................15
3.11 VCI_StartCAN............................................................................................................15
3.12 VCI_ResetCAN ..........................................................................................................15
3.13 VCI_Transmit .............................................................................................................16
3.14 VCI_Receive...............................................................................................................16
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
1
第1章 概述
Virtual CAN Interface (VCI) 函数库是专门为 ZLGCAN设备在 PC上使用而提供的应用
程序接口。库里的函数从 ControlCAN.dll中导出,在 LabVIEW7.0中可以直接使用这些库函
数而无需额外的操作。VCI函数的使用流程如图 1.1所示。
VCI_OpenDevice
VCI_InitCan
VCI_ReadBoardInf
VCI_ReadErrInfo
VCI_GetReceiveNu
m
VCI_GetReference
VCI_ReadCanStatus
VCI_ClearBuffer
VCI_SetReference
VCI_StartCAN
VCI_Transmit VCI_ResetCAN
VCI_Receive
VCI_CloseDevice
图 1.1 VCI函数使用流程
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
2
第2章 使用 VCI函数
2.1 数据结构
VCI 函数库中定义了一些数据结构用于数据交换,在使用 VCI 函数前应该先创建这些
数据结构。在 LabVIEW中创建这些结构时应该使用簇——Cluster。一个簇就是一个由若干
不同的数据类型的成员组成的集合体,类似于 C 语言中的结构。其成员可以是任意的数据
类型,但必须都是控件或都是显示件。成员的逻辑顺序是由它们被放入簇的先后顺序决定的。
2.1.1 VCI_BOARD_INFO结构
VCI_BOARD_INFO 结构体包含 ZLGCAN 系列接口卡的设备信息。结构体将在
VCI_ReadBoardInfo函数中被填充。
typedef struct _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;
图 2.1 VCI_BOARD_INFO结构
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
3
2.1.2 VCI_CAN_OBJ结构
VCI_CAN_OBJ结构体在VCI_Transmit和VCI_Receive函数中被用来传送CAN信息帧。
typedef struct _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;
图 2.2 VCI_CAN_OBJ结构
2.1.3 VCI_CAN_STATUS结构
VCI_CAN_STATUS结构体包含 CAN控制器状态信息。结构体将在 VCI_ReadCanStatus
函数中被填充。
typedef struct _VCI_CAN_STATUS {
UCHAR ErrInterrupt;
UCHAR regMode;
UCHAR regStatus;
UCHAR regALCapture;
UCHAR regECCapture;
UCHAR regEWLimit;
UCHAR regRECounter;
UCHAR regTECounter;
DWORD Reserved;
} VCI_CAN_STATUS, *PVCI_CAN_STATUS;
图 2.3 VCI_CAN_STATUS结构
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
4
2.1.4 VCI_ERR_INFO结构
VCI_ERR_INFO结构体用于装载VCI库运行时产生的错误信息。结构体将在
VCI_ReadErrInfo函数中被填充。
typedef struct _ERR_INFO {
UINT ErrCode;
BYTE Passive_ErrData[3];
BYTE ArLost_ErrData;
} VCI_ERR_INFO, *PVCI_ERR_INFO;
图 2.4 VCI_ERR_INFO结构
2.1.5 VCI_INIT_CONFIG结构
VCI_INIT_CONFIG结构体定义了初始化 CAN的配置。结构体将在 VCI_InitCan函数中
被填充。
typedef struct _INIT_CONFIG {
DWORD AccCode;
DWORD AccMask;
DWORD Reserved;
UCHAR Filter;
UCHAR Timing0;
UCHAR Timing1;
UCHAR Mode;
} VCI_INIT_CONFIG, *PVCI_INIT_CONFIG;
图 2.5 VCI_INIT_CONFIG结构
下面以 VCI_CAN_OBJ 结构为例,在 LabVIEW7.0 中创建 VCI_CAN_OBJ 结构。先在
Array&Cluster 控件子模板选择一个簇的空壳放到前面板上,将其命名为 VCI_CAN_OBJ,
然后根据需要放置的控件多少用定位工具调整簇空壳的大小;按照 VCI_CAN_OBJ 结构成
员的顺序,从 Numeric控件子模板中取 Numeric Indicator控件或从前面板上移动控件到簇的
空壳中,并按图 2.6将各 Numeric Indicator控件重命名。
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
5
图 2.6 在前面板上创建 VCI_CAN_OBJ结构的簇
此时,簇壳内的成员的数据类型都为默认的 Double 类型。在簇壳内的边框上弹出快捷
菜单,选择 Representation。在下一级子菜单中选择与 VCI_CAN_OBJ 结构成员类型一致的
类型。
图 2.7 设置成员类型
2.2 调用 VCI库函数
在 LabVIEW中调用 VCI库函数的过程比较简单。LabVIEW在 Advanced函数子模板中
提供了 Calling Library Function Node,只要知道动态连接库里被导出的函数名称及其参数,
就可以通过 Calling Library Function Node调用。ZLG VCI函数库已经提供了库里的函数声
明,因此,在 LabVIEW中使用 VCI函数库将通过 Calling Library Function Node来实现。
以调用 VCI_OpenDevice函数为例。在 LabVIEW图形代码窗口中放上调用库函数节点,
用鼠标双击节点或使用快捷菜单命令 Configure弹出如图 2.8所示的对话框。
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
6
图 2.8 调用库函数 1
单击 Browse…按钮,打开一个文件对话框,找到 ControlCAN.dll 文件。或者直接输入
库文件路径和名称。
在 Function Name下拉列表框中照到 VCI_OpenDevice函数。或直接输入函数名。
图 2.9 调用库函数 2
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
7
在 Calling Conventions下拉列表框中选中 stdcall(WINAPI),因为 VCI库函数使用的是
stdcall调用约定。
Parameter框中的 return type不变。Type框中选Numeric。Data Type框中选Unsigned 32-bit
Interger。即指定返回 32位整形数。
单击 Add a Parameter After按钮,Parameter框中的选项变为图 2.10所示。将缺省值 agr1
改为 DevType,因为在 VCI_OpenDevice函数声明中定义了参数 DevType。
图 2.10 调用库函数 3
Type框中选 Numeric,并在 Data Type框中选 Unsigned 32-bit Integer。表示将编程时指
定的 LabVIEW数据类型为 32位无符号整型。Pass框中选择 Value。
同样,按以上步骤添加 DevIndex、Reserved参数。
单击 OK按钮退出这个对话框。调用库函数节点变为图 2.11所示中的情况。图中的参
数端口由上到下分别为 return type、DevType、DevIndex和 Reserved。每个端口均有一个输
入端和一个输出端,左边的端口为输入端,右边的端口位输出端。因为第一个端口是函数的
返回值,所以没有输入端,在图中可以看到其输入端为填充的蓝色。其他端口则是函数的参
数,如果参数的类型是指针的话,可以通过参数的输出端输出数据。在其他情况下,不需要
使用参数的输出端。
图 2.11 调用库函数程序框图
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
8
2.3 应用示例
应用示例 Demo演示了在 LabVIEW7.0下如何使用 VCI库函数。其界面如图 2.12所示。
图 2.12 demo界面
在Demo中实现了数据的收发,并将在CAN总线上收发的数据在列表示图中显示。Demo
程序当中有 3个主要的While循环:主循环、发送数据循环和接收数据循环。这三个循环是
并行运行的。其中,主循环处理与用户交互的界面,并通过用户事件 TREvent 与发送数据
循环和接收数据循环通信。
在主循环中使用事件驱动机制处理用户在前面板的操作。打开设备的程序框图如图
2.13所示。在图中调用 VCI_OpenDevice函数打开设备,如果打开成功,则调用 VCI_InitCAN
函数初始化设备,成功的话就处理一下前面板控件的状态。
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
9
图 2.13 打开设备框图
图 2.14 所示是接收数据的程序框图。接收数据的过程是在一个 while 循环中,这个循
环在程序已开始时就一直运行,直到前面板上的 stop按钮被按下并在其 Value Change事件
中使 stop2 的值变为 False 时才停止。在循环当中,只有设备已经启动时才会进行读操作。
在 Demo 中 VCI_Reveive函数一次只读取一帧,输出的数据保存到 VCI_CAN_OBJ_R 结构
中,如果 VCI_Reveive函数执行成功的话,就把接收到的数据通过事件传递给主循环处理。
图 2.14 接收数据
发送数据的过程与接收数据的过程相似。当前面板上的 Transmit 按钮被按下时,才会
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
10
把 VCI_CAN_OBJ_T 结构中的数据通过 VCI_Transmit 函数发送到 CAN 总线上。发送成功
后,生成一个 TREvent事件,并通过这个事件把 VCI_CAN_OBJ_T 结构的内容传递给主循
环显示。
图 2.15 发送数据
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
11
第3章 VCI函数调用参考
在 LabVIEW中使用 Calling Library Function Node调用 VCI库函数的配置如下各表所
示。
3.1 VCI_OpenDevice
Function name VCI_OpenDevice
Prototype
DWORD __stdcall VCI_OpenDevice(DWORD DeviceType, DWORD DeviceIndex,
DWORD Reserved);
Calling
Conventions
stdcall(WINAPI)
Reentrant
Type Data Type Data Format Pass
return type Numeric
Unsigned
32-bit Integer
Value
DeviceType Numeric
Unsigned
32-bit Integer
Value
Parameter
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
3.2 VCI_CloseDevice
Function name VCI_CloseDevice
Prototype DWORD __stdcall VCI_CloseDevice(DWORD DeviceType, DWORD DeviceIndex);
Calling
Conventions
stdcall(WINAPI)
Reentrant
Type Data Type Data Format Pass
return type Numeric
Unsigned
32-bit Integer
Value
DeviceType Numeric
Unsigned
32-bit Integer
Value
Parameter
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
3.3 VCI_InitCan
Function name VCI_InitCan
Prototype
DWORD __stdcall VCI_InitCan(DWORD DeviceType, DWORD DeviceIndex, DWORD
CANIndex, PVCI_INIT_CONFIG pInitConfig);
Calling
Conventions
stdcall(WINAPI)
Reentrant
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
12
Type Data Type Data Format Pass
return type Numeric
Unsigned
32-bit Integer
Value
DeviceType Numeric
Unsigned
32-bit Integer
Value
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
CANIndex Numeric
Unsigned
32-bit Integer
Value
Parameter
pInitConfig Adapt to Type
Handle by
Value
3.4 VCI_ReadBoardInfo
Function name VCI_ReadBoardInfo
Prototype
DWORD __stdcall VCI_ReadBoardInfo(DWORD DeviceType, DWORD DeviceIndex,
PVCI_BOARD_INFO pInfo);
Calling
Conventions
stdcall(WINAPI)
Reentrant
Type Data Type Data Format Pass
return type Numeric
Unsigned
32-bit Integer
Value
DeviceType Numeric
Unsigned
32-bit Integer
Value
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
Parameter
pInfo Adapt to Type
Handle by
Value
3.5 VCI_ReadErrInfo
Function name VCI_ReadErrInfo
Prototype
DWORD __stdcall VCI_ReadErrInfo(DWORD DeviceType, DWORD DeviceIndex,
DWORD CANIndex, PVCI_ERR_INFO pErrInfo);
Calling
Conventions
stdcall(WINAPI)
Reentrant
Type Data Type Data Format Pass
return type Numeric
Unsigned
32-bit Integer
Value
Parameter
DeviceType Numeric
Unsigned
32-bit Integer
Value
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
13
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
CANIndex Numeric
Unsigned
32-bit Integer
Value
pErrInfo Adapt to Type
Handle by
Value
3.6 VCI_ReadCanStatus
Function name VCI_ReadCanStatus
Prototype
DWORD __stdcall VCI_ReadCanStatus(DWORD VCI_ReadCanStatus, DWORD
DeviceIndex, DWORD CANIndex, PVCI_CAN_STATUS pCANStatus);
Calling
Conventions
stdcall(WINAPI)
Reentrant
Type Data Type Data Format Pass
return type Numeric
Unsigned
32-bit Integer
Value
DeviceType Numeric
Unsigned
32-bit Integer
Value
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
CANIndex Numeric
Unsigned
32-bit Integer
Value
Parameter
pCANStatus Adapt to Type
Handle by
Value
3.7 VCI_GetReference
Function name VCI_GetReference
Prototype
DWORD __stdcall VCI_GetReference(DWORD DeviceType, DWORD DevIndex,
DWORD DeviceIndex, DWORD RefType, PVOID pData);
Calling
Conventions
stdcall(WINAPI)
Reentrant
Type Data Type Data Format Pass
return type Numeric
Unsigned
32-bit Integer
Value
DeviceType Numeric
Unsigned
32-bit Integer
Value
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
Parameter
RefType Numeric
Unsigned
32-bit Integer
Value
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
14
pData Adapt to Type
Handle by
Value
3.8 VCI_SetReference
Function name VCI_SetReference
Prototype
DWORD __stdcall VCI_SetReference(DWORD DeviceType, DWORD DeviceIndex,
DWORD CANIndex, DWORD RefType, PVOID pData);
Calling
Conventions
stdcall(WINAPI)
Reentrant
Type Data Type Data Format Pass
return type Numeric
Unsigned
32-bit Integer
Value
DeviceType Numeric
Unsigned
32-bit Integer
Value
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
CANIndex Numeric
Unsigned
32-bit Integer
Value
RefType Numeric
Unsigned
32-bit Integer
Value
Parameter
pData Adapt to Type
Handle by
Value
3.9 VCI_GetReceiveNum
Function name VCI_GetReceiveNum
Prototype
ULONG __stdcall VCI_GetReceiveNum(DWORD DeviceType, DWORD DeviceIndex,
DWORD CANIndex);
Calling
Conventions
stdcall(WINAPI)
Reentrant
Type Data Type Data Format Pass
return type Numeric
Unsigned
32-bit Integer
Value
DeviceType Numeric
Unsigned
32-bit Integer
Value
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
Parameter
CANIndex Numeric
Unsigned
32-bit Integer
Value
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
15
3.10 VCI_ClearBuffer
Function name VCI_ClearBuffer
Prototype
DWORD __stdcall VCI_ClearBuffer(DWORD DeviceType, DWORD DeviceIndex,
DWORD CANIndex);
Calling
Conventions
stdcall(WINAPI)
Reentrant
Type Data Type Data Format Pass
return type Numeric
Unsigned
32-bit Integer
Value
DeviceType Numeric
Unsigned
32-bit Integer
Value
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
Parameter
CANIndex Numeric
Unsigned
32-bit Integer
Value
3.11 VCI_StartCAN
Function name VCI_StartCAN
Prototype
DWORD __stdcall VCI_StartCAN(DWORD DeviceType, DWORD DeviceIndex,
DWORD CANIndex);
Calling
Conventions
stdcall(WINAPI)
Reentrant
Type Data Type Data Format Pass
return type Numeric
Unsigned
32-bit Integer
Value
DeviceType Numeric
Unsigned
32-bit Integer
Value
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
Parameter
CANIndex Numeric
Unsigned
32-bit Integer
Value
3.12 VCI_ResetCAN
Function name VCI_ResetCAN
Prototype
DWORD __stdcall VCI_ResetCAN(DWORD DeviceType, DWORD DeviceIndex,
DWORD CANIndex);
Calling
Conventions
stdcall(WINAPI)
Reentrant
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
16
Type Data Type Data Format Pass
return type Numeric
Unsigned
32-bit Integer
Value
DeviceType Numeric
Unsigned
32-bit Integer
Value
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
Parameter
CANIndex Numeric
Unsigned
32-bit Integer
Value
3.13 VCI_Transmit
Function name VCI_Transmit
Prototype
ULONG __stdcall VCI_Transmit(DWORD DeviceType, DWORD DeviceIndex, DWORD
CANIndex, PVCI_CAN_OBJ pSend, ULONG Length);
Calling
Conventions
stdcall(WINAPI)
Reentrant
Type Data Type Data Format Pass
return type Numeric
Unsigned
32-bit Integer
Value
DeviceType Numeric
Unsigned
32-bit Integer
Value
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
CANIndex Numeric
Unsigned
32-bit Integer
Value
pSend Adapt to Type
Handle by
Value
Parameter
Length Numeric
Unsigned
32-bit Integer
Value
3.14 VCI_Receive
Function name VCI_Receive
Prototype
ULONG __stdcall VCI_Receive(DWORD DeviceType, DWORD DeviceIndex, DWORD
CANIndex, PVCI_CAN_OBJ pReceive, ULONG Length, INT WaitTime=-1);
Calling
Conventions
stdcall(WINAPI)
Reentrant
Type Data Type Data Format Pass Parameter
return type Numeric
Unsigned
32-bit Integer
Value
广州周立功单片机发展有限公司 Tel:(020)38730977 38730977 Fax:38730925 http://www.zlgmcu.com
17
DeviceType Numeric
Unsigned
32-bit Integer
Value
DeviceIndex Numeric
Unsigned
32-bit Integer
Value
CANIndex Numeric
Unsigned
32-bit Integer
Value
pReceive Adapt to Type
Handle by
Value
Length Numeric
Unsigned
32-bit Integer
Value
WaitTime Numeric
Signed 32-bit
Integer
Value
ZLGCAN
2005年 3月
本文档为【ZLGCAN接口函数库】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。