J1708 Sample

From Digi Developer

Jump to: navigation, search

This page contains a sample of how to use the J1708 Python API on a Digi device.

## Import the J1708 python API
import digij1708
import struct
 
##Create a handle to the J1708 bus through the class J1708Handle()
##The class takes 1 parameter, the bus number to get a handle to.
print "Getting a handle to the J1708 Bus"
handle = digij1708.J1708Handle(0)
 
##Configure the bus to a particular mid. in this case 0x48
handle.configure(0x48)
 
##Create a callback function.  This function requires three parameters:
##  mid, payload, and arg
 
## mid:  The message ID
## payload: The payload of the message
## arg:  An arbitrary parameter defined when setting the callback.
 
def callback_1(mid, payload, arg):  
  print "\nMid: ", mid
  print "Payload: ", payload
  print "Arg: ", arg
 
## Register the callback on the J1708 bus
handle.register_callback(callback_1, 'foo')
print "Type 'quit' to exit, or type a message in to send then hit enter:\n"
while 1:
  input = raw_input()
  if input.lower() in ['q', 'qu', 'qui', 'quit']:
    break
 
  input = input.strip()
 
  ## If no input, create a fake message  
  if len(input) == 0:
    msg = struct.pack('=2B', 45, 12)
 
  ## If input, take up to 19 characters of it
  else:
    if len(input) > 19:
      msg = input[:19]
    else:
      msg = input
 
  ## Send the message at priority 1
  try:
    handle.send(1, msg)
  except Exception, e:
    print e
 
## Unregister the call back, must use exact same input as the register call
print "Unregistering callback"
handle.unregister_callback(callback_1, 'foo')
Personal tools
Wiki Editing