Description

utils/consoleCAN.h provides the following function to a console application:

  • s_console_CAN_cmds() :
    • candump canX – CAN monitor on can0 or can1 (send ‘stop’ to stop it)
    • cangen canX – send a random CAN messages on can0 or can1 (send ‘stop’ to stop it)
    • can1baud – change baudrate of CAN1 [125/250/500/1000] (please use bootloader settings for CAN0 baudrate)

Note: Each CAN bus interface has to be initialised first before it can be accessed by the console functions! The main CAN bus MCAN0 is initialised automatically by the start-up code. For devices with two CAN busses the MCAN1 interface has to be started in the setup() function.

Syntax (examples)

#include "utils/consoleCAN.h"
. . .
#ifdef MCAN1_CANID
MCAN1.begin(((uint32_t)BOOTLOADER_SETTING.u16_get_can_baudrate()) * 1000);
MCAN1.setMode(MCP_LISTENONLY);
#endif
. . .
s_console_string = s_console_CAN_cmds( s_console_string );

Parameters

String s_console_string – String that holds the user command. Normally returned by s_console_routine() or a previous console function.

Returns

String : the user command or an empty String “” if the user command was recognised and processed by this function. (see of commands above)

Example Codes:

#include "consoleLogin.h"
#include "utils/consoleCAN.h"
void setup()
{
 #ifdef MCAN1_CANID
  MCAN1.begin(((uint32_t)BOOTLOADER_SETTING.u16_get_can_baudrate()) * 1000);	 
  MCAN1.setMode(MCP_LISTENONLY);
 #endif
  console_init(&Serial);
}
void loop()
{
  String s_console_string = s_console_routine();  
  s_console_CAN_cmds(s_console_string);
}

Notes and Warnings

This function and its commands might be helpful to test and learn how to work with the CAN bus(ses).

See the source code as example code for your own application:

C:\Users\yourName\AppData\Local\Arduino15\packages\dice\hardware\samc\1.0.0\libraries\Console\src\... 

In a ‘real world’ application, these functions might not be necessary or it might even be problematic to offer the end-user the opportunity to send random messages and change the baud rate.

Please keep in mind that each included file and its used function increase the size of the resulting application. So in order to keep the application binaries as small as possible, include only functions that are really necessary for the final application.

An example can be found in the Arduino IDE menu:
File -> Examples -> Consoles -> Dev_Console

See also

SAMC21_CAN – Library
BOOTLOADER SETTINGS – Library – setting the baudrate and mode of the main CAN bus MCAN0