技术博客
11/10/2016
作者 Mikkel
自定义通道名
CANlib v5.17的新功能之一是添加自定义通道名。在支持的设备¹上,您就就可以识别出特定设备的通道。当启动Kvaser产品的时候,会显示一个新行,如图1所示。我们尚未分配任何自定义通道名称,因此Kvaser 产品显示为“ – ”。
图1:Kvaser产品现在可以显示自定义通道名
要分配新的自定义通道名,请使用setchannelname.exe工具²。该工具可以在您安装CANlib SDK的Bin目录中找到。我将CANlib SDK安装在了D:\CanlibSDK\CanlibSDK_5.17,
所以工具位置就在D:\CanlibSDK\CanlibSDK_5.17\Bin\setchannelname.exe。运行-h参数工具可打印使用帮助。
d:\>D:\CanlibSDK\CanlibSDK_5.17\Bin\setchannelname.exe --help
Channel naming utility for Kvaser AB products.
-h --help | Print this information.
-channel=X | CANlib channel index
-name="ABc de" | Name for selected channel.
-v | Verbose mode.
Example:
setchannelname -channel=4 -name="Test unit 1"
因此,让我们为设备通道1设置自定义通道名。从图1可以看出,设备通道1可以使用CANlib通道0访问。让我们将此自定义通道名设置为“Backbone 2”。
D:\CanlibSDK\CanlibSDK_5.17\Bin\setchannelname.exe -channel=0 -name="Backbone 2"
echo
自定义通道名的最大长度因设备而异,但至少要有24个字节(包括空终止符)。
图2:自定义通道名现在设置为“Backbone 2”
为了读取CANlib中的自定义通道名,我们使用canCHANNELDATA_CUST_CHANNEL_NAME调用函数canGetChannelData()。参见下面清单1中的第39行。
#include <stdio.h>
#include <canlib.h>
void Check(char* id, canStatus stat)
{
if (stat != canOK) {
char buf[50];
buf[0] = '#include <stdio.h>
#include <canlib.h>
void Check(char* id, canStatus stat)
{
if (stat != canOK) {
char buf[50];
buf[0] = '\0';
canGetErrorText(stat, buf, sizeof(buf));
printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
}
}
void main(int argc, char* argv[])
{
canStatus stat;
int i, chanCount;
canInitializeLibrary();
stat = canGetNumberOfChannels(&chanCount);
Check("canGetNumberOfChannels", stat);
if (chanCount<0 || chanCount > 1000) {
printf("ChannelCount = %d but I don't believe it.\n", chanCount);
exit(1);
} else {
printf("%d channels.\n", chanCount);
}
for (i=0; i<chanCount; i++) {
char name[64];
printf("== Channel %d ===============================\n", i);
stat = canGetChannelData(i, canCHANNELDATA_CHANNEL_NAME, name, sizeof(name));
Check("canCHANNELDATA_CHANNEL_NAME", stat);
printf("Channel name = '%s'\n", name);
stat = canGetChannelData(i, canCHANNELDATA_CUST_CHANNEL_NAME, name, sizeof(name));
if (stat != canOk) {
name[0] = '\0';
}
printf("Custom Channel name = '%s'\n", name);
}
}
';
canGetErrorText(stat, buf, sizeof(buf));
printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
}
}
void main(int argc, char* argv[])
{
canStatus stat;
int i, chanCount;
canInitializeLibrary();
stat = canGetNumberOfChannels(&chanCount);
Check("canGetNumberOfChannels", stat);
if (chanCount<0 || chanCount > 1000) {
printf("ChannelCount = %d but I don't believe it.\n", chanCount);
exit(1);
} else {
printf("%d channels.\n", chanCount);
}
for (i=0; i<chanCount; i++) {
char name[64];
printf("== Channel %d ===============================\n", i);
stat = canGetChannelData(i, canCHANNELDATA_CHANNEL_NAME, name, sizeof(name));
Check("canCHANNELDATA_CHANNEL_NAME", stat);
printf("Channel name = '%s'\n", name);
stat = canGetChannelData(i, canCHANNELDATA_CUST_CHANNEL_NAME, name, sizeof(name));
if (stat != canOk) {
name[0] = '#include <stdio.h>
#include <canlib.h>
void Check(char* id, canStatus stat)
{
if (stat != canOK) {
char buf[50];
buf[0] = '\0';
canGetErrorText(stat, buf, sizeof(buf));
printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
}
}
void main(int argc, char* argv[])
{
canStatus stat;
int i, chanCount;
canInitializeLibrary();
stat = canGetNumberOfChannels(&chanCount);
Check("canGetNumberOfChannels", stat);
if (chanCount<0 || chanCount > 1000) {
printf("ChannelCount = %d but I don't believe it.\n", chanCount);
exit(1);
} else {
printf("%d channels.\n", chanCount);
}
for (i=0; i<chanCount; i++) {
char name[64];
printf("== Channel %d ===============================\n", i);
stat = canGetChannelData(i, canCHANNELDATA_CHANNEL_NAME, name, sizeof(name));
Check("canCHANNELDATA_CHANNEL_NAME", stat);
printf("Channel name = '%s'\n", name);
stat = canGetChannelData(i, canCHANNELDATA_CUST_CHANNEL_NAME, name, sizeof(name));
if (stat != canOk) {
name[0] = '\0';
}
printf("Custom Channel name = '%s'\n", name);
}
}
';
}
printf("Custom Channel name = '%s'\n", name);
}
}
清单1:读取自定义通道名简短实例程序。
运行上面的程序输出以下结果:
4 channels.
== Channel 0 ===============================
Channel name = 'Kvaser Memorator 2xHS v2 #1 (Channel 0)'
Custom Channel name = 'Backbone 2'
== Channel 1 ===============================
Channel name = 'Kvaser Memorator 2xHS v2 #1 (Channel 1)'
Custom Channel name = ''
== Channel 2 ===============================
Channel name = 'Virtual #0 (Channel 0)'
canCHANNELDATA_CUST_CHANNEL_NAME: failed, stat=-32 (Not implemented)
Custom Channel name = ''
== Channel 3 ===============================
Channel name = 'Virtual #0 (Channel 1)'
canCHANNELDATA_CUST_CHANNEL_NAME: failed, stat=-32 (Not implemented)
Custom Channel name = ''
要删除自定义通道名,您只需要写一个新的通道名。
希望此简短概述让您了解利用自定义通道名功能。
我们非常感谢您能通过电子邮件发送到support@kvaser.com提供错误报告、贡献、改进建议以及其他类似方面。
脚注
¹所有新款设备,如Kvaser Memorator 2xHS v2,都支持自定义通道名。
²在Linux下不支持写入自定义通道名。