CAN通信-程序编译成功但仍无法正常发送指令

2023-10-133851
AI 快速预览详细 收起
本文讨论了使用Arduino Uno R4与伺服驱动器进行CAN通信时遇到的传输失败问题。通过检查CAN.begin函数和错误代码,可以有效解决这一问题。具体步骤包括初始化CAN总线、设置波特率,并在循环中发送数据。如果发送失败,会输出错误代码以便进一步排查。
  1. #include <Arduino_CAN.h>   
  2. #include <CanUtil.h>  
  3. #include <R7FA4M1_CAN.h>
  4. #include <R7FA6M5_CAN.h>        
  5. #include <SyncCanMsgRingbuffer.h>   
  6. static uint32_t const CAN_ID = 0x601;
  7. void setup() {
  8. Serial.begin(115200);
  9. while (!Serial) {}
  10.   CAN.begin(CanBitRate::BR_125k);
  11.   if (CAN.begin(CanBitRate::BR_125k)) {
  12.     Serial.println("CAN.begin(...) Complished.");
  13.     delay(100);
  14.     Serial.println("CAN_BR is 125k.");
  15.   }
  16. if (!CAN.begin(CanBitRate::BR_250k)) {
  17.     Serial.println("CAN.begin(...) failed.");
  18.   }
  19. }
  20. static uint32_t msg0_cnt = 0;
  21. static uint32_t msg1_cnt = 0;
  22. static uint32_t msg2_cnt = 0;
  23. void loop(){
  24.   uint8_t const msg0_data[] = { 0xCA, 0xFE, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
  25.   memcpy((void *)(msg0_data + 4), &msg0_cnt, sizeof(msg0_cnt));
  26.   CanMsg const msg0(CanStandardId(CAN_ID), sizeof(msg0_data), msg0_data);
  27.   CAN.write(msg0);
  28.   if (int const rc = CAN.write(msg0); rc < 0) {
  29.     Serial.print("CAN.write(...) failed with error code ");
  30.     Serial.println(rc);
  31.   }
  32.   msg0_cnt++;
  33.   delay(1000);
  34. }
复制代码
[size=15.008px]需要用Arduino Uno R4(板载CAN总线)跟伺服驱动器通信。
[size=15.008px]程序编译成功,但串行端口监视器始终显示传输失败的消息。[size=15.008px]

[size=15.008px]我觉得可能是发送命令的函数没用对,希望有大佬能给指点一下!!!!

FriOctober-202310132887..png (29.35 KB, 下载次数: 2319)

CAN通信故障排查 · Arduino Uno R4_image_3.webp

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(1)
DFEDU
DFEDU
接线拍照
- 没有更多了 -