how-to/c/listChannels.c

This example shows how to show information about all enumerated CANlib channels. See Library Initialization and Identifying Devices and Channels for more information.

------------------------------------------------------------------------------

#include <stdint.h>
#include <stdio.h>
#include "canlib.h"
static void check(char* id, canStatus stat);
void show_channel_info(int channel_no);
// Find out how many channels that exists and loop over them
void list_channels(void)
{
char driverName[256];
int chanCount = 0;
canStatus stat;
int i;
stat = canGetNumberOfChannels(&chanCount);
check("canGetNumberOfChannels", stat);
printf("Found %d channel(s).\n", chanCount);
for (i = 0; i < chanCount; i++) {
show_channel_info(i);
}
} // list_channels
// Print status message if stat contains an error code
static void check(char* id, canStatus stat)
{
if (stat != canOK) {
char buf[50];
buf[0] = '\0';
// Retreive informational text about the status code
canGetErrorText(stat, buf, sizeof(buf));
printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
}
} // check
void show_channel_info(int channel_no)
{
canStatus stat;
char name[256];
char driverName[256];
char custChanName[40];
unsigned int ean[2], fw[2], serial[2];
unsigned int chan_no_on_card;
memset(name, 0, sizeof(name));
memset(custChanName, 0, sizeof(custChanName));
memset(ean, 0, sizeof(ean));
memset(fw, 0, sizeof(fw));
memset(serial, 0, sizeof(serial));
stat = canGetChannelData(channel_no, canCHANNELDATA_DEVDESCR_ASCII, &name, sizeof(name));
check("canGetChannelData: DEVDESCR_ASCII", stat);
stat = canGetChannelData(channel_no, canCHANNELDATA_CARD_UPC_NO, &ean, sizeof(ean));
check("canGetChannelData: CARD_UPC_NO", stat);
stat = canGetChannelData(channel_no, canCHANNELDATA_CARD_SERIAL_NO, &serial, sizeof(serial));
check("canGetChannelData: CARD_SERIAL_NO", stat);
stat = canGetChannelData(channel_no, canCHANNELDATA_CARD_FIRMWARE_REV, &fw, sizeof(fw));
check("canGetChannelData: CARD_FIRMWARE_REV", stat);
&chan_no_on_card, sizeof(chan_no_on_card));
check("canGetChannelData: canCHANNELDATA_CHAN_NO_ON_CARD", stat);
custChanName, sizeof(custChanName));
printf("ch %2.1d: %-22s\t%x-%05x-%05x-%x, s/n %u, v%u.%u.%u :%d \"%s\"\n",
channel_no, name,
(ean[1] >> 12), ((ean[1] & 0xfff) << 8) | ((ean[0] >> 24) & 0xff),
(ean[0] >> 4) & 0xfffff, (ean[0] & 0x0f),
serial[0],
fw[1] >> 16, fw[1] & 0xffff, fw[0] & 0xffff,
chan_no_on_card, custChanName);
} // list_channel