【问题标题】:Will a CAN Bus network with 2 Arduino / MCP2515 nodes work?带有 2 个 Arduino / MCP2515 节点的 CAN 总线网络可以工作吗?
【发布时间】:2017-11-19 01:18:08
【问题描述】:

我想使用 Arduino Pro Minis 和 MCP2515 卡建立一个包含多个节点的 CAN 网络。但我无法让 Receive 工作。

#include <mcp_can.h>
#include <SPI.h>

long unsigned int  rxId;
unsigned char      len = 0;
unsigned char      rxBuf[8];
char               msgString[128];
#define  CAN0_INT  2                    // Set INT to pin 2
MCP_CAN            CAN0(10);            // Set CS to pin 10

void setup() {
    Serial.begin(115200);       
    //  Initialize MCP2515 running at 8MHz with a baudrate of 125kb/s
    //  and the masks and filters disabled.
    while (CAN_OK != CAN0.begin(MCP_ANY, CAN_125KBPS, MCP_8MHZ)) {
        Serial.println("CAN BUS Module Failed to Initialize.");
    } 
    Serial.println("MCP2515 Initialized Successfully!");
    CAN0.setMode(MCP_NORMAL);
    pinMode(CAN0_INT, INPUT);        // Configuring pin for /INT input
}

void loop() {
    if(!digitalRead(CAN0_INT)) {  // If CAN0_INT is low, read receive buffer
        CAN0.readMsgBuf(&rxId, &len, rxBuf);   // Read data: len = data length, buf = data byte(s)

        if((rxId & 0x80000000) == 0x80000000)  // Is ID standard (11 bits) or extended (29 bits)?
            sprintf(msgString, "Extended ID: 0x%.8lX  DLC: %1d  Data:", (rxId & 0x1FFFFFFF), len);
        else
            sprintf(msgString, "Standard ID: 0x%.3lX   DLC: %1d  Data:", rxId, len);
        Serial.print(msgString);

        if((rxId & 0x40000000) == 0x40000000) {    // Is message a remote request frame?
            sprintf(msgString, " REMOTE REQUEST FRAME");
            Serial.print(msgString);
        } else {
            for(byte i = 0; i<len; i++) {
                sprintf(msgString, " 0x%.2X", rxBuf[i]);
                Serial.print(msgString);
            }
        }        
        Serial.println();
    }
}

但是,我得到的只是错误消息,包括:

Entering Configuration Mode Failure

我在这里错过了什么?

【问题讨论】:

  • “CAN 网络”具体是什么意思?常见用法是指校园区域网络,虽然我听说它是​​指受控接入网络,但这似乎对您的问题没有意义。
  • “来自维基百科,控制器局域网 (CAN) 总线是一种“车辆总线标准,旨在允许微控制器和设备在没有主机的情况下在车辆内相互通信。”这些设备也可以称为电子控制单元 (ECU)。本质上,CAN 总线是车辆内的一堆链接的 ECU,它们基于广播与每个 ECU 进行通信。每个 ECU 拦截每个广播,但单独决定是否对它做出反应。” (instructables.com/id/…)
  • 好的。我的观点是,当有多种含义时,您需要更加具体。您应该编辑您的问题以使其更清楚。

标签: networking arduino can-bus


【解决方案1】:

我让电路工作了。 2 节点 CAN 总线正在通信。
我找到了这个site 并进行了一些更改:

  1. 我的 Arduino ProMini MISI、MISO 引脚未与 MCP2515 上的 SI、SO 引脚对齐。
  2. 我使用了 CAN_BUS_Shield 库。

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多