CAN Module

Overview of CAN functions

Group

Available functions

Configuration

Data Transfer

Introduction

CAN messages can be received and sent with the CAN module. All baud rates from 10kBit/s to 1Mbit/s are possible.

The module has a termination of 120 Ohm active at its connection by default. If required, this can also be deactivated via setTermination().

Receive Messages

To receive messages, 28 message slots are available for messages with a standard identifier (11 bit) and 8 message slots for messages with an extended identifier (29 bit).

The filters are run through in sequence from 0 - n. The first filter in the match accepts the message and transfers it to the corresponding message slot.

Alternative text

At the start of a test (default setting), both filters 0 are set so that they accept every message received and copy it to Msg slot 0. All other filters are deactivated by default. If you want to work with the filters, the filter with ID 0 must first be reconfigured accordingly using setStdFilter() / setExtFilter(). The filters should then be used in sequence from top to bottom.

The filters are configured in the classic way with a bit mask and an ID.

In order to be able to handle cyclical data, it makes sense to set the filter so that it reacts to a dedicated ID (set filter mask = filter ID). Received messages can then be read out via the method getMessage() on the corresponding slot. You always get the last message received. The timeSinceLastMsg data field in the CANMsg object can be used to read the period with which the messages were received in this slot (unit: microseconds). The global timestamp when the message was received can be read via the timestamp data field in the CANMsg object. This is a millisecond timestamp.

Send Messages

The sendMessage() or sendMessageObj() is available for sending individual messages.

To be able to send messages cyclically, with an exact period, there are two message slots which can be used via sendMessageObjCyclic(). The message transfer can be started and stopped at any time using startCyclicTransfer() and stopCyclicTransfer(). This function can be used, for example, to increase the bus load for a test. To change the data of the transferred messages, this can be done simply by calling the sendMessageObjCyclic() method again.

CAN Message Object

class ClassCAN.CANMsg
id: int

Identifier of the CAN message

timestamp: int

Global timestamp when the message was received (unit: microseconds)

timeSinceLastMsg: int

Time elapsed since the last message was received in the same slot (unit: microseconds). This field is useful for messages that are transmitted periodically. In this case you can see the time period of the message transmission.

extended: bool

Indicates whether it is an extended identifier.

RTR: bool

Indicates whether it is a remote transmission request.

data: tuple

tuple with the data of the message.

print()

For debugging purposes: prints all information about the message into the console window.

CAN Methods

class ClassCAN.ClassCAN(_address)
setBaudrate(baudrate: int) bool

With this method you can setup the baudrate for the CAN interface.

Parameters:

baudrate – Choose baudrate in Bit/s between 10.000Bit/s and 1.000.000Bit/s

Returns:

True if baudrate was set successfully, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
can = ext.getCAN()
can.setBaudrate(800000)     #set baudrate to 800kBit/s
sendStdMessage(identifier: int, data=(), RTR: bool = False) bool

Method to send a CAN Message with standard identifier (11 bit).

Parameters:
  • identifier – Identifier of the Message

  • data (int or tuple) – Datafield of the message. You can let this parameter empty or use an integer for single byte message or use a tuple of miximum 8 bytes for multiple bytes

  • RTR (bool) – Defines if the RTR (Remote Tranmission Request) Bit Filed should be set in the message.

Returns:

True if message was sent successfully, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
can = ext.getCAN()
can.setBaudrate(800000)              #set baudrate to 800kBit/s
can.sendStdMessage(0x600, (1,2,3))   #send 3 bytes with ID 0x600
can.sendStdMessage(10, 20)           #send 1 byte (value: 20) with ID 10
can.sendStdMessage(1, RTR=True)      #send empty Remote Transmission Request with ID 1
sendExtMessage(identifier: int, data=(), RTR: bool = False) bool

Method to send a CAN Message with extended identifier (29 bit).

Parameters:
  • identifier – Identifier of the Message

  • data (int or tuple) – Datafield of the message. You can let this parameter empty or use an integer for single byte message or use a tuple of miximum 8 bytes for multiple bytes

  • RTR (bool) – Defines if the RTR (Remote Tranmission Request) Bit Filed should be set in the message.

Returns:

True if message was sent successfully, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
can = ext.getCAN()
can.setBaudrate(800000)              #set baudrate to 800kBit/s
can.sendExtMessage(0x600, (1,2,3))   #send 3 bytes with ID 0x600
can.sendExtMessage(10, 20)           #send 1 byte (value: 20) with ID 10
can.sendExtMessage(1, RTR=True)      #send empty Remote Transmission Request with ID 1
sendMessageObj(msg) bool

Method to send a CAN Message.

Parameters:

msg (CANMsg) – CAN Message to be sent.

Returns:

True if message was sent successfully, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
can = ext.getCAN()
can.setBaudrate(500000)           #set baudrate to 500kBit/s

msg = CANMsg()
msg.id = 0x100
msg.data = (1,2,3,4)

can.sendMessageObj(msg)
sendMessageObjCyclic(slot: int, msg, period_us: int, autostart=True) bool

Send a CAN Message periodically. There are two Messages which can be sent periodically (slot 0 and slot 1).

Keep in mind: The duration of message transfer depends on the baudrate and data length. If you set a period which is shorter than a message transfer takes, you will lost messages.

Parameters:
  • slot (int) – Slot in which the message should be set (0 or 1)

  • msg (CANMsg) – CAN Message to be sent.

  • period_us (int) – period (unit microseconds) how often the message should be sent. (allowed range: 150us - 30s)

Returns:

True if message was accepted, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
can = ext.getCAN()
can.setBaudrate(500000)           #set baudrate to 500kBit/s

msg = CANMsg()
msg.id = 0x100
msg.data = (1,2,3,4)

can.sendMessageObjCyclic(0, msg, 10000)     #send the message each 10ms
startCyclicTransfer(slot: int, period_us: int = None) bool

Starts the cyclic transfer of the message which was previosly defined with the sendMessageObjCyclic method.

Parameters:
  • slot (int) – Slot in which the message should be set (0 or 1)

  • period_us (int) – Optionally you can change the period of transfer. If this parameter isnt set, the transfer will happen with the period, set in the sendMessageObjCyclic method.

Returns:

True if the transfer was started, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
can = ext.getCAN()
can.setBaudrate(500000)           #set baudrate to 500kBit/s

msg = CANMsg()
msg.id = 0x100
msg.data = (1,2,3,4)

can.sendMessageObjCyclic(0, msg, 10000, False)     #send the message each 10ms, but do not start yet

await waitForHigh(IN5, 5000)    #sync messagetransfer with an external signal
can.startCyclicTransfer(0)
stopCyclicTransfer(slot: int) bool

Stops the cyclic transfer of the message which was previosly defined with the sendMessageObjCyclic method.

Parameters:

slot (int) – Slot in which the message should be set (0 or 1)

Returns:

True if the transfer was started, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
can = ext.getCAN()
can.setBaudrate(500000)           #set baudrate to 500kBit/s

msg = CANMsg()
msg.id = 0x100
msg.data = (1,2,3,4)

can.sendMessageObjCyclic(0, msg, 10000)     #send the message each 10ms, but do not start yet

await sleep_ms(500)

can.stopCyclicTransfer(0)
setTermination(termination: bool) bool

Enables or Disables the 120 Ohm termination resistor on the CAN line.

Parameters:

termination (bool) – True to enable the termination, False to disable the termination resistor.

Returns:

True if termination was set successfully, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
can = ext.getCAN()
can.setTermination(False)
setStdFilter(slotId: int, filterMask: int, filterId: int) bool

Here you can configure the filter slot for CAN Messages with standard identifier (see description in the Introduction)

Parameters:
  • slotId (int) – ID of the Filter: 0-27.

  • filterMask (int) – Bitmask for the Filter (11 bit used).

  • filterId (int) – ID which should be filtered.

Returns:

True if filter was set successfully

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
can = ext.getCAN()
can.setStdFilter(0, 0x7FF, 0x620)
setExtFilter(slotId: int, filterMask: int, filterId: int) bool

Here you can configure the filter slot for CAN Messages with extended identifier (see description in the Introduction)

Parameters:
  • slotId (int) – ID of the Filter: 0-7.

  • filterMask (int) – Bitmask for the Filter (29 bit used).

  • filterId (int) – ID which should be filtered.

Returns:

True if filter was set successfully

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
can = ext.getCAN()
can.setExtFilter(0, 0x7FF, 0x620)
getMessage(slotId: int, extended: bool = False)

Returns the last received CAN message of a message slot.

Parameters:
  • slotId (int) – index of the message slot of which you want to read the message.

  • extended (bool) – defines if yoh want a message from the buffer of extended Identifier (True) oder standard identifier (False).

Returns:

last received CAN message.

Return type:

CANMsg

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
can = ext.getCAN()
msg = can.getMessage(0)
msg.print()
getRxCnt(slotId: int, extended: bool = False)

Returns the total amount of CAN messages received for the given slot.

Parameters:
  • slotId (int) – index of the message slot of which you want to read the message.

  • extended (bool) – defines if yoh want a message from the buffer of extended Identifier (True) oder standard identifier (False).

Returns:

amount of Messages

Return type:

int

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
can = ext.getCAN()

can.setStdFilter(0, 0x7FF, 0x620)   #listen on message with ID=0x620

await sleep_ms(100)

amountReceived = can.getRxCnt(0)

report.checkValueMin("Some messages with ID 0x620 received?", amountReceived, 10)