CAN bus Sample

From Digi Developer

Jump to: navigation, search

This page contains a sample that demonstrates the usage of the CAN bus using the python API on the Digi devices.

## Import the CAN module
from digicanbus import *
import struct, time, sys
 
speed = 125000
if len(sys.argv) >= 2:
  speed = int(sys.argv[1])
 
## The digicanbus module has CANHandle().  A function that returns a handle to 
## the current CAN bus.
 
## Specify the CAN bus number.
print "Getting handle to CAN bus 0"
handle = CANHandle(0)
 
## First we configure the bus to the speed specified
print "Configuring for %d bps" %speed 
handle.configure(speed)
 
## We create a simple callback function to use with the CAN filters.
## The callback must have the following parameters defined:
## width: specifies either 11 or 29 bit message
## identifier: The identifier that was matched
## remote_frame: Boolean indicating a remote frame (RTR)
## payload: 0-8 bytes of payload for the message
## return_arg:  Argument specified when creating the filter
 
## We will use the return_arg parameter to determine which filter triggered the 
## callback.
 
def callback_1(width, identifier, remote_frame, payload, return_arg):
  print "\ncallback_1 function was called"
  print '11 or 29 bit: ', width, ' bit'
  print 'Identifier matched: ', identifier
  print 'Is remote frame: ', remote_frame
  print 'Payload: ', struct.unpack('%dB'%len(payload), payload)  
  print 'Return arg: ', return_arg
 
## We create a tuple, which contains all the information needed for the filter
## Width:  11 or 29 bit message
## Identifier: Which CAN identifier will be selected
## Mask:  Which bits of the identifier matter when matching
## callback function:  The function will be called when something is matched
## return_arg:  Argument passed to the call back when
 
## Note: Multiple filters can use the same callback function
 
## Below we are exploring different scenarios with the filters.
 
print "Defining filters:"
## Filter 1 will only trigger on messages with the 0x700 identifier.
## This is done by saying that the bits 0x700 must be on, and all bits will be
## measured in the mask (0x7FF).
 
filter_1 = (11, 0x700, 0x7FF, callback_1, 'filter_1')
print "Filter 1: ", filter_1
 
## Filter 2 will trigger on messages between 0x700 and 0x7FF.
## This is done by saying that the bits 0x700 must be on, but only the 0x700
## bits will be measured in the mask.
 
filter_2 = (11, 0x700, 0x700, callback_1, 'filter_2')
print "Filter 2: ", filter_2
 
## Filter 3 will trigger on all 29 bit messages.
## This is done by setting the width to 29, and using values 0x0 for identifier
## and mask. 
 
filter_3 = (29, 0x0, 0x0, callback_1, 'filter_3')
print "Filter 3: ", filter_3
 
## Register the filters on the CAN bus
print "Registering filters..."
handle.register_filter(*filter_1)
handle.register_filter(*filter_2)
handle.register_filter(*filter_3)
 
counter = 0
print "Hit enter to send a CAN message, type 'quit' to exit"
while raw_input().lower() != 'quit':
  counter += 1
  msg = (11, counter % 0x500, False, str(counter % 99999))
  handle.send(*msg)
 
## Unregister the filters created using the stored tuples
print "Unregistering filters"
handle.unregister_filter(*filter_1)
handle.unregister_filter(*filter_2)
handle.unregister_filter(*filter_3)
Personal tools
Wiki Editing