The following examples can also be found in the Arduino IDE:
File -> Examples -> DICE -> CAN2SER ->
Examples
a) Create an additional serial data channel to the PC
In some cases it might be good to have not only one Serial
interface to the PC, but more independent serial channels.
The normal Serial
link [channel 0] could be used to interact with the user.
A second link [channel 1] could be used to print debug information. Similar to Linux ‘standard input, output, error’ pipes.
// create an additional CAN2SER instance
// to the PC (channel 1, no handshake)
CAN2SER SerialPC2(&MCAN0, &BOOTLOADER_SETTING, 1,false);
void setup()
{
Serial.begin();
SerialPC2.begin();
// wait for both PC monitors to be opened
while(!Serial || !SerialPC2) { yield(); }
}
void loop ()
{
Serial.println("Hello from Serial");
SerialPC2.println("Hello from SerialPC2");
delay(1000);
}
b) Serial link between two devices
If there is more than one devices on the same CAN bus then they can communicate to each other via a CAN2SER serial link, too.
A possible use case could be to send remote commands from one device to the other (e.g. using the Console library) or to transfer files between them (see FileTransfer library) .
For the following you need two devices connected on the same CAN bus.
The example creates the CAN2SER instances and gives them the information about the peer it should talk to (peers CAN-ID + LID). Then it prints everything that it receives from the other node so that the user can see it.
1) Configuration of their bootloader-settings of the devices as follows:

Note: see Device addressing for details about theses settings
2) Open two (independent) Arduino IDE windows and open the two different example projects for node A and node B in one of each:File -> Examples -> DICE -> CAN2SER -> SerialLinkBetweenTwoDevices -> SerialLinkDevice_A
File -> Examples -> DICE -> CAN2SER -> SerialLinkBetweenTwoDevices -> SerialLinkDevice_B
3) Select the target details for each device in the Arduino menuTools -> Board -> DICE SAMC -> 'DICE xxx' or 'DICE yyy'
Tools -> Local ID -> 1 or 2
Tools -> CAN-ID -> 0x766 or 0x767
Tools -> tools -> flash only
4) Compile and upload the project to the corresponding targets dice-A and dice-B
5) Open a CAN2SER-monitor (e.g. the standalone version of the tool)
6) Create a second window with Misc -> Open new window
7) Modify the settings of the CAN2SER-Monitor window for each device (Device CAN-ID & LID)
Code example for device A (with CAN-ID: 0x766; LID: 1)
// Create a CAN2SER instance to connect to
// nodeB CAN-ID 0x767, LID 2, channel 0, handshake on
CAN2SER SerialToPeer( &MCAN0, &BOOTLOADER_SETTING, 0, true, 0x767, 2 );
void setup()
{
Serial.begin();
SerialToPeer.begin();
}
loop()
{
if(SerialToPeer.available())
Serial.write( SerialToPeer.read() );
if(Serial.available())
SerialToPeer.println(Serial.readStringUntil('\n'));
}
Code example for device B (with CAN-ID: 0x767; LID: 2):
// Create a CAN2SER instance to connect to
// nodeA CAN-ID 0x766, LID 1, channel 0, handshake on
CAN2SER SerialToPeer( &MCAN0, &BOOTLOADER_SETTING, 0, true, 0x766, 1 );
void setup()
{
Serial.begin();
SerialToPeer.begin();
}
loop()
{
if(SerialToPeer.available())
Serial.write( SerialToPeer.read() );
if(Serial.available())
SerialToPeer.println(Serial.readStringUntil('\n'));
}
Now, in this example there are in fact 3 CAN2SER communication links in operation. One each between the PC and both devices, and one link between the two devices.