(Originally posted on the comments to the hookup guide)
ON AST-CAN485, how do I check whether there is a CAN bus message already received in the buffers/MOBs? The example code’s shows how to receive the message with Msg.cmd = CMD_RX_DATA, can_cmd(&Msg) and can_get_status(&Msg), which waits for a message to come in. In my application I’d like to check whether there is a message, process it if there is one, and perform other tasks if there is none, then loop.
It looks like you just need to switch the while loop around the can_get_status function call to a check if the command has been completed. Then the Arduino loop function can be used to periodically check while you do other things. Something like the following should do the trick:
bool rx_cmd_active = false;
void loop() {
if(rx_cmd_active == false) {
// Clear the message buffer
clearBuffer(&Buffer[0]);
// Send command to the CAN port controller
Msg.cmd = CMD_RX_DATA;
// Wait for the command to be accepted by the controller
while(can_cmd(&Msg) != CAN_CMD_ACCEPTED);
rx_cmd_active = true;
}
if(can_get_status(&Msg) != CAN_STATUS_NOT_COMPLETED) {
// Data is now available in the message object
// Print received data to the terminal
serialPrintData(&Msg);
rx_cmd_active = false; // ensure new rx command is initiated
}
// Do other stuff
}
Note that I don’t have one of these boards in front of me to check the above code, so some tweaking may be needed. I based it solely on the library implementation. I hope it helps anyway!
This worked well! It successfully received the latest message which was sent while the other stuff is done.
My other question is, how to receive/retrieve all messages, one by one, that are received by the controller while the ‘other stuff’ is performed?
For example, when the bus traffic is high, a burst of multiple messages may be on the bus in a very short amount of time. Since the AT90CAN128 has 15 MOBs, my understanding is that up to 15 messages can be received and stored waiting for processing. The question is how to retrieve all 15 messages.