【问题标题】:use of arduino with can bus sheild使用带有 can bus shield 的 arduino
【发布时间】:2019-03-08 21:30:38
【问题描述】:

我正在尝试从Reachstacker 42-45 tonnes 中获取VGM CAN message

我在哪里使用 arduino MEGA 2560CAN-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 为 11 位或 29 位,具体取决于您使用的版本,您不应将它们存储在无符号字符中。
  • CAN shield 跟什么有什么关系?它只是哑金属,不会影响您的源代码。
  • 我知道他们是 29 位 @Guille

标签: arduino can-bus


【解决方案1】:

您的文档和正在生成的输出之间有两部分不匹配:

  1. 数据负载
  2. CAN 帧的 ID

对于数据有效负载,它只是一个格式化问题。您将每个字节打印为整数值,而在文档中将其打印为十六进制值。使用

Serial.print(buf[i], HEX)

将有效负载打印为十六进制字符。

对于 CAN 帧的 ID,您可以从文档中看到它们不适合无符号字符,正如 @Guille 的评论中已经提到的那样。实际上,这些是 29 位标识符,理想情况下,您应该将其存储在适当大小的变量中。理想情况下使用unsigned long

unsigned long canId = CAN.getCanId();

在文档中,CAN ID 也以十六进制打印,所以这里也使用:

Serial.println(canId, HEX);

【讨论】:

  • 非常感谢@oh.dae.su,在将它带到生产环境之前我需要做一个测试,我有两个 arduino:Arduino Mega 2560 和 Arduino One,有两个 CAN-BUS -shield v1.2,在每个带有 can-bus 的 arduino 中,模拟一个发送数据和另一个接收数据,在我的问题中添加我用来改进发送数据的代码
  • 将我用来模拟发送数据的代码添加到我的问题中
  • 很高兴我的回答很有帮助
  • 朋友@oh.dae.su,我怎么能做一个过滤器,让它只读取我需要的ID。例如 id 18FFFE27
  • 如何在unsigned char canId = CAN.getCanId() 之后放置一个if 语句来检查canId 的值,并将打印部分保留在if 语句中。或者,请检查 CAN 控制器的内置过滤器可能性:github.com/Seeed-Studio/CAN_BUS_Shield/blob/master/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
相关资源
最近更新 更多