J1587 sample

This page contains a sample of the J1587 Python API on Digi devices.

            ## The J1587 support native to the Digi product is limited to the
## interpretation of the raw J1708 messages into a J1587 PID that contains the
## discrete samples from the J1708 message.

## No J1587 communication is explicitly provided.  All messages are converted
## back into J1708 messages of the same meaning then sent across the J1708
## communication bus.

## Many of the steps required to make use of the J1587 PID require the same
## setup as the J1708 bus, as it is built upon that.

## 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

  print "Converting to J1587 PID message"

  ## The payload is used to instantiate the J1587 PID.   The PID implements
  ## Python dictionary functions, so iteration over the keys or direct
  ## key look up is possible.

  pid = digij1708.J1587_PIDdict(payload)
  for key in pid:
    print "Pid: %s = %s" %(key, pid[key])

## Register the callback on the J1708 bus
handle.register_callback(callback_1, 'foo')
print "Type 'quit' to exit, or type a J1587 payload message then hit enter:\n"
  while 1:
  input = raw_input()
  if input.lower() in ['q', 'qu', 'qui', 'quit']:
    break

  input = input.strip()
  ## create a fake message J1587 compatible
  if len(input) == 0:
    msg = struct.pack('=2B', 45, 65)

  ## Use it to create a J1587 PID.
  pid = digij1708.J1587_PIDdict(msg)

  ## Print the discrete samples within the PID
  print "Sending pid with values:"
  for key in pid:
    print "Pid: %s = %s" %(key, ord(pid[key]))

  ## Convert the message into a J1708 message, and send it out the J1708 handle
  msg = pid.J1708_payload()
  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')