将arduino与can bus shield一起使用
use of arduino with can bus sheild
我正在尝试从 Reachstacker 42-45 tonnes
中获取 VGM CAN message
我使用 arduino MEGA 2560
和 CAN-BUS shield
这是我当前的代码:
#include <SPI.h>
#include "mcp_can.h"
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
START_INIT:
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init ok!");
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT;
}
}
void loop()
{
unsigned char len = 0;
unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned char canId = CAN.getCanId();
Serial.println("-----------------------------");
Serial.println("get data from ID: ");
Serial.println(canId);
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i]);
Serial.print("\t");
}
Serial.println();
}
}
这是做测试时的结果,我不明白结果的问题
根据文档应该有这样的结果:
这是文档的另一部分:
如果有人需要更多信息或者不明白我在找什么,你可以请求你需要的帮助我
发送数据:
// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
START_INIT:
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init ok!");
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT;
}
}
unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
void loop()
{
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0x00, 0, 8, stmp);
delay(100); // send data per 100ms
}
您有两部分不适合您的文档和您正在生成的输出:
- 数据负载
- CAN 帧的 ID
对于数据负载来说,这只是一个格式化问题。您将每个字节打印为整数值,而在文档中它打印为十六进制值。使用
Serial.print(buf[i], HEX)
将负载打印为十六进制字符。
对于 CAN 帧的 ID,您从文档中看到它们不适合在 unsigned char 中,正如@Guille 的评论中已经提到的那样。实际上,这些是 29 位标识符,最好将其存储在适当大小的变量中。理想情况下使用 unsigned long
:
unsigned long canId = CAN.getCanId();
在文档中,CAN ID 也以十六进制打印,所以这里也使用:
Serial.println(canId, HEX);
我正在尝试从 Reachstacker 42-45 tonnes
VGM CAN message
我使用 arduino MEGA 2560
和 CAN-BUS shield
这是我当前的代码:
#include <SPI.h>
#include "mcp_can.h"
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
START_INIT:
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init ok!");
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT;
}
}
void loop()
{
unsigned char len = 0;
unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned char canId = CAN.getCanId();
Serial.println("-----------------------------");
Serial.println("get data from ID: ");
Serial.println(canId);
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i]);
Serial.print("\t");
}
Serial.println();
}
}
这是做测试时的结果,我不明白结果的问题
根据文档应该有这样的结果:
这是文档的另一部分:
如果有人需要更多信息或者不明白我在找什么,你可以请求你需要的帮助我
发送数据:
// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
START_INIT:
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init ok!");
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT;
}
}
unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
void loop()
{
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0x00, 0, 8, stmp);
delay(100); // send data per 100ms
}
您有两部分不适合您的文档和您正在生成的输出:
- 数据负载
- CAN 帧的 ID
对于数据负载来说,这只是一个格式化问题。您将每个字节打印为整数值,而在文档中它打印为十六进制值。使用
Serial.print(buf[i], HEX)
将负载打印为十六进制字符。
对于 CAN 帧的 ID,您从文档中看到它们不适合在 unsigned char 中,正如@Guille 的评论中已经提到的那样。实际上,这些是 29 位标识符,最好将其存储在适当大小的变量中。理想情况下使用 unsigned long
:
unsigned long canId = CAN.getCanId();
在文档中,CAN ID 也以十六进制打印,所以这里也使用:
Serial.println(canId, HEX);