<h2>Name</h2>

<b>can</b> - Tcl interface to Linux SocketCAN

<h2>Synopsis</h2>

<tt>package require Tcl 8.6</tt><br>
<tt>package require tclcan</tt><br>
<tt>can bitrate <i>ifname ?rate?
<tt>can bittiming
<tt>can bitttiming_const
<tt>can berr
<tt>can clock
<tt>can close
<tt>can ctrlmode <i>ifname ?mode
<tt>can devstat
<tt>can dump
<tt>can
<tt>can open
<tt>can read
<tt>can restart
<tt>can restart_ms <i>ifname
<tt>can start
<tt>can state
<tt>can stop
<tt>can write <i>chan canid data

<h2>Description</h2>

This  package provides Tcl support for Linux SocketCAN raw <b>AF_CAN</b> sockets. The package implements a new channel type and a  Tcl  command  to
perform operations on these channels. The standard <tt>gets</tt>, <tt>puts</tt>, and <tt>read</tt> Tcl commands are not supported, but <tt>close</tt>, <tt>fconfigure</tt>,  and  <tt>fileevent</tt> are available as for normal channels, e.g. sockets. When the <b>libsocketcan</b> shared library is available, various subcommands  can  be  used  to manage CAN interfaces, too.

<h2>Commands</h2>

<tt>can bitrate <i>ifname ?rate?

    Gets or sets the bitrate (and sets optional sample point to on the CAN interface

<tt>can bittiming

    Retrieves the current bit timing of the CAN interface For details refer to

<tt>can bittiming_const

    Retrieves configuration on bit timing of the CAN interface For details refer to

<tt>can berr

    Retrieves error counters of the CAN interface The result is a dictionary made up of the keys <tt>txerr</tt> and <tt>rxerr</tt> with respective integer error counters.

<tt>can clock

    Retrieves the clock frequency of the CAN interface For details refer to

<tt>can close

    Closes the channel which was formerly obtained by <tt>can open</tt>. This is equivalent to invoking the <tt>close</tt> command  with as parameter.

<tt>can ctrlmode <i>ifname ?mode

    Gets or sets modes on the CAN interface If no is specified, the current active modes are returned as a list. Otherwise, must be one or more words of <tt>loopback</tt>, <tt>listenonly</tt>, <tt>3_samples</tt>, <tt>one_shot</tt>, <tt>berr_reporting</tt>,  <tt>fd</tt>, and <tt>presume_ack</tt>. In order to turn a mode off, prefix the word with a minus sign. Likewise, to turn it on, a plus sign may be optionally used as prefix.

<tt>can devstats

    Retrieves device statistics as a dictionary. For details refer to and

<tt>can dump

    Reads a CAN message off and returns a formatted representation of it as a list. The list is empty if no CAN message was pending on Otherwise the list has five or six elements which are:  1. an  integer time stamp equivalent to <tt>clock microseconds</tt>, 2. the interface index (see <tt>can interfaces</tt>), 3. the  CAN identifier as a hexadecimal string with 0x prefix, 4. a frame format tag of the CAN message as <tt>EFF</tt> (extended frame format) or <tt>SFF</tt> (standard frame format) optionally followed by <tt>|RTR</tt> (remote transmission request) or <tt>|ERR</tt> (error frame), 5. the data length as a decimal number, and optionally 6. the data portion of the CAN message as hexadecimal dump without blanks and prefix.

<tt>can interfaces</tt>

    Returns a list of CAN network interface names and indices suitable for <tt>can open</tt>, <tt>can read</tt>, <tt>can write</tt> and link management subcommands.

<tt>can open

    Opens a channel (raw <b>AF_CAN</b> socket) on the given CAN interface If is specified as an empty string, the channel is  bound to all CAN interfaces. The command returns an identifier for the channel which is to be used in subsequent <tt>can read</tt> and <tt>can write</tt> commands.

<tt>can read

    Reads a CAN message off as a list. The list is empty if no CAN message was pending on Otherwise it is made up of four elements, 1. the CAN identifier as an integer number including flags as explained below, 2. the data portion of the CAN message as a byte array, 3. the interface index of the CAN interface the CAN message was received from, and 4. a boolean value indicating if more CAN messages can be read using <tt>can read</tt>.

<tt>can restart

    Performs a link restart on the CAN interface

<tt>can restart_ms <i>ifname

    Gets or sets the restart timer of the CAN interface must be specified as positive integer number of milliseconds.

<tt>can start

    Performs a link startup on the CAN interface

<tt>can state

    Retrieves the linmk state of the CAN interface The result is one of <tt>error_active</tt>, <tt> error_warning</tt>, <tt>error_passive</tt>, <tt>bus_off</tt>, <tt>stopped</tt>, <tt>sleeping</tt>, or <tt>unknown</tt>.

<tt>can stop

    Performs a link stop on the CAN interface

<tt>can write <i>chan canid data

    Writes a CAN message to is the  CAN identifier as integer number, a byte array of the data to be sent. The optional is the CAN interface index (see <tt>can interfaces</tt>) on which the message is to be sent. It is mandatory to specify when is bound to all interfaces, i.e. the interface name on <tt>can open</tt> was an empty string.

<h2>CAN Identifiers</h2>

The Linux SocketCAN interface defines special bits in CAN identifiers which are made up of the three most significant bits in a 32 bit integer: 0x80000000 for extended frame format (<tt>EFF</tt>), 0x40000000 for remote transmission request (<tt>RTR</tt>), and 0x20000000 for error frames (<tt>ERR</tt>). The lower 29 (for <tt>EFF</tt>) or 11 (for <tt>SFF</tt>) bits make up the CAN identifier. In order to retrieve the real CAN identifier of  a received CAN message from <tt>can read</tt> a binary and with the masks 0x1FFFFFFF or 0x7FF must be carried out. In order to send an <tt>RTR</tt> message, the CAN identifier must be binary or-ed with 0x40000000 for <tt>can write</tt>. In order to send a 29 bit CAN identifier it must be or-ed with 0x80000000.

<h2>Channel Options</h2>

The following list describes the additional channel options of CAN channels.

<tt>-error</tt>

    The last system error message on the channel. This is a read-only option.

<tt>-filter

    Message filters applied on reception. must be made up of an even number of integers specifying CAN identifiers and masks. The default is no filtering, expressed as two zero values. Up to 16 filters can be specified. For details refer to


<tt>-loopback

    Messages sent are looped back on the local system  when  enabled (on by default).

<tt>-ownmsgs

    Messages sent are received on the same channel when enabled (off by default).

<h2>Link Management</h2>

The link management subcommands <tt>bitrate</tt>, <tt>bittiming</tt>, <tt>bittiming_const</tt>, <tt>berr</tt>, <tt>clock</tt>, <tt>ctlrmode</tt>, <tt>devstat</tt>, <tt>restart</tt>, <tt>restart_ms</tt>, <tt>start</tt>, <tt>state</tt>, and <tt>stop</tt> depend on an installed <b>libsocketcan</b> shared library for proper operation. Otherwise they report "function not implemented". All changes of link state by these commands usually require administrative rights. Either the calling process must have super user privileges or the <b>CAP_NET_ADMIN</b> capability must be effective. Furthermore, retrieving link  information  depends on CAN driver support. Usually, the virtual CAN driver <b>vcan</b> and drivers attached through a serial line discipline (using the <b>slcan_attach</b> or <b>slcand</b> programs) only provide rudimentary link state information, YMMV.