CAN_RX Function for Receiving CAN Messages in C
This code defines a function called 'CAN_RX' that receives a CAN message with an ID specified by the input parameter 'rx_ID'. The function first initializes a message object with the specified ID and acceptance mask, and waits for new data to arrive. Once new data is received, the function reads the message and extracts the message ID and data, which are then stored in a 'IfxMultican_Message' structure called 'rxResult'. Finally, the function returns the 'rxResult' structure containing the received message data.
IfxMultican_Message rxResult;
IfxMultican_Message CAN_RX(uint32 rx_ID)
{
{
// create message object config
IfxMultican_Can_MsgObjConfig canMsgObjConfig;
IfxMultican_Can_MsgObj_initConfig(&canMsgObjConfig, &canDstNode);
// assigned message object:
canMsgObjConfig.msgObjId = 1;
canMsgObjConfig.messageId = rx_ID;
canMsgObjConfig.acceptanceMask = 0x7FFFFFFFUL;
canMsgObjConfig.frame = IfxMultican_Frame_receive;
canMsgObjConfig.control.messageLen = IfxMultican_DataLengthCode_8;
canMsgObjConfig.control.extendedFrame = FALSE;
canMsgObjConfig.control.matchingId = TRUE;
// initialize message object
IfxMultican_Can_MsgObj_init(&canDstMsgObj, &canMsgObjConfig);
}
while (!IfxMultican_Can_MsgObj_isRxPending(&canDstMsgObj))
{}
IfxMultican_Message rxMsg;
IfxMultican_Message_init(&rxMsg, 0xdead, 0xdeadbeef, 0xdeadbeef, IfxMultican_DataLengthCode_8); // start with invalid values
// read message
IfxMultican_Status readStatus = IfxMultican_Can_MsgObj_readMessage(&canDstMsgObj, &rxMsg);
// if new data is been received report an error
if((readStatus & IfxMultican_Status_newData))
{
rxResult.data[0] = rxMsg.data[0];
rxResult.data[1] = rxMsg.data[1];
rxResult.id = rxMsg.id;
}
// rxResult.data[0] = rxMsg.data[0];
// rxResult.data[1] = rxMsg.data[1];
// rxResult.id = rxMsg.id;
return rxResult;
}
原文地址: https://www.cveoy.top/t/topic/n5nL 著作权归作者所有。请勿转载和采集!