我想把VGM CAN message从Reachstacker 42-45 tonnes中救出来
在这里,我使用带有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
}发布于 2019-03-09 20:07:35
在文档和正在生成的输出之间有两个不适合的部分:
对于数据有效负载来说,这只是一个格式化问题。将每个字节打印为整数值,而在文档中打印为十六进制值。使用
Serial.print(buf[i], HEX)若要将有效负载打印为十六进制字符,请执行以下操作。
对于CAN帧的ID,您可以从文档中看到它们不适合于未签名的字符,正如@Guille的注释中已经提到的那样。实际上,这些都是29位标识符,理想情况下,这些标识符应该存储在适当大小的变量中。理想情况下使用unsigned long:
unsigned long canId = CAN.getCanId();在文档中,CAN ID也是以十六进制打印的,因此这里还使用:
Serial.println(canId, HEX);https://stackoverflow.com/questions/55071275
复制相似问题