【发布时间】: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