首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >arduino在can总线卷帘中的应用

arduino在can总线卷帘中的应用
EN

Stack Overflow用户
提问于 2019-03-08 21:30:38
回答 1查看 820关注 0票数 0

我想把VGM CAN messageReachstacker 42-45 tonnes中救出来

在这里,我使用带有arduino MEGA 2560CAN-BUS shield

这是我的当前代码:

代码语言:javascript
复制
#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();
    }
}

这是做测试时的结果,是我不明白结果的问题。

根据文档,结果应该是这样的:

这是文件的另一部分:

如果有人需要更多的信息或者不明白我在找什么,你可以要求你帮助我

发送数据:

代码语言:javascript
复制
// 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
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-09 20:07:35

在文档和正在生成的输出之间有两个不适合的部分:

  1. 数据有效载荷
  2. CAN帧的ID。

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

代码语言:javascript
复制
Serial.print(buf[i], HEX)

若要将有效负载打印为十六进制字符,请执行以下操作。

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

代码语言:javascript
复制
unsigned long canId = CAN.getCanId();

在文档中,CAN ID也是以十六进制打印的,因此这里还使用:

代码语言:javascript
复制
Serial.println(canId, HEX);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55071275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档