我正在尝试在两个Arduino板(大型和微型)之间设置本地CAN总线。但是我不能让他们彼此收到任何消息...
对于两块板,我正在使用以下CAN模块:https://www.amazon.com/CHENBO-Electronics-Receiver-Controller-Development/dp/B015W4D9WY/ref=sr_1_3
Arduino板与CAN之间的连接通过以下方式完成:
ProMicro:
超级2560:
两块板均已连接到PC,并已连接到串行监视器。
我也有一个树莓派PI用于此模块:https://www.waveshare.com/rs485-can-hat.htm
从该软件,我正在使用以下Arduino库:https://github.com/Seeed-Studio/CAN_BUS_Shield 而这个是给Raspberry的:https://github.com/sebi2k1/node-can
这是Arduino micro的代码:
#define DEBUG_EN 1
#include <SPI.h>
#include "mcp_can.h"
const int SPI_CS_PIN = 10;
long unsigned int rxId;
unsigned char len = 0;
unsigned char buf[8];
long unsigned int txID = 0xF0;
unsigned char stmp[8] = {0, 9, 8, 7, 6, 5, 4, 3};
MCP_CAN CAN(SPI_CS_PIN);
void setup()
{
Serial.begin(9600);
delay(1000);
while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_8MHz))
{
Serial.println("CAN BUS Module Failed to Initialize");
Serial.println("Retrying....");
delay(200);
}
Serial.println("CAN BUS Module Initialized!");
Serial.println("PGN\t\tByte0\tByte1\tByte2\tByte3\tByte4\tByte5\tByte6\tByte7");
}
void loop()
{
if (CAN_MSGAVAIL == CAN.checkReceive())
{
Serial.println("I have something on micro...");
CAN.readMsgBuf(&len, buf);
rxId = CAN.getCanId();
Serial.print("0x");
Serial.print(rxId, HEX);
Serial.print("\t");
for (int i = 0; i < len; i++)
{
if (buf[i] > 15)
{
Serial.print("0x");
Serial.print(buf[i], HEX);
}
else
{
Serial.print("0x0");
Serial.print(buf[i], HEX);
}
Serial.print("\t");
}
Serial.println();
Serial.println("sending message...");
CAN.sendMsgBuf(txID, 1, 8, stmp);
}
}
对于Arduino mega几乎相同:
#include <mcp_can.h>
#include <SPI.h>
const int SPI_CS_PIN = 53;
unsigned char len = 0;
unsigned char buf[8];
long unsigned int rxId;
long unsigned int txID = 0xFF;
unsigned char stmp[8] = {1, 2, 3, 4, 5, 6, 7, 8};
MCP_CAN CAN(SPI_CS_PIN);
void setup()
{
Serial.begin(9600);
delay(1000);
while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_8MHz))
{
Serial.println("CAN BUS Module Failed to Initialize");
Serial.println("Retrying....");
delay(200);
}
Serial.println("CAN BUS Module Initialized!");
Serial.println("PGN\t\tByte0\tByte1\tByte2\tByte3\tByte4\tByte5\tByte6\tByte7");
}
void loop()
{
if (CAN_MSGAVAIL == CAN.checkReceive())
{
Serial.println("I have something no mega...");
CAN.readMsgBuf(&len, buf);
rxId = CAN.getCanId();
Serial.print("0x");
Serial.print(rxId, HEX);
Serial.print("\t");
for (int i = 0; i < len; i++)
{
if (buf[i] > 15)
{
Serial.print("0x");
Serial.print(buf[i], HEX);
}
else
{
Serial.print("0x0");
Serial.print(buf[i], HEX);
}
Serial.print("\t");
}
Serial.println();
Serial.println("sending...");
CAN.sendMsgBuf(txID, 1, 8, stmp);
}
}
并在树莓派上调试代码:
var can = require("socketcan");
var channel = can.createRawChannel("can0", true);
// Log any message
channel.addListener("onMessage", function(msg) {
console.log(msg);
});
channel.start();
console.log("Sending initial message...");
channel.send({id: 0x0F, ext: 1, rtr: false, data: Buffer.from([1, 1, 1, 1, 1, 1, 1, 1])});
console.log("Listening to messages");
在开始测试之前,我正在Raspberry上执行这些命令:
sudo ip link set can0 type can bitrate 500000
sudo ifconfig can0 up
这些是输出:
来自Raspberry:
Sending initial message...
Listening to messages
{ ts_sec: 1560890627,
ts_usec: 297829,
id: 240,
ext: true,
data: <Buffer 00 09 08 07 06 05 04 03> }
{ ts_sec: 1560890627,
ts_usec: 318244,
id: 255,
ext: true,
data: <Buffer 01 02 03 04 05 06 07 08> }
这意味着两个Arduino板都在向CAN总线发送正确的数据...
来自Arduino Micro:
lib is working....
Enter setting mode success
set rate success!!
Enter Normal Mode Success!!
CAN BUS Module Initialized!
PGN Byte0 Byte1 Byte2 Byte3 Byte4 Byte5 Byte6 Byte7
I have something on micro...
0xF 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01
sending message...
这里我缺少来自Arduino Mega的消息
以及来自Arduino Mega
lib is working....
Enter setting mode success
set rate success!!
Enter Normal Mode Success!!
CAN BUS Module Initialized!
PGN Byte0 Byte1 Byte2 Byte3 Byte4 Byte5 Byte6 Byte7
I have something no mega...
0xF 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01
sending...
这里还有一个问题是Arduino Micro没有消息
我如何使Arduino开发板从CAN总线接收所有消息?