#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
static void Usage(void)
{
printf("\nCANLIB Dump Program\n");
printf("(Part of the CANLIB SDK from KVASER AB - http://www.kvaser.se)\n");
printf("\n");
printf("This is a sample program that just dumps all incoming messages on the screen,\n");
printf("or to a file.\n");
printf("\nUsage: candump [flags] [filename]\n");
printf(" -X Listen to CAN channel number X.\n");
printf(" -B<value> Set the bitrate. Value is any of 1000,500,250,125.\n");
printf(" -A<value> Set the CAN FD Arbitration bitrate. Value is any of 500,1000.\n");
printf(" -D<value> Set the CAN FD Data bitrate. Value is any of 1000,2000,4000,8000.\n");
printf(" Default bitrate is 125 kbit/s.\n");
printf(" -h Print this help text.\n");
printf(" -q Be more quiet than usual.\n");
printf(" -fd Use CAN FD.\n");
printf(" -virtual Accept virtual channels.\n");
printf("\nIf no filename is specified, standard output is used.\n\n");
printf("The following keys can be used during logging:\n");
printf(" ESC Stop logging.\n");
printf(" Q Be more quiet.\n");
printf(" q Be less quiet.\n");
printf("\nExample:\n");
printf("candump -B250 -0 logfile.log\n");
printf(" would set CAN channel 0 to 250 kbit/s and log to logfile.log\n");
exit(1);
}
int Bitrate = 0;
int BitrateFdA = 0;
int BitrateFdD = 0;
int Source = 0;
char Filename[512];
int Quiet = 0;
int can_open_flags = 0;
{
if (stat != canOK) {
char buf[50];
buf[0] = '\0';
fprintf(stderr, "%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
exit(1);
}
}
int InitCtrl(int ctrl)
{
if (hnd < 0) {
}
if (Bitrate != 0) {
if (stat < 0) {
ErrorExit("canSetBusParams", stat);
}
} else if (BitrateFdA != 0 && BitrateFdD != 0) {
if (stat < 0) {
ErrorExit("canSetBusParams", stat);
}
if (stat < 0) {
ErrorExit("canSetBusParamsFd", stat);
}
} else {
Usage();
}
if (stat < 0) {
ErrorExit("canBusOn", stat);
exit(1);
}
return hnd;
}
void main(int argc, char* argv[])
{
int i;
FILE *f;
int Ready;
unsigned long MessageCount;
unsigned int timeOffset;
int Overrun;
Filename[0] = '\0';
for (i=1; i<argc; i++) {
int tmp;
char c;
if (sscanf(argv[i], "-%d%c", &tmp, &c) == 1) {
Source = tmp;
} else if (sscanf(argv[i], "-B%d%c", &tmp, &c) == 1) {
switch (tmp) {
default : Usage();
}
} else if (sscanf(argv[i], "-A%d%c", &tmp, &c) == 1) {
switch (tmp) {
default : Usage();
}
} else if (sscanf(argv[i], "-D%d%c", &tmp, &c) == 1) {
switch (tmp) {
default : Usage();
}
} else if (strcmp(argv[i], "-q") == 0) {
Quiet++;
} else if (strcmp(argv[i], "-fd") == 0) {
} else if (strcmp(argv[i], "-virtual") == 0) {
} else if (strcmp(argv[i], "-h") == 0) {
Usage();
} else if (argv[i][0] != '-') {
strcpy(Filename, argv[i]);
} else {
Usage();
}
}
if (Quiet == 0) {
fprintf(stderr, "Logging to %s (candump -h for help; ESC to quit.)\n",
Filename[0]? Filename : "standard output");
}
hnd = InitCtrl(Source);
if (strlen(Filename) > 0) {
f = fopen(Filename, "w");
if (!f) {
fprintf(stderr, "Can't create log file '%s'.\n", Filename);
exit(1);
}
} else {
f = stdout;
}
if (stat < 0) {
ErrorExit("kvReadTimer", stat);
}
if (Quiet == 0) {
time_t t;
time(&t);
fprintf(f, "; Logging started at %s", ctime(&t));
fprintf(f, "; (x = Extended Id, R = Remote Frame, o = Overrun, N = NERR)\n");
fprintf(f, "; Ident xRoN DLC Data 0........................7 Time\n");
}
Ready = FALSE;
MessageCount = 0;
Overrun = FALSE;
while (!Ready) {
while (!_kbhit()) {
long id;
unsigned int dlc, flags;
unsigned char msg[64];
DWORD time;
long t, timeFrac, timeInt;
unsigned int i;
do {
stat =
canRead(hnd, &
id, msg, &dlc, &flags, &time);
if (stat == canOK && (Quiet <= 1)) {
fprintf(f, "%8ld%c%c%c%c%c%c %02u ",
id,
dlc);
} else {
fprintf(f, " (%04lx) Error ", id);
}
if ((flags & canMSG_RTR) == 0) {
for (i=0; i < dlc; i++) fprintf(f, "%3u ", msg[i]);
for (; i<8; i++) fprintf(f, " ");
} else {
fprintf(f, " ");
}
}
else {
if ((flags & canMSG_RTR) == 0) {
for (i=0; i < ((dlc > 8)? 8: dlc); i++) fprintf(f, "%3u ", msg[i]);
for (; i<8; i++) fprintf(f, " ");
} else {
fprintf(f, " ");
}
}
t = time - timeOffset;
if (t < 0) {
timeFrac = (-t) % 1000;
timeInt = (-t) / 1000;
fprintf(f, "-%5ld.%03ld\n", timeInt, timeFrac);
} else {
timeFrac = t % 1000;
timeInt = t / 1000;
fprintf(f, "%6ld.%03ld\n", timeInt, timeFrac);
}
if (flags & canMSGERR_OVERRUN) Overrun = TRUE;
MessageCount++;
}
} while (stat == canOK);
Sleep(50);
}
switch (_getch()) {
case 3:
case 27:
Ready = TRUE;
break;
case 'q':
if (Quiet > 0) Quiet--;
fprintf(stderr, "Quiet=%d\n", Quiet);
break;
case 'Q':
if (Quiet < 5) Quiet++;
fprintf(stderr, "Quiet=%d\n", Quiet);
break;
}
}
if (Quiet == 0) {
time_t t;
time(&t);
fprintf(f, "; Logging ended at %s", ctime(&t));
fprintf(f, "; %lu messages.%s\n",
MessageCount,
Overrun? " There were overruns." : "");
}
fclose(f);
}