Linux下can发送和接收应用程序代码 - 技术 - 电子技术论坛 - 最好最受欢迎电子论坛!

Linux下can发送和接收应用程序代码

vvg ( 楼主 ) 2014-11-25 10:33:56  只看该作者 倒序浏览
Linux下can发送应用程序代码
  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  8. #include
  9. #include
  10. #include

  11. /**
  12. * @brief: print usage message
  13. * @Param: stream: output device
  14. * @Param: exit_code: error code which want to exit
  15. */
  16. void print_usage (FILE *stream, int exit_code)
  17. {
  18.     fprintf(stream, "Usage: option [ dev... ] n");
  19.     fprintf(stream,
  20.             "t-h  --help     Display this usage information.n"
  21.             "t-d  --device   The device can[0-1]n"
  22.             "t-i  --id       Set the can id that want to receiven");
  23.     exit(exit_code);
  24. }

  25. /**
  26. * @brief: main function
  27. * @Param: argc: number of parameters
  28. * @Param: argv: parameters list
  29. */
  30. int main(int argc, char *argv[])
  31. {
  32.     int s, nbytes, i;
  33.     char *device;
  34.     int id, next_option, device_flag=0, id_flag=0;
  35.     struct sockaddr_can addr;
  36.     struct ifreq ifr;
  37.     struct can_frame frame;
  38.     struct can_filter rfilter[1];
  39.     const char *const short_options = "hd:i:";
  40.     const struct option long_options[] = {
  41.         { "help",   0, NULL, 'h'},
  42.         { "device", 1, NULL, 'd'},
  43.         { "id", 1, NULL, 'i'},
  44.         { NULL,     0, NULL, 0  }
  45.     };
  46.      
  47.     while (1) {
  48.         next_option = getopt_long (argc, argv, short_options, long_options, NULL);
  49.         if (next_option < 0)
  50.             break;
  51.         switch (next_option) {
  52.             case 'h':
  53.                 print_usage (stdout, 0);
  54.                 break;
  55.             case 'd':
  56.                 device = optarg;
  57.                 device_flag = 1;
  58.                 break;
  59.             case 'i':
  60.                 id = atoi(optarg);
  61.                 id_flag = 1;
  62.                 break;
  63.             case '?':
  64.                 print_usage (stderr, 1);
  65.                 break;
  66.             default:
  67.                 abort ();
  68.         }
  69.     }
  70.      
  71.     if (!device_flag) {
  72.         print_usage (stdout, 0);
  73.         exit(0);   
  74.     }
  75.     /* create a socket */
  76.     s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  77.     strcpy(ifr.ifr_name, device);
  78.     /* determine the interface index */
  79.     ioctl(s, SIOCGIFINDEX, &ifr);                  
  80.     addr.can_family = AF_CAN;
  81.     addr.can_ifindex = ifr.ifr_ifindex;
  82.     /* bind the socket to a CAN interface */   
  83.     bind(s, (struct sockaddr *)&addr, sizeof(addr));
  84.      
  85.     if (id_flag) {   
  86.         /* define the filter rules */  
  87.         rfilter[0].can_id   = id;
  88.         rfilter[0].can_mask = CAN_SFF_MASK;
  89.         /* Set the filter rules */   
  90.         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
  91.     }
  92.     while(1) {
  93.         /* receive frame */
  94.         nbytes = read(s, &frame, sizeof(frame));           
  95.         /* printf the received frame */
  96.         if (nbytes > 0) {
  97.             printf("%s  %#x  [%d]  ", ifr.ifr_name, frame.can_id, frame.can_dlc);
  98.             for (i = 0; i < frame.can_dlc; i++)
  99.                 printf("%#x ", frame.data[i]);
  100.             printf("n");
  101.         }
  102.     }
  103.     close(s);
  104.     return 0;
  105. }
复制代码

Linux下can接收应用程序代码
  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  8. #include
  9. #include
  10. #include

  11. char *program_name;

  12. /**
  13. * @brief: print usage message
  14. * @Param: stream: output device
  15. * @Param: exit_code: error code which want to exit
  16. */
  17. void print_usage (FILE *stream, int exit_code)
  18. {
  19.     fprintf(stream, "Usage: %s [ dev... ] n", program_name);
  20.     fprintf(stream,
  21.             "t-h  --help     Display this usage information.n"
  22.             "t-d  --device   The device can[0-1]n"
  23.             "t-i  --id       Set the can id that want to sendn");
  24.     exit(exit_code);
  25. }
  26. /**
  27. * @brief: Hexadecimal number converted to decimal number
  28. * @Param: pHex: the Hexadecimal number that formularize string format
  29. */
  30. int hex2dec(const char * phex)
  31. {
  32.     int dwhexnum=0;
  33.          
  34.     if ((phex[0] == '0') && (phex[1] == 'x' || phex[1] == 'X')) {
  35.         phex = phex + 2;
  36.     }
  37.     for (; *phex!=0 ; phex++) {   
  38.          dwhexnum *= 16;
  39.          if ((*phex>='0') && (*phex<='9'))
  40.             dwhexnum += *phex-'0';
  41.          else if ((*phex>='a') && (*phex<='f'))
  42.             dwhexnum += *phex-'a'+10;
  43.          else if ((*phex>='A') && (*phex<='F'))
  44.             dwhexnum += *phex-'A'+10;
  45.          else {
  46.             printf("hex format error!n");
  47.             exit(0);   
  48.          }      
  49.     }
  50.      return dwhexnum;
  51. }

  52. /**
  53. * @brief: main function
  54. * @Param: argc: number of parameters
  55. * @Param: argv: parameters list
  56. */
  57. int main(int argc, char *argv[])
  58. {
  59.     int s, nbytes, i;
  60.     char *device;
  61.     int id, next_option, device_flag=0, id_flag=0;  
  62.     struct sockaddr_can addr;
  63.     struct ifreq ifr;
  64.     struct can_frame frame[1];
  65.     const char *const short_options = "hd:i:";
  66.     const struct option long_options[] = {
  67.         { "help",   0, NULL, 'h'},
  68.         { "device", 1, NULL, 'd'},
  69.         { "id", 1, NULL, 'i'},
  70.         { NULL,     0, NULL, 0  }
  71.     };
  72.      
  73.     program_name = argv[0];
  74.     while (1) {
  75.         next_option = getopt_long (argc, argv, short_options, long_options, NULL);
  76.         if (next_option < 0)
  77.             break;
  78.         switch (next_option) {
  79.             case 'h':
  80.                 print_usage (stdout, 0);
  81.                 break;
  82.             case 'd':
  83.                 device = optarg;
  84.                 device_flag = 1;
  85.                 break;
  86.             case 'i':
  87.                 id = hex2dec(optarg);
  88.                 id_flag = 1;
  89.                 break;
  90.             case '?':
  91.                 print_usage (stderr, 1);
  92.                 break;
  93.             default:
  94.                 abort ();
  95.         }
  96.     }
  97.      
  98.     if (!device_flag || !id_flag) {
  99.         print_usage (stdout, 0);
  100.         exit(0);   
  101.     }
  102.      
  103.     /* create a socket */
  104.     s = socket(PF_CAN, SOCK_RAW, CAN_RAW);     
  105.     strcpy(ifr.ifr_name, device );
  106.     /* determine the interface index */
  107.     ioctl(s, SIOCGIFINDEX, &ifr);                    
  108.     addr.can_family = AF_CAN;
  109.     addr.can_ifindex = ifr.ifr_ifindex;
  110.     /* bind the socket to a CAN interface */
  111.     bind(s, (struct sockaddr *)&addr, sizeof(addr));            
  112.     /* Set the filter rules */
  113.     setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
  114.     /* generate CAN frames */
  115.     frame[0].can_id = id;
  116.     frame[0].can_dlc = argc - 5;
  117.     for(i = 0; i < frame[0].can_dlc; i++) {
  118.         frame[0].data[i] = hex2dec(argv[5 + i]);   
  119.     }
  120.       
  121.     /* send CAN frames */
  122.     while(1) {
  123.         nbytes = write(s, &frame[0], sizeof(frame[0]));   
  124.         if (nbytes < 0) {
  125.             perror("can raw socket write");
  126.                 return 1;
  127.         }

  128.         /* paranoid check ... */
  129.         if (nbytes < sizeof(struct can_frame)) {
  130.                 fprintf(stderr, "read: incomplete CAN framen");
  131.                 return 1;
  132.         }
  133.         sleep(1);
  134.     }
  135.     close(s);
  136.     return 0;
  137. }
复制代码




4个回复

小杜城南 发表于 2014-12-16 21:54:14
好好努力,争取早日有所成
刘文田 发表于 2014-12-16 22:23:53
很好,正需要。
回复 1

举报 使用道具

葛君 发表于 2015-4-1 10:55:43
感谢,很需要            
临危之草 发表于 2017-3-2 15:25:56
波特率在哪里设置呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则


关闭

站长推荐上一条 /6 下一条

小黑屋|手机版|Archiver|电子发烧友 ( 湘ICP备2023018690号 )

GMT+8, 2024-9-29 23:45 , Processed in 0.528605 second(s), Total 42, Slave 33 queries .

Powered by 电子发烧友网

© 2015 bbs.elecfans.com

微信扫描
快速回复 返回顶部 返回列表