Class CbusMessage


  • public class CbusMessage
    extends java.lang.Object
    Class to allow use of CBUS concepts to access the underlying can message.

    Methods that take a CanMessage or CanReply as argument:

    • CanMessage - Can Frame being sent by JMRI
    • CanReply - Can Frame being received by JMRI
    https://github.com/MERG-DEV/CBUSlib.
    • Method Detail

      • opcRangeToStl

        public static CanReply opcRangeToStl​(CanReply original)
        Return a CanReply for use in sensors, turnouts + light If a response event, set to normal event In future, this may also translate extended messages down to normal messages.
        Parameters:
        original - CanReply to be coverted to normal opc
        Returns:
        new CanReply perhaps converted from response OPC to normal OPC.
      • getOpcode

        public static int getOpcode​(AbstractMessage am)
        Get the Op Code from the CanMessage
        Parameters:
        am - CanMessage or CanReply
        Returns:
        OPC of the message
      • getDataLength

        public static int getDataLength​(AbstractMessage am)
        Get the Data Length from the CanMessage
        Parameters:
        am - CanMessage or CanReply
        Returns:
        the message data length
      • getNodeNumber

        public static int getNodeNumber​(AbstractMessage am)
        Get the Node Number from a CanFrame Event
        Parameters:
        am - CanMessage or CanReply
        Returns:
        the node number if not a short event
      • getEvent

        public static int getEvent​(AbstractMessage m)
        Get the Event Number from a CBUS Event
        Parameters:
        m - CanMessage or CanReply
        Returns:
        the message event ( device ) number, else -1 if not an event.
      • getEventType

        public static int getEventType​(AbstractMessage am)
        Get the Event Type ( on or off ) from a CanFrame
        Parameters:
        am - CanFrame or CanReply
        Returns:
        CbusConstant EVENT_ON or EVENT_OFF
      • isEvent

        public static boolean isEvent​(AbstractMessage am)
        Tests if a CanMessage or CanReply is an Event. Performs Extended and RTR check. Adheres to cbus spec, ie on off responses to an AREQ are events.
        Parameters:
        am - CanMessage or CanReply
        Returns:
        True if event, else False.
      • isShort

        public static boolean isShort​(AbstractMessage am)
        Tests if CanFrame is a short event
        Parameters:
        am - CanReply or CanMessage
        Returns:
        true if Short Event, else false
      • setId

        public static void setId​(AbstractMessage am,
                                 int id)
                          throws java.lang.IllegalArgumentException
        Set the CAN ID within a CanMessage or CanReply Header
        Parameters:
        am - CanMessage or CanReply
        id - CAN ID
        Throws:
        java.lang.IllegalArgumentException
      • setPri

        public static void setPri​(AbstractMessage am,
                                  int pri)
                           throws java.lang.IllegalArgumentException
        Set the priority within a CanMessage or CanReply Header.
        Parameters:
        am - CanMessage or CanReply
        pri - Priority
        Throws:
        java.lang.IllegalArgumentException
      • toAddress

        public static java.lang.String toAddress​(AbstractMessage m)
        Returns string form of a CanMessage ( a Can Frame sent by JMRI ) Short / Long events converted to Sensor / Turnout / Light hardware address message priority not indicated
        Parameters:
        m - CanReply or CanMessage
        Returns:
        String of hardware address form
      • isRequestTrackOff

        public static boolean isRequestTrackOff​(CanMessage m)
        Checks if a CanMessage is requesting Track Power Off
        Parameters:
        m - Can Frame Message
        Returns:
        boolean
      • isRequestTrackOn

        public static boolean isRequestTrackOn​(CanMessage m)
        Checks if a CanMessage is requesting Track Power On
        Parameters:
        m - Can Frame Message
        Returns:
        True if outgoing track power on request
      • getId

        public static int getId​(AbstractMessage f)
                         throws java.lang.IllegalArgumentException
        Get the CAN ID within a CanReply or CanMessage Header
        Parameters:
        f - CanReply or CanMessage
        Returns:
        CAN ID of the outgoing message
        Throws:
        java.lang.IllegalArgumentException
      • getPri

        public static int getPri​(AbstractMessage r)
                          throws java.lang.IllegalArgumentException
        Get the priority from within the CanReply or CanMessage Header
        Parameters:
        r - CanReply or CanMessage
        Returns:
        Priority of the outgoing message
        Throws:
        java.lang.IllegalArgumentException
      • isTrackOff

        public static boolean isTrackOff​(CanReply m)
        Tests if CanReply is confirming Track Power Off.
        Parameters:
        m - CanReply
        Returns:
        True if is a Track Off notification
      • isTrackOn

        public static boolean isTrackOn​(CanReply m)
        Tests if CanReply is confirming Track Power On.
        Parameters:
        m - CanReply
        Returns:
        True if is a Track On notification
      • isArst

        public static boolean isArst​(CanReply m)
        Tests if CanReply is a System Reset
        Parameters:
        m - CanReply
        Returns:
        True if emergency Stop
      • getReadCV

        public static CanMessage getReadCV​(int cv,
                                           ProgrammingMode mode,
                                           int header)
        CBUS programmer commands
        Parameters:
        cv - CV to read
        mode - Programming Mode
        header - CAN ID
        Returns:
        CanMessage ready to send
      • getVerifyCV

        public static CanMessage getVerifyCV​(int cv,
                                             ProgrammingMode mode,
                                             int startVal,
                                             int header)
        CBUS programmer commands CBUS VCVS works like a QCVS read but the programmer will first check if the CV contents are equal to the startVal. This can speed up CV reads by skipping reading of other values.
        Parameters:
        cv - CV to read
        mode - Programming Mode
        startVal - Hint of current CV value
        header - CAN ID
        Returns:
        CanMessage ready to send
      • getWriteCV

        public static CanMessage getWriteCV​(int cv,
                                            int val,
                                            ProgrammingMode mode,
                                            int header)
        Get a CanMessage to write a CV.
        Parameters:
        cv - Which CV, 0-65534
        val - New CV value, 0-255
        mode - Programming Mode
        header - CAN ID
        Returns:
        ready to send CanMessage
      • getOpsModeWriteCV

        public static CanMessage getOpsModeWriteCV​(int mAddress,
                                                   boolean mLongAddr,
                                                   int cv,
                                                   int val,
                                                   int header)
        CBUS Ops mode programmer commands
        Parameters:
        mAddress - Loco Address, non-DCC format
        mLongAddr - If Loco Address is a long address
        header - CAN ID
        val - New CV value
        cv - Which CV, 0-65534
        Returns:
        ready to send CanMessage
      • getRequestTrackOn

        public static CanMessage getRequestTrackOn​(int header)
        Get a CanMessage to send track power on
        Parameters:
        header - for connection CAN ID
        Returns:
        the CanMessage to send to request track power on
      • getRequestTrackOff

        public static CanMessage getRequestTrackOff​(int header)
        Get a CanMessage to send track power off
        Parameters:
        header - for connection CAN ID
        Returns:
        the CanMessage to send to request track power off
      • getBootEntry

        public static CanMessage getBootEntry​(int nn,
                                              int header)
        This is a strict CBUS message to put a node into boot mode.
        Parameters:
        nn - Node Number 1-65534
        header - CAN ID
        Returns:
        ready to send CanMessage
      • getBootNop

        public static CanMessage getBootNop​(int a,
                                            int header)
        Microchip AN247 format NOP message to set address.

        The CBUS bootloader uses extended ID frames

        Parameters:
        a - address
        header - CAN ID - overridden by call to setHeader
        Returns:
        ready to send CanMessage
      • getBootReset

        public static CanMessage getBootReset​(int header)
        Microchip AN247 format message to reset and enter normal mode.
        Parameters:
        header - CAN ID - overridden by call to setHeader
        Returns:
        ready to send CanMessage
      • getBootInitialise

        public static CanMessage getBootInitialise​(int a,
                                                   int header)
        Microchip AN247 format message to initialise the bootloader and set the start address.
        Parameters:
        a - start address
        header - CAN ID - overridden by call to setHeader
        Returns:
        ready to send CanMessage
      • getBootCheck

        public static CanMessage getBootCheck​(int c,
                                              int header)
        Microchip AN247 format message to send the checksum for comparison. At time of writing [6th Feb '20] The MERG bootloader doc is incorrect and shows the checksum as being byte swapped.
        Parameters:
        c - 0-65535 2's complement of sum of all program bytes sent
        header - CAN ID - overridden by call to setHeader
        Returns:
        ready to send CanMessage
      • getBootTest

        public static CanMessage getBootTest​(int header)
        Microchip AN247 format message to check if a module is in boot mode.
        Parameters:
        header - CAN ID - overridden by call to setHeader
        Returns:
        ready to send CanMessage
      • getBootDevId

        public static CanMessage getBootDevId​(int header)
        CBUS bootloader v1.0 format message to request device ID.
        Parameters:
        header - CAN ID - overridden by call to setHeader
        Returns:
        ready to send CanMessage
      • getBootId

        public static CanMessage getBootId​(int header)
        CBUS bootloader v1.0 format message to request bootloader ID.
        Parameters:
        header - CAN ID - overridden by call to setHeader
        Returns:
        ready to send CanMessage
      • getBootEnables

        public static CanMessage getBootEnables​(int enables,
                                                int header)
        CBUS bootloader v1.0 format message to set memory region write enables
        Parameters:
        enables - enable bits for memory regions
        header - CAN ID - overridden by call to setHeader
        Returns:
        ready to send CanMessage
      • getBootWriteData

        public static CanMessage getBootWriteData​(int[] d,
                                                  int header)
        Microchip AN247 format message to write 8 bytes of data
        Parameters:
        d - data array, 8 length, values 0-255
        header - CAN ID - overridden by call to setHeader
        Returns:
        ready to send CanMessage
      • getBootWriteData

        public static CanMessage getBootWriteData​(byte[] d,
                                                  int header)
        Microchip AN247 format message to write up to 8 bytes of data
        Parameters:
        d - data array, values 0-255
        header - CAN ID - overridden by call to setHeader
        Returns:
        ready to send CanMessage
      • isBootWriteData

        public static boolean isBootWriteData​(CanMessage m)
        Tests if a message is a bootloader data write
        Parameters:
        m - message
        Returns:
        true if the message is a bootloader data write
      • isBootError

        public static boolean isBootError​(CanReply r)
        Tests if incoming CanReply is a Boot Command Error.
        Parameters:
        r - CanReply
        Returns:
        True if is a Boot Command Error
      • isBootDataError

        public static boolean isBootDataError​(CanReply r)
        Tests if incoming CanReply is a Boot Data Error.
        Parameters:
        r - CanReply
        Returns:
        True if is a Boot Data Error
      • isBootOK

        public static boolean isBootOK​(CanReply r)
        Tests if incoming CanReply is a Boot Command OK.
        Parameters:
        r - CanReply
        Returns:
        True if is a Boot COmmand OK
      • isBootDataOK

        public static boolean isBootDataOK​(CanReply r)
        Tests if incoming CanReply is a Boot Data OK.
        Parameters:
        r - CanReply
        Returns:
        True if is a Boot Data OK
      • isBootOutOfRange

        public static boolean isBootOutOfRange​(CanReply r)
        Tests if incoming CanReply is a Boot Out of Range
        Parameters:
        r - CanReply
        Returns:
        True if is a Boot Data OK
      • isBootDataOutOfRange

        public static boolean isBootDataOutOfRange​(CanReply r)
        Tests if incoming CanReply is a Boot Out of Range
        Parameters:
        r - CanReply
        Returns:
        True if is a Boot Data OK
      • isBootConfirm

        public static boolean isBootConfirm​(CanReply r)
        Tests if incoming CanReply is a Boot Confirm.
        Parameters:
        r - CanReply
        Returns:
        True if is a Boot Confirm
      • isBootDevId

        public static boolean isBootDevId​(CanReply r)
        Tests if incoming CanReply is a device ID reply.
        Parameters:
        r - CanReply
        Returns:
        True if is a Boot Confirm
      • isBootId

        public static boolean isBootId​(CanReply r)
        Tests if incoming CanReply is a bootloader ID reply.
        Parameters:
        r - CanReply
        Returns:
        True if is a Boot Confirm