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