Class MakePacket

java.lang.Object
jmri.jmrix.direct.MakePacket

public class MakePacket extends Object
Provide utilities for coding/decoding NMRA S&RP DCC packets into sequences to send through a standard serial port.

This is strongly based on the makepckt.c file from the PacketScript 1.1. package of Kenneth Rice. The original header comment from that file follows here.


 Some Useful Background information

      startbit: 1
      stopbit : 1
      databits: 8
      baudrate: 19200

      ==> one serial bit takes 52.08 usec. (at 19200 baud)

      ==> NMRA-1-Bit: 01         (52 usec low and 52 usec high)
          NMRA-0-Bit: 0011       (at least 100 usec low and high)
           note we are allowed to stretch NMRA-0 e.g. 000111,
           00001111, 000001111
          are all valid NMRA-0 representations

      serial stream (only start/stop bits):

      0_______10_______10_______10_______10_______10_______10___ ...

      problem: how to place the NMRA-0- and NMRA-1-Bits in the
    serial stream

     examples:

      0          0xF0     _____-----
      00         0xC6     __--___---
      01         0x78     ____----_-
      10         0xE1     _-____----
      001        0x66     __--__--_-
      010        0x96     __--_-__--
      011        0x5C     ___---_-_-
      100        0x99     _-__--__--
      101        0x71     _-___---_-
      110        0xC5     _-_-___---
      0111       0x56     __--_-_-_-
      1011       0x59     _-__--_-_-
      1101       0x65     _-_-__--_-
      1110       0x95     _-_-_-__--
      11111      0x55     _-_-_-_-_-
                          ^        ^
                          start-   stop-
                          bit      bit

 Limitation
 If we ever need to generate a pattern of four '1's followed by a '0' and
 land it on a the start of a byte boundary - Sorry - it can't be done !!


 makepckt.c

 Send an nmra packet out the serial port in such a way that the signal can
 just be amplified and put on the track.

 Copyright 1993 Kenneth Rice

 You may freely distribute this source code, and use all or part of
 this source code in all software including commercial, freeware,
 shareware and private applications.

 Please report bugs, fixes, etc to me at:
 kenr@xis.xerox.com
 or
 73577,1653 (compuserve)

 Created 02/08/93
   03/05/93 Works for all 3 byte packets. Still errors for 4 byte.
   07/01/93 Renamed to makepckt.c to be nice to dos users.
   10/23/93 Added backtracking and max length.
 
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static int[]
    bitStreamToSerialBytes(int[] inputBitStream)
    Generate the serial bytes from the bit stream.
    static int[]
    createStream(byte[] packet)
    Take in the packet as an array of Bytes and convert them into NMRA'1','0' representation, in preparation to be sent over a serial link.
    (package private) static boolean
    readFirstChild(int[] bs, int offset, int validBits, jmri.jmrix.direct.MakePacket.Node thisNode)
    Find the first largest (ie longest length) child at this Node.
    (package private) static boolean
    readNextChild(jmri.jmrix.direct.MakePacket.Node thisNode)
    Find the next largest (ie longest length) child at this Node.
    static boolean
    setPreambleLength(int preambleLen)
    Set the Preamble Length - Default is 15 NRMA '1's Every NMRA packet decoded starts with a preamble Service mode requires longer preambles Thus this public function allowing user to define the length of desired preamble

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • setPreambleLength

      public static boolean setPreambleLength(int preambleLen)
      Set the Preamble Length - Default is 15 NRMA '1's Every NMRA packet decoded starts with a preamble Service mode requires longer preambles Thus this public function allowing user to define the length of desired preamble
      Parameters:
      preambleLen - int
      Returns:
      boolean true if preamble is a multiple of 5, otherwise fails and returns false
    • createStream

      public static int[] createStream(byte[] packet)
      Take in the packet as an array of Bytes and convert them into NMRA'1','0' representation, in preparation to be sent over a serial link.
      Parameters:
      packet - byte[] NRMA packet in a array of bytes
      Returns:
      int[] first byte is length - 0 length indicates failed to do
    • bitStreamToSerialBytes

      static int[] bitStreamToSerialBytes(int[] inputBitStream)
      Generate the serial bytes from the bit stream.

      Basically this is a depth first, prune largest tree search, always going down the subtree that uses the most bits for the next byte. If we get an error, backtrack up the tree until we reach a Node that we have not completely traversed all the subtrees for and try going down the subtree that uses the second most bits. Keep going until we finish converting the packet or run out of things to try.

      This is not guaranteed to find the shortest serial stream for a given packet, but it is guaranteed to find a stream if one exists. Also, it usually does come up with the shortest packet

      Parameters:
      inputBitStream - sequence of binary bits to send as DCC waveform
      Returns:
      sequence of long/short codes to send, coded as voltage high 1, voltage low 0 values as aynch bytes to send at the DCC rate
    • readNextChild

      static boolean readNextChild(jmri.jmrix.direct.MakePacket.Node thisNode)
      Find the next largest (ie longest length) child at this Node.
      Parameters:
      thisNode - (INPUT/OUTPUT) determine if there is another child if so update Node with ie the Bit pattern and its associated length
      Returns:
      false if one doesn't exist otherwise returns true
    • readFirstChild

      static boolean readFirstChild(int[] bs, int offset, int validBits, jmri.jmrix.direct.MakePacket.Node thisNode)
      Find the first largest (ie longest length) child at this Node.
      Parameters:
      bs - (INPUT) Bit stream array
      offset - Offset in to buffer
      validBits - (INPUT) number of valid bits in the bit stream
      thisNode - (OUTPUT) where to put largest child found ie the Bit pattern and its associated length
      Returns:
      false if one doesn't exist otherwise returns true