K300/K700 CANBUS FAQ

This article provides details on using the K300 and K700 microcontroller, with some examples of setup and usage. This does not apply to other units in the Karbon series (i.e. K410, K800, etc) and specific details can be found on the specific product pages.

Getting started

To get started with the basics of communication with the Karbon K300 and K700 units, the command line utility can be downloaded for Linux and Windows systems. 
This utility will allow you to work the the Microcontroller which is responsible for the native CAN Bus functionality on the unit. No additional drivers need to be installed to work with the native CAN bus on the system. Although additional python libraries will be needed if you wish to develop python scripts to utilize the native CAN Bus

Connectivity

For this tutorial there will be a K300 running Windows 10 IoT and a K300 running Ubuntu 20.04 with a cable connecting the two units via the CAN Bus port located on the side of the chassis.

The K300 and K700’s internal CAN device should be attached directly to a CAN Bus, but features no internal termination. Because of this, for proper operation, it may be required to implement an external termination resistor. This resistor should match the nominal impedance of the cable; typically 120Ω, which complies with ISO 11898.

Using the Microcontroller CLI utility to send CAN Messages on Linux

After downloading and extracting the Windows directory from the Microcontroller Utility, open command prompt and navigate to the directory containing the Karbon.exe CLI utility. From here you can use the command karbon.exe raw devices to query which Virtual COM port in the OS is handling CAN messaging.

We can then use PuTTY to open the CAN interface port and now the PuTTY terminal is listening to the CAN interface of the Microcontroller.

On the Linux system, we can extract the Karbon MCU utility’s Linux directory and navigate to it. Running the Karbon utility with sudo, we can send normal CAN Frames over the systems CAN Bus using the sudo ./karbon op can-message -i 123 -d 11223344 command

The Windows machine’s PuTTY Window will receive and present the sent CAN message

Using the Microcontroller CLI utility to send CAN Messages on Windows

The Linux Microcontroller CLI utility can report which /dev/ address is handling CAN communication with the command sudo ./karbon raw devices

The CAN interface can be opened with an elevated PuTTY command to listen for CAN messages.

On the Windows system using an elevated command prompt, CAN messages can be sent utilizing the karbon.exe op can-message -i 123 -d 11223344 command.

The CAN message will show up on the Linux system’s PuTTY window


Microcontroller Utility

The can subcommand supports interfacing with and controlling the onboard CAN device. This includes sending and receiving CAN messages and setting the baudrate of the CAN Bus. This module requires the CAN device be configured standard operational mode. The mode of the CAN device is controlled by the can-mode configuration parameter.

The CAN device does not support internal termination, and should be externally terminated when connected to a CAN bus.

Using SLCAN

The K300 and K700 platforms also support processing CAN commands using an slcan compliant parser. To get up and running with slcan on a K300 or K700 running Ubuntu, start by installing can-utils:

$ sudo apt install can-utils

Then detect and set up the system, for example:

slcan_setup.sh

#! /bin/bash

# ASCII Command vs CAN Bitrate
# s0 --- s1 --- s2 --- s3 --- s4 --- s5 --- s6 --- s7 --- s8
# 10     20     50     100    125    250    500    800    1000  Kbits/s
BAUD=8

# Name of slcan device to attach
DEV=can0


# Detect correct port for device interfaces
TERM_PORT=$(ls -l /dev/serial/by-id/ | grep 1fc9_00a3 | sed 's/.*\///g' | awk '{if(NR==2) print $0}')
CANB_PORT=$(ls -l /dev/serial/by-id/ | grep 1fc9_00a3 | sed 's/.*\///g' | awk '{if(NR==1) print $0}')

# Start or stop the service
while getopts ":ks" opt; do
    case $opt in
        k)
            # Stop the SLCAN service and turn off the can device
            echo "Shutting down can interface..."
            sudo ifconfig $DEV down 2> /dev/null
            sudo pkill slcand

            sudo slcand -c /dev/"$CANB_PORT"
            sudo pkill slcand
            ;;
        s)
            # Open the can device, and start the slcan service.
            echo "Terminal interface on: $TERM_PORT"
            echo "CAN Bus  interface on: $CANB_PORT"

            # Set the mode to slcan
            echo -ne "set can-mode slcan" > /dev/$TERM_PORT

            echo "Attaching slcan device..."

            # Attach to the port with slcand
            sudo slcand -s$BAUD -o /dev/"$CANB_PORT" $DEV

            # Give interface time to come up
            sleep .25

            # Enable the inteface
            sudo ifconfig $DEV up
            sudo ifconfig $DEV txqueuelen 1000

            echo "Interface is set up."
            ;;
        \?)
            echo "Invalid option: -$OPTARG" >&2
            exit 1
            ;;
        :)
            echo "Option -$OPTARG requires an argument." >&2
            exit 1
            ;;
    esac
done

The system is now ready to send and receive can messages using can-utils:

$ cansend can0 123#11223344
$ candump can0

By default, the can subcommand will first check the operational mode before sending a command. Because this is slower than just sending or receiving can messages, and requires access to the terminal port, it can disabled with the --nocheck flag.

Don’t check can mode

$ karbon can -n <COMMAND>

Additionally all can operations support setting the baudrate before processing the command. This requires access to the terminal serial interface, and does not change the baudrate that will be loaded at system boot. If no baudrate is specified, the last transmitted baudrate will continue to be used.

Specify can baudrate

$ karbon can -b 500 <COMMAND>

The baudrate is specified in Kbits/s, and supports setting rates from 100-1000.

Can Commands

The Microcontroller CLI supports sending and receiving can messages with two commands: send and dump.

Send

The send command supports sending standard, extended, and remote frames. All messages require an id. Standard and extended frames also require data. Standard frames are sent by default, while extended and remote frames can be sent by using the corresponding flag.

Usage

$ karbon can send [FLAGS] [OPTIONS] <identifier> [data]
ArgumentDescriptionType
dataThe data to be transmitted, represented as an up-to eight byte hex string. Passed data will be checked for validity before transmission. This argument is required, except when sending remote request frames (-r).Example$ karbon can send 123 1122334455667788arg
identifierThe ‘id’ of the can frame. Should be specified as hex string from 1-7FF for standard frames, and 1-1FFFFFFF for extended frames.Lower numbered frames will have priority during message arbitration, and by default, the id will be interpreted as a standard frame id.Validity checking will be performed against the hex string and id range.arg
extendedSend message in extended frame format. Supports larger id range.Example$ karbon can send -e 999 11223344flag
lengthLength of data to be transmitted. If not specified, the data length will be automatically determined.Example$ karbon can send -l 3 123 112233opt
remoteSend a remote frame. When specified, data is not required.Example$ karbon can send -r 123flag

Validity Checking

The send command’s id and data arguments are specified as strings, and parsed into hexadecimal. Because of this, validity checking is performed against the specified ID and data, before the command is passed on to the microcontroller. In psuedo-code the checks performed are as follows:

Additionally, it is acceptable to specify id or data with a leading 0x, which will simply be trimmed before data validation is performed.

Dump

The dump command supports reporting all received CAN messages. Messages may be filtered by id, and the time between frames can be recorded.

By default, the messages will be formatted and printed to stdout, but the data can also be reported in csv format and/or saved to file.

For instance, to print all messages to the terminal along with their time delta, run:

$ karbon can dump -d

Or to log only messages with a matching id to messages.csv, run:

$ karbon can dump -d -c -f 123 7FF -- messages.csv

Usage

karbon can dump [FLAGS] [OPTIONS] [--] [output]
ArgumentDescriptionType
csvFormat the output as a csv. Does not redirect output from stdout. Header will be included as id,len,data or as id,len,data,delta.Example$ karbon can dump -c id,len,data 123,4,11223344flag
deltaLog the time delta between frames. Times will be reported as floating point seconds; 1.001 is 1001 milliseconds.Time delta is calculated at the host, and should not be considered a perfect representation of time between frames.Example$ karbon can dump -d id [l] data delta 123 [4] 11223344 2.907689 7FF [4] ABCD 3.386151 123 [4] 11223344 6.232194 00000999 [8] 1122334455667788 0.000519flag
filterA list of CAN frame identifiers to match received messages against. All other messages will be discarded.ImportantExtended IDs should be specified in their full form, including leading zeroes. E.g. 00000999 not 999.Example$ karbon can dump -f 123 1FFFFFFF 00000999 3AAopt
outputThe output location for printing CAN frames. Defaults to stdout when not specified.arg
Updated on June 26, 2023

Was this article helpful?

Related Articles

Keep In Touch.
Subscribe today and we’ll send the latest product and content updates right to your inbox.
Subscribe