]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/utils/send.c
53135dba486f3ad06000055cba769371b69835e0
[lincan.git] / lincan / utils / send.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <sys/ioctl.h>
6
7 #include "../include/can.h"
8 #define MAXL 40
9
10 int main(void)
11 {
12         int i=0, fd=0, ret=0;
13         unsigned long bits;
14         char specialfile[MAXL+1]="/dev/can0", buf[MAXL+1];
15         struct canmsg_t message;
16
17         printf("\nThis program allows you to send  a stream of Can messages.\n");
18         printf("Please answer the following questions:\n\n");
19
20 //              message.flags = 0;
21         message.flags |= MSG_EXT;  //hard code EXT for now
22
23         printf("From wich device file would you like to send the message?\n");
24         printf(specialfile);
25         *buf='\0';
26         fgets(buf,MAXL,stdin);
27         buf[strcspn(buf,"\n")]='\0';
28         if(*buf)
29                 strncpy(specialfile,buf,MAXL);
30         specialfile[MAXL]='\0';
31         printf("Enter the starting Message ID ");
32         scanf("%lx",&message.id);
33         printf("Enter the Message Length ");
34         scanf("%d",&message.length);
35         for (i=0; i<message.length; i++) {
36                 printf("Enter data byte [%d] ",i);
37                 scanf("%x",(int *)&message.data[i]);
38         }
39
40         fd=open(specialfile,O_RDWR);
41         if (fd<0) {
42                 printf("Error opening %s\n",specialfile);
43                 return -1;
44         }
45
46         bits=0;
47         while(1) {
48                 message.flags = MSG_EXT;
49                 message.id++;
50                 message.length %= 8;
51                 message.length++;
52                 bits += 8*(4+message.length);
53                 memcpy(message.data,&bits,sizeof(bits));
54                 ret=write(fd, &message, sizeof(struct canmsg_t));
55                 if (ret<0)
56                         printf("Error sending message from %s\n",specialfile);
57         }
58
59         if (close(fd)) {
60                 printf("Error closing %s\n",specialfile);
61                 return -1;
62         }
63
64         return 0;
65 }