jmri.jmrix.loconet
Class LocoNetMessage

java.lang.Object
  extended by jmri.jmrix.loconet.LocoNetMessage
All Implemented Interfaces:
Serializable

public class LocoNetMessage
extends Object
implements Serializable

Represents a single command or response on the LocoNet.

Content is represented with ints to avoid the problems with sign-extension that bytes have, and because a Java char is actually a variable number of bytes in Unicode.

Note that this class does not manage the upper bit of the message. By convention, most LocoNet messages have the upper bit set on the first byte, and on no other byte; but not all of them do, and that must be managed elsewhere.

Note that many specific message types are created elsewhere. In general, if more than one tool will need to use a particular format, it's useful to refactor it to here.

Some of the message formats used in this class are Copyright Digitrax, Inc. and used with permission as part of the JMRI project. That permission does not extend to uses in other software products. If you wish to use this code, algorithm or these message formats outside of JMRI, please contact Digitrax Inc for separate permission.

Author:
Bob Jacobsen Copyright (C) 2001
See Also:
NceMessage, Serialized Form

Constructor Summary
LocoNetMessage(byte[] contents)
           
LocoNetMessage(int len)
          Create a new object, representing a specific-length message.
LocoNetMessage(int[] contents)
          Create a message with specified contents
LocoNetMessage(LocoNetMessage original)
           
 
Method Summary
 boolean checkParity()
          check whether the message has a valid parity
 boolean equals(LocoNetMessage m)
          Two messages are the same if their entire data content is the same.
 int getElement(int n)
           
 int getNumDataElements()
          Get length, including op code and error-detection byte
 int getOpCode()
           
 String getOpCodeHex()
          Get a String representation of the op code in hex
 int[] getPeerXfrData()
          Get the 8 data bytes from an OPC_PEER_XFR message
protected static boolean highBit(int val)
          Check if a high bit is set, usually used to store it in some other location (LocoNet does not allow the high bit to be set in data bytes)
protected static int highByte(int val)
           
 int inputRepAddr()
          If this is an OPC_INPUT_REP, return the 0-n address, else -1
protected static int lowByte(int val)
           
static LocoNetMessage makePeerXfr(int src, int dst, int[] d, int code)
          Return a newly created OPC_PEER_XFR message.
 int sensorAddr()
          Sensor-format 0-n address
 void setElement(int n, int v)
           
 void setOpCode(int i)
           
 void setParity()
          Set the parity byte(s) of this message
 String toString()
          Get a String representation of the entire message in hex
 int turnoutAddr()
          Return the 1-N turnout address
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LocoNetMessage

public LocoNetMessage(int len)
Create a new object, representing a specific-length message.

Parameters:
len - Total bytes in message, including opcode and error-detection byte.

LocoNetMessage

public LocoNetMessage(int[] contents)
Create a message with specified contents

Parameters:
contents - The array of contents for the message. The error check word must be present, e.g. a 4-byte message must have four values in the array

LocoNetMessage

public LocoNetMessage(byte[] contents)

LocoNetMessage

public LocoNetMessage(LocoNetMessage original)
Method Detail

setOpCode

public void setOpCode(int i)

getOpCode

public int getOpCode()

getOpCodeHex

public String getOpCodeHex()
Get a String representation of the op code in hex


getNumDataElements

public int getNumDataElements()
Get length, including op code and error-detection byte


getElement

public int getElement(int n)

setElement

public void setElement(int n,
                       int v)

toString

public String toString()
Get a String representation of the entire message in hex

Overrides:
toString in class Object

setParity

public void setParity()
Set the parity byte(s) of this message


checkParity

public boolean checkParity()
check whether the message has a valid parity


getPeerXfrData

public int[] getPeerXfrData()
Get the 8 data bytes from an OPC_PEER_XFR message

Returns:
int[8] data bytes

equals

public boolean equals(LocoNetMessage m)
Two messages are the same if their entire data content is the same. We ignore the error-check byte to ease comparisons before a message is transmitted.

Parameters:
m -
Returns:
true if objects contain the same message contents

makePeerXfr

public static LocoNetMessage makePeerXfr(int src,
                                         int dst,
                                         int[] d,
                                         int code)
Return a newly created OPC_PEER_XFR message.

Parameters:
src - Source address
dst - Destination address
d - int[8] for the data contents or null
code - The instruction code placed in the pcxt1 pcxt2 bytes
Returns:
The formatted message

highBit

protected static boolean highBit(int val)
Check if a high bit is set, usually used to store it in some other location (LocoNet does not allow the high bit to be set in data bytes)

Parameters:
val -
Returns:
True if the argument has the high bit set

lowByte

protected static int lowByte(int val)

highByte

protected static int highByte(int val)

sensorAddr

public int sensorAddr()
Sensor-format 0-n address

Returns:
0 to n-1 address

inputRepAddr

public int inputRepAddr()
If this is an OPC_INPUT_REP, return the 0-n address, else -1

Returns:
0 to n-1 address

turnoutAddr

public int turnoutAddr()
Return the 1-N turnout address

Returns:
1-N address


Copyright © 1997 - 2009 JMRI Community.
JMRI, DecoderPro, PanelPro, SoundPro, DispatcherPro and associated logos are our trademarks.

Additional information on copyright, trademarks and licenses is linked here.
Site hosted by: Get JMRI Model Railroad Interface at SourceForge.net. Fast, secure and Free Open Source software downloads