【发布时间】:2019-01-23 09:44:42
【问题描述】:
我正在创建一个软件,它必须在具有 CAN 网络的单元中请求数据。 出于某种原因,我只在被要求时获得了一部分数据。 如代码所示,该单元的 CAN 频率为 100 kbit 或 100000 位。 我正在使用 Nucleo-F767ZI 并连接到 CAN 网络,我正在使用开发板的内置功能。
我已经查看了正在发送的数据是否有错误,但这似乎很好,因为不同的程序能够毫无错误地读取它。 我目前用来测试的代码就是这个位。
#include "mbed.h"
CAN can1(PD_0, PD_1);//Sets the pins for the CAN.
Serial pc(USBTX, USBRX);//Selects the type of serial and assigns a name to it.
char buffer[300];
int main (){
pc.baud(9600);//sets serialportbaud to 9600
CANMessage test;
can1.frequency(100000); //Sets frequency speed. it has to be 100000 otherwise the unit wont respond
test.format = CANStandard; //Selects the format for the can message.
test.id = 24; //Gives the can message an ID.
test.len = 2; //How long the message is in bytes.
test.data[0] = 0; //Select the data for this byte.
test.data[1] = 3;
can1.write(test); //this sends the data of 0 , 3 with the id of 24 and gets data from the unit its being send to.
printf("\n\rsended \n\r"); //confirmation that it has come this far without crashing.
while(true){
can1.read(receive);//receives any message send over the network except his own.
sprintf(buffer, "%d/%d/%d/%d/%d/%d/%d/%d", receive.data[0], receive.data[1], receive.data[2], receive.data[3], receive.data[4], receive.data[5], receive.data[6], receive.data[7]);//stores data in buffer
printf("%s \n\r", buffer);//shows what the data was
}
}
【问题讨论】:
-
为什么使用十进制格式的 CAN 标识符?那是代码的味道,它们总是以十六进制列出。另一件值得一提的是,100kbps 并不是标准的波特率之一,而是一种比较常见的非标准扩展。您确定所有涉及的设备都可以处理它吗?当然,解决这个问题的关键是用你的 CAN 监听器观察实际的总线。没有一个句号,您就无法开发 CAN 应用程序。
-
(顺便说一句,像这样的问题可能会在electronics.stackexchange.com 得到更好的答案,大多数 CAN 总线专家都潜伏在那里。在那里问软件问题是完全可以的,只要它们是关于微控制器固件的或其他与硬件相关的)
标签: c++ embedded can-bus mbed nucleo