-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceive-block.c
85 lines (79 loc) · 1.72 KB
/
receive-block.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
73
74
75
76
77
78
79
80
81
82
83
84
85
/* This example puts all received datas in the file 'logfile.txt',
* you can compare them with the transmited datas to test whether they are
* right
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "can4linux.h"
#define STDDEV "can0"
#define MAX 5
/***********************************************************************
*
* main
*
*/
int main(int argc,char **argv)
{
int fd;
int got;
int c,count = 0;
char *pname;
extern char *optarg;
extern int optind;
FILE * logFile;
canmsg_t rx[MAX];
char device[50];
printf("usage: %s [dev] \n", argv[0]);
if(argc > 1)
{
sprintf(device, "/dev/%s", argv[1]);
}
else
{
sprintf(device, "/dev/%s", STDDEV);
}
fd = open(device, O_RDONLY);
if( fd < 0 )
{
fprintf(stderr,"Error opening CAN device %s\n", device);
perror("open");
exit(1);
}
printf("using CAN device %s\n", device);
printf("Receive CAN messages will be written to logfile.txt file\n");
logFile = fopen("logfile.txt","w");
while(1) //count<500)
{
got=read(fd, rx, MAX);
if( got > 0)
{
int i;
int j;
for(i = 1; i <= got ; i++)
{
fprintf(logFile,"%d\t",*(int*)rx[i-1].data );
if((count + i)%10 == 0)
fprintf(logFile,"\n" );
fflush(stdout);
fflush(logFile);
}
count+=got;
}
else
{
printf("Received with ret=%d\n", got);
fflush(stdout);
}
}
printf("count: %d\n",count);
fclose(logFile);
close(fd);
return 0;
}