CAN总线开发实例

  1. 在本例中,有两个进程,其中一个进程负责报文的发送(标识符为0x11和0x22),另一个进程负责报文的接收和过滤(只接收标识符为0x11的
  2. 报文)。

  3. /* 1.报文发送程序 */  
  4. #include <stdio.h> 
  5. #include <stdlib.h> 
  6. #include <string.h> 
  7. #include <unistd.h> 
  8. #include <net/if.h> 
  9. #include <sys/ioctl.h> 
  10. #include <sys/socket.h> 
  11. #include <linux/can.h> 
  12. #include <linux/can/raw.h> 
  13. int main()  
  14. {  
  15.     int s, nbytes;  
  16. struct sockaddr_can addr;  
  17. struct ifreq ifr;  
  18. struct can_frame frame[2] = {{0}};  
  19. s = socket(PF_CAN, SOCK_RAW, CAN_RAW);      //创建套接字  
  20. strcpy(ifr.ifr_name, "can0" );  
  21. ioctl(s, SIOCGIFINDEX, &ifr);                   //指定can0设备  
  22. addr.can_family = AF_CAN;  
  23. addr.can_ifindex = ifr.ifr_ifindex;  
  24. bind(s, (struct sockaddr *)&addr, sizeof(addr));            //将套接字与can0绑定  
  25. //禁用过滤规则,本进程不接收报文,只负责发送  
  26.         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);  
  27.         //生成两个报文  
  28. frame[0].can_id = 0x11;  
  29. frame[0]. can_dlc = 1;  
  30. frame[0].data[0] = 'Y';  
  31. frame[0].can_id = 0x22;  
  32. frame[0]. can_dlc = 1;  
  33. frame[0].data[0] = 'N';  
  34. //循环发送两个报文  
  35. while(1) {  
  36. nbytes = write(s, &frame[0], sizeof(frame[0]));     //发送frame[0]  
  37. if (nbytes != sizeof(frame[0])) {  
  38.                 printf("Send Error frame[0]\n!");  
  39.                 break;                              //发送错误,退出  
  40. }  
  41.     sleep(1);  
  42. nbytes = write(s, &frame[1], sizeof(frame[1]));     //发送frame[1]  
  43. if (nbytes != sizeof(frame[0])) {  
  44.                 printf("Send Error frame[1]\n!");  
  45.                 break;  
  46. }  
  47.     sleep(1);  
  48. }  
  49. close(s);  
  50. return 0;  
  51. }  
  52. /* 2. 报文过滤接收程序 */  
  53. #include <stdio.h> 
  54. #include <stdlib.h> 
  55. #include <string.h> 
  56. #include <unistd.h> 
  57. #include <net/if.h> 
  58. #include <sys/ioctl.h> 
  59. #include <sys/socket.h> 
  60. #include <linux/can.h> 
  61. #include <linux/can/raw.h> 
  62. int main()  
  63. {  
  64. int s, nbytes;  
  65. struct sockaddr_can addr;  
  66. struct ifreq ifr;  
  67. struct can_frame frame;  
  68. struct can_filter rfilter[1];  
  69. s = socket(PF_CAN, SOCK_RAW, CAN_RAW);      //创建套接字  
  70. strcpy(ifr.ifr_name, "can0" );  
  71. ioctl(s, SIOCGIFINDEX, &ifr);                   //指定can0设备  
  72. addr.can_family = AF_CAN;  
  73. addr.can_ifindex = ifr.ifr_ifindex;  
  74. bind(s, (struct sockaddr *)&addr, sizeof(addr));            //将套接字与can0绑定  
  75.         //定义接收规则,只接收表示符等于0x11的报文  
  76. rfilter[0].can_id   = 0x11;  
  77. rfilter[0].can_mask = CAN_SFF_MASK;  
  78. //设置过滤规则  
  79. setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));   
  80.         while(1) {  
  81.             nbytes = read(s, &frame, sizeof(frame));            //接收报文  
  82.             //显示报文  
  83. if (nbytes > 0) {  
  84. printf(“ID=0x%X DLC=%d data[0]=0x%X\n”, frame.can_id,  
  85. frame.can_dlc, frame.data[0]);  
  86. }  
  87. }  
  88. close(s);  
  89. return 0;  
  90. }  
  • 1
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值