imx6ul can调试(代码)

电玩女神 2022-12-30 03:56 363阅读 0赞

下面是我的代码,然后代码呢实际的使用 需要注意,如果重新设置了can的引脚对应关系,需要重新配置设备树文件;

更改设备树文件:https://blog.csdn.net/cao849861802/article/details/111604277

然后注意can0SystemInit和can1SystemInit中的频率,125000,这个可以修改和从机相同的频率用来通信;

can_id这个是表示can的id这个发送接收的对应要注意;

  1. #include <sys/ioctl.h>
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. #include <sys/ioctl.h>
  5. #include <net/if.h>
  6. #include <linux/can.h>
  7. #include <pthread.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #ifndef PF_CAN
  12. #define PF_CAN 29
  13. #endif
  14. #ifndef AF_CAN
  15. #define AF_CAN PF_CAN
  16. #endif
  17. /****************CAN START******************/
  18. int can0fd;
  19. int can1fd;
  20. struct sockaddr_can can0Addr;
  21. struct sockaddr_can can1Addr;
  22. void can0fdCreate()
  23. {
  24. struct ifreq ifr;
  25. int ret = 0;
  26. memset(&ifr,0,sizeof(ifr));
  27. can0fd = socket(PF_CAN,SOCK_RAW,CAN_RAW);
  28. if(can0fd < 0)
  29. {
  30. printf("can0fd create failed \r\n");
  31. return ;
  32. }
  33. strcpy((char *)(ifr.ifr_name),"can0");
  34. ioctl(can0fd,SIOCGIFINDEX,&ifr);
  35. can0Addr.can_family = AF_CAN;
  36. can0Addr.can_ifindex = ifr.ifr_ifindex;
  37. if((ret = bind(can0fd,(struct sockaddr*)&can0Addr,sizeof(can0Addr))) < 0)
  38. {
  39. printf("bind can0fd failed ,error = %d\r\n",ret);
  40. }
  41. }
  42. void can1fdCreate()
  43. {
  44. struct ifreq ifr;
  45. int ret = 0;
  46. memset(&ifr,0,sizeof(ifr));
  47. can1fd = socket(PF_CAN,SOCK_RAW,CAN_RAW);
  48. if(can1fd < 0)
  49. {
  50. printf("can1fd create failed \r\n");
  51. }
  52. strcpy((char *)(ifr.ifr_name),"can1");
  53. ioctl(can1fd,SIOCGIFINDEX,&ifr);
  54. can1Addr.can_family = AF_CAN;
  55. can1Addr.can_ifindex = ifr.ifr_ifindex;
  56. if((ret = bind(can1fd,(struct sockaddr*)&can1Addr,sizeof(can1Addr))) < 0)
  57. {
  58. printf("bind can1fd failed ,error = %d\r\n",ret);
  59. }
  60. }
  61. void can0SystemInit()
  62. {
  63. system("ifconfig can0 down");
  64. system("ip link set can0 type can bitrate 125000 triple-sampling on");
  65. system("ifconfig can0 up");
  66. }
  67. void can1SystemInit()
  68. {
  69. system("ifconfig can1 down");
  70. system("ip link set can1 type can bitrate 125000 triple-sampling on");
  71. system("ifconfig can1 up");
  72. }
  73. int can0BufSend(char *buf,int len)
  74. {
  75. int i = 0;
  76. int nbytes = 0;
  77. struct can_frame frame;
  78. printf("can 0 send buf = %s\r\n",buf);
  79. frame.can_id = 123;
  80. fcntl(fd,F_SETFL,FNDELAY);
  81. while(len > 0)
  82. {
  83. frame.can_dlc = (len >= 8) ? 8 : len;
  84. memcpy(frame.data,buf,frame.can_dlc);
  85. buf = buf + frame.can_dlc;
  86. len = len - frame.can_dlc;
  87. if ((nbytes = write(can0fd, &frame, sizeof(frame))) != sizeof(frame))
  88. {
  89. printf("can0 send buf failed ret = %d\r\n",nbytes);
  90. fcntl(fd,F_SETFL,0);
  91. return nbytes;
  92. }
  93. }
  94. fcntl(fd,F_SETFL,0);
  95. return 0;
  96. }
  97. int can1BufSend(char *buf,int len)
  98. {
  99. int i = 0;
  100. int nbytes = 0;
  101. struct can_frame frame;
  102. printf("can 1 send buf = %s\r\n",buf);
  103. frame.can_id = 124;
  104. while(len > 0)
  105. {
  106. frame.can_dlc = (len >= 8) ? 8 : len;
  107. memcpy(frame.data,buf,frame.can_dlc);
  108. buf = buf + frame.can_dlc;
  109. len = len - frame.can_dlc;
  110. if ((nbytes = write(can1fd, &frame, sizeof(frame))) != sizeof(frame))
  111. {
  112. printf("can1 send buf failed ret = %d\r\n",nbytes);
  113. return nbytes;
  114. }
  115. }
  116. return 0;
  117. }
  118. void *can0Task(void *arg)
  119. {
  120. int nbytes = 0;
  121. struct can_frame frame;
  122. char buf[16];
  123. can0SystemInit();
  124. can0fdCreate();
  125. while(1)
  126. {
  127. memset(&frame,0,sizeof(frame));
  128. if ((nbytes = read(can0fd, &frame, sizeof(frame))) <= 0)
  129. {
  130. printf("can 0 recv failed,return \r\n");
  131. return NULL;
  132. }
  133. if(frame.can_dlc == 0)
  134. continue;
  135. if(frame.can_id == 124)
  136. {
  137. memset(buf,0,sizeof(buf));
  138. memcpy(buf,frame.data,frame.can_dlc);
  139. printf("can 0 recv buf = %s\r\n",buf);
  140. }
  141. }
  142. return NULL;
  143. }
  144. void *can1Task(void *arg)
  145. {
  146. int nbytes = 0;
  147. struct can_frame frame;
  148. char buf[16];
  149. can1SystemInit();
  150. can1fdCreate();
  151. while(1)
  152. {
  153. memset(&frame,0,sizeof(frame));
  154. if ((nbytes = read(can1fd, &frame, sizeof(frame))) <= 0)
  155. {
  156. printf("can 1 recv failed,return \r\n");
  157. return NULL;
  158. }
  159. if(frame.can_dlc == 0)
  160. continue;
  161. if(frame.can_id == 123)
  162. {
  163. memset(buf,0,sizeof(buf));
  164. memcpy(buf,frame.data,frame.can_dlc);
  165. printf("can 1 recv buf = %s\r\n",buf);
  166. }
  167. }
  168. return NULL;
  169. }
  170. void canTask()
  171. {
  172. pthread_t ntid;
  173. pthread_create(&ntid, NULL, can0Task, NULL);
  174. printf("can 0 task start ---------\r\n");
  175. pthread_create(&ntid, NULL, can1Task, NULL);
  176. printf("can 1 task start ---------\r\n");
  177. sleep(5);
  178. can0BufSend("can 0 send",strlen("can 0 send"));
  179. can0BufSend("can 0 send",strlen("can 0 send"));
  180. can0BufSend("can 0 send",strlen("can 0 send"));
  181. sleep(5);
  182. can1BufSend("can 1 send",strlen("can 1 send"));
  183. can1BufSend("can 1 send",strlen("can 1 send"));
  184. can1BufSend("can 1 send",strlen("can 1 send"));
  185. }
  186. /****************CAN stop******************/
  187. int main()
  188. {
  189. canTask();
  190. while(1)
  191. {
  192. sleep(1);
  193. }
  194. return 0;
  195. }

发表评论

表情:
评论列表 (有 0 条评论,363人围观)

还没有评论,来说两句吧...

相关阅读

    相关 imx6ul can调试代码

    下面是我的代码,然后代码呢实际的使用 需要注意,如果重新设置了can的引脚对应关系,需要重新配置设备树文件; 更改设备树文件:[https://blog.csdn.net/c