#include "stdio.h"
#include "signal.h"
static void check(
const char *name,
canStatus status);
static void intHandler(int unused);
static void usage();
static int interrupt = 0;
static const char *CONF_500KBIT = "{"
"\"tseg1\" : 59,"
"\"tseg2\" : 20,"
"\"sjw\" : 16,"
"\"brp\" : 2"
"}";
int main(int argc, char *argv[])
{
int channel = -1;
for (int i=1; i<argc; i++) {
if (sscanf(argv[i], "-ch=%d", &channel) == 1);
else {
usage();
exit(1);
}
}
if (channel == -1) {
usage();
exit(1);
}
check("kvDiagAttachAnalyzer", stat);
check("kvDiagSetProgram", stat);
check("kvDiagStart", stat);
signal(SIGINT, intHandler);
while (1) {
if (stat == canOK) {
printSample(&sample);
}
if (interrupt) {
break;
}
}
check("kvDiagStop", stat);
check("kvDiagDetachAnalyzer", stat);
check("canClose", stat);
return 0;
}
static void check(
const char *name,
canStatus status)
{
if (status != canOK) {
printf("%s returned %d\n", name, status);
exit(1);
}
}
static void intHandler(int unused)
{
interrupt = 1;
}
{
uint64_t time = sample->
sample.startTime;
int value = sample->
sample.startValue;
int i = 0;
printf("Start \t End \t Length (microseconds) \t Value\n");
printf("-----------------------------------------------\n");
do {
printf("%6llu \t %6llu \t %2.2f \t\t %u\n",
time,
time + sample->
sample.edgeTimes[i],
(1.0 * sample->
sample.edgeTimes[i]) / (1.0 * sample->
sample.sampleFreq),
value);
time += sample->
sample.edgeTimes[i];
value = !value;
i++;
} while (i < sample->sample.edgeCount);
printf("-----------------------------------------------\n\n");
}
{
printf("CAN ID: 0x%x SEQ: %u. DLC: %u, flags: 0x%x, data:",
if (sample->
msg.dlc > 0) {
for (
int i=0; i < sample->
msg.dlc; i++) {
printf(
" %02X", (
unsigned char)sample->
msg.data[i]);
}
}
printf("\n\n");
printEdges(sample);
}
static void usage()
{
printf(
"Usage: 'normal_example -ch=n', "
"where 'n' is a CANlib channel with analyzer capability.\n\n"
"Note: This program requires a working CAN bus with traffic to function properly.\n");
}