This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
/
CANRx.c
72 lines (66 loc) · 1.81 KB
/
CANRx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#define THREAD_USE_POSIX
#include "libtsctl.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void CANMessagePrint(CANMessage *msg) {
struct tm * tm;
int i;
if (msg->flags & FLAG_CONTROL) printf("*** control packet\n");
if (msg->flags & FLAG_BUS_ERROR) printf("*** bus error\n");
if (msg->flags & FLAG_ERROR_PASSIVE) printf("*** error passive\n");
if (msg->flags & FLAG_DATA_OVERRUN) printf("*** data overrun error\n");
if (msg->flags & FLAG_ERROR_WARNING) printf("*** error warning\n");
tm = localtime((time_t *)&msg->t_sec);
printf("%02d:%02d:%02d.%06d %c%c ",
tm->tm_hour, tm->tm_min, tm->tm_sec,msg->t_usec,
(msg->flags & FLAG_EXT_ID) ? 'E' : 'S',
(msg->flags & FLAG_RTR) ? 'R' : '.');
if (msg->flags & FLAG_CONTROL) {
printf("id=0x%08x ", msg->id);
printf("cmd=%d ",msg->length);
printf("dat=");
for (i = 0; i < 8 ; i++) {
printf("0x%02x", msg->data[i]);
if (i != (msg->length - 1)) printf(":");
}
} else if (msg->length != 0) {
printf("id=0x%08x ", msg->id);
if (msg->length < 0 || msg->length > 8) {
printf("bad length=%d\n",msg->length);
} else {
printf("dat=");
for (i = 0; i < msg->length; i++) {
printf("0x%02x", msg->data[i]);
if (i != (msg->length - 1)) printf(":");
}
}
}
printf("\n");
}
CAN *can;
static void alarmsig(int x) {
can->Abort(can);
}
int main(int argc,char *argv[]) {
int i;
CANMessage msg;
can = CANInit(0);
if (!can) {
fprintf(stderr,"Error: No CAN present\n");
return 1;
}
if (SignalCapture(2,alarmsig) < 0) {
fprintf(stderr,"signal capture error:%m\n");
return 1;
}
can->BaudSet(can,1000000);
printf("Waiting to Rx\n");
i = can->Rx(can,&msg);
if (i > 0) {
CANMessagePrint(&msg);
} else {
printf("Error %d receiving\n",i);
}
return 0;
}