This library allows to tunnel serial data through the CAN bus between the device and the PC or between two devices. The class inherits from ‘HardwareSerial’, hence it can be used just like any Serial link (e.g., UART, USB) as known in Arduino Serial. On the PC side the CAN2SER Monitor is embedded in the Arduino IDE as part of the dice support package. The library supports up to four can2ser-link channels between two nodes. That is like four serial cables connected between the two devices / PC.
To use this library
There is no need to manually include the library since it is part of the core code. Also one global object is already created (channel 0) by the start up code:
Serial
Example usage:
Serial.begin();
Serial.println("hello world!");

Example
// Simple example using the Serial (CAN2SER)
// interface that tunnels serial data through the
// CAN bus.
void setup()
{
// wait for the handshake from the PC (can2ser-monitor)
while(!Serial);
Serial.println("Hello user what is your name");
}
void loop()
{
// Read data from Serial
if(Serial.peek() != -1)
{
String s_userName = Serial.readStringUntil('\n');
Serial.println("Good to meet you "+ s_userName);
}
}
Advanced Examples
a) Create an additional serial data channel to the PC
b) Serial link between two devices
CAN2SER library compatibility
In the examples above there is no difference to any serial interface of the Arduino environment with one exception. The CAN bus transmits up to 8 bytes per message and handles the CAN traffic in a controlled timing. This is why the CAN2SER library needs to be called regularly to handle its traffic (for timed transmission of buffered messages, reception and distribution). This can be achieved by calling yield() or delay(); and happens with every loop() iteration. So the developer should avoid constructs that leads to blockings or longer wait states. e.g.:
while(1);don't while(1) { yield(); } do
Further information
dice users manual – details about device addressing schemata
Bootloader settings library – work with CAN bus and addressing settings
SAMC21_CAN library – working with the CAN bus
Device addressing scheme – inter-node communication background
CAN2SER Protocol – informative protocol explanation
CAN2SER Handshaking – background to the usage of handshakes
CAN2SER class
See Serial in the Arduino language reference for standard serial functions like println(), readUntil, read(), write() etc.
The following provides information about the overloaded functions and extensions:
Methodes:
- CAN2SER()
- begin()
- end()
- bool
- available()
- availableForWrite()
- peek()
- print()
- println()
- read()
- write()
- flush()
- i_flush()
- readBytes()
- readBytesUntil()
- readString()
- readStringUntil()
- setTimeout()
- set_handshake()
- set_send_interval()
- u8_set_ident_data()
- u8_request_peers_name_ident()
- s_get_peers_DevName()
- s_get_peers_ident_data()
- toString()
Properties: