Description

CAN2SER() – Call the constructor if you need further instances of serial links over the CAN bus.
The start-up code already creates an instance of the CAN2SER class to communicate to the PC: Serial that can be used by the application straight away.
This means you only need to create new ones (by calling the constructor) if you want:

  • to create an additional serial link channel to the PC
  • or to create a serial connection to another device

Syntax

CAN2SER mySerial ( opt_can_bus, opt_bootloader_sets, ou8_Channel, ob_handshake , ou16_CAN_LISTEN_ID, ou8_Peers_LID);

Parameters

type and variabledescriptioncode example
SAMC21_CAN* opt_can_buspointer to the CAN object (&MCAN0 or &MCAN1)&MCAN0
BOOTLOADER_SETTINGS_CLASS* opt_bootloader_setspointer to bootloader setting instance&BOOTLOADER_SETTING
uint8_t ou8_Channelchannel number [0…3]1
bool ob_handshakesending handshakes to peerfalse
uint16_t ou16_CAN_LISTEN_IDCAN-ID of the peer; we are listening for this ID0x765
uint8_t ou8_Peers_LIDlocal ID of the destination / peer63
(hint: using the ‘code example’ values would set up a second serial link to the PC)

In the majority of cases, the first two parameters will be &MCAN0 and &BOOTLOADER_SETTING. These two objects are pre-created and stand for the first CAN-bus and the bootloader settings (holding the details about the CAN-ID + LID of our device).

Returns

nothing

Example Code

The following example creates a second and third CAN2SER object that can be used just like Serial for communications to the PC-CAN2SER-Monitors. The new objects will use channel 1 and 2 whereas Serial uses channel 0 by default.

// Create the additional global objects
CAN2SER SerialPC2(&MCAN0, &BOOTLOADER_SETTING, 1, false);
CAN2SER SerialPC3(&MCAN0, &BOOTLOADER_SETTING, 2, false);

void setup()
{  
  // Start all 3 serial connections
  Serial.begin();
  SerialPC2.begin();
  SerialPC3.begin();

  // Wait for the user to open 3 can2ser-monitor windows
  while (!Serial || !SerialPC2 || !SerialPC3) 
  {
    yield(); // wait for all 3 PC-CAN2SER-monitors to be opened 
  }
  // Send 'hello' through all three link channels

  Serial.println("Hello PC. Here is Serial");
  Serial2.println("Hello PC. Here is Serial2");
  Serial3.println("Hello PC. Here is Serial3");
}

void loop () 
{
}

Notes and Warnings

See also

Example to create a serial link between two devices
Example to create an additional link to the PC
Device addressing scheme
Bootloader settings class
CAN bus class