具有2个Arduino / MCP2515节点的CAN总线网络是否有效?

时间:2017-11-19 01:18:08

标签: networking arduino can-bus

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

#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

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

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

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