文章
Receive CAN FD messages
refer to NTCAN Application Developers Manual document charpter
- 1.4.2 Integration and Migration
- 8.2 Receiving messages (Classical CAN and CAN FD)
example code
fd_rcv.c:程序展示如何调用NTCAN API来读取接收到的CAN FD消息。 如下code snippet是需要注意的内容。
uint32_t mode=NTCAN_MODE_FD; /* Mode bits for canOpen */
NTCAN_BAUDRATE_X baud; /* Bit rate configuration */
CMSG_X cmsg[8]; /* Buffer for can messages */
baud.mode = NTCAN_BAUDRATE_MODE_INDEX;
baud.flags = NTCAN_BAUDRATE_FLAG_FD;
baud.arb.u.idx = NTCAN_BAUD_1000; /* Nominal bit rate: 1000KBit/s */
baud.data.u.idx = NTCAN_BAUD_5000; /* Data phase bit rate: 5 MBit/s */
retvalue = canSetBaudrateX(rxhandle, &baud);
for(;;) {
/*
* Set max numbers of messages that can be returned with
* one canReadX() call according to buffer size.
*/
len = 8;
retvalue = canReadX(rxhandle, &cmsg[0], &len, NULL);
if (retvalue == NTCAN_RX_TIMEOUT)
{
printf("canReadX() returned timeout\n");
continue;
}
else if(retvalue != NTCAN_SUCCESS)
{
printf("canReadX() failed with error %d!\n", retvalue);
}
else
{
printf("Function canReadX() returned OK !\n");
printf("ID of received message :%x!\n", cmsg[0].id);
printf("DLC of received message :%x!\n", NTCAN_DLC(cmsg[0].len));
if(NTCAN_IS_FD(cmsg[0].len)) {
// printf("BRS of received message :%x!\n", !NTCAN_IS_FD_WITHOUT_BRS(cmsg[0].len));
if(NTCAN_IS_FD_WITHOUT_BRS(cmsg[0].len))
printf("CAN FD without BRS!\nThe bit rate of data phase is NOT changed.\n");
else
printf("CAN FD with BRS!\nThe bit rate of data phase is changed.\n");
} else
{
printf("RTR of received message :%x!\n", NTCAN_IS_RTR(cmsg[0].len));
}
for (i=0;i<NTCAN_LEN_TO_DATASIZE(cmsg[0].len);i++)
printf("Byte %d of received message :%x!\n", i, cmsg[0].data[i]);
}
break;
};
程序运行
首先建立一个CAN FD的总线。本人Demo总线系统上有两个设备,一个设备为esd CAN-PCIe/402-B4-FD安装在Ubuntu台式电脑上,上述的example code编译和运行在此电脑上。另一个设备为CAN-USB/3-FD连接到Win11笔记本电脑,此笔记本电脑上安装了最新版的CANreal软件。
参考CANreal中CAN FD的章节,向总线发送CAN FD的消息。编辑CAN FD消息时可以尝试勾选/不勾选"No bitrate switch(CA FD -BR)"的选项。接收端运行example code等待接收并解析CAN FD消息。