我正在创建一个软件,它必须在具有 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
}
}