Class CanMessage
- java.lang.Object
-
- jmri.jmrix.AbstractMessage
-
- jmri.jmrix.AbstractMRMessage
-
- jmri.jmrix.can.CanMessage
-
- All Implemented Interfaces:
CanFrame
,CanMutableFrame
,Message
public class CanMessage extends AbstractMRMessage implements CanMutableFrame
Base class for messages in a CANbus based message/reply protocol.It is expected that any CAN based system will be based upon basic CANbus concepts such as ID (standard or extended), Normal and RTR frames and a data field.
The _dataChars[] and _nDataChars members refer to the data field, not the entire message.
"header" refers to the full 11 or 29 bit header; which mode is separately set via the "extended" parameter
CBUS uses a 2-bit "Pri" field and 7-bit "ID" ("CAN ID") field, with separate accessors. CBUS ID is set as a layout connection preference and registered by the traffic controller.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) int
_header
(package private) boolean
_isExtended
(package private) boolean
_isRtr
-
Fields inherited from class jmri.jmrix.AbstractMRMessage
LONG_TIMEOUT, SHORT_TIMEOUT
-
Fields inherited from class jmri.jmrix.AbstractMessage
_dataChars, _nDataChars
-
-
Constructor Summary
Constructors Constructor Description CanMessage(byte[] d, int header)
Create a new CanMessage from a byte arrayCanMessage(int header)
Create a new CanMessage with 8 data bytesCanMessage(int[] d, int header)
Create a new CanMessage from an int arrayCanMessage(int numDataBytes, int header)
Create a new CanMessage of given lengthCanMessage(CanMessage m)
Create a new CanMessage from an existing CanMessageCanMessage(CanReply m)
Create a new CanMessage from an existing CanReply
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(java.lang.Object a)
Note that a CanMessage and a CanReply can be tested for equality.int[]
getData()
Get the data bytes in array form.int
getElement(int n)
Get a particular element in a Message.int
getHeader()
Get the CAN Frame header.int
getNumDataElements()
Get the number of data elements in a Message.int
hashCode()
Hash on the headerboolean
isExtended()
Get if the CAN Frame has an extended header.boolean
isRtr()
Get if the CAN Frame is an RTR Frame.boolean
isTranslated()
Check if translation flag has been set.boolean
replyExpected()
Is a reply expected to this message?void
setElement(int n, int v)
Set a single Data Element at a particular index.void
setExtended(boolean b)
Set if the CAN Frame header is extended.void
setHeader(int h)
Set the CAN Frame header.void
setNumDataElements(int n)
Set the number of data elements in the main CAN Frame body.void
setRtr(boolean b)
Set if the CAN Frame is an RTR Frame.void
setTranslated(boolean translated)
Tag whether translation is needed.java.lang.String
toMonitorString()
This format matchesCanReply
java.lang.String
toString()
This format matchesCanReply
-
Methods inherited from class jmri.jmrix.AbstractMRMessage
addIntAsFourHex, addIntAsThree, addIntAsThreeHex, addIntAsTwoHex, getNeededMode, getOpCode, getOpCodeHex, getRetries, getTimeout, isBinary, setBinary, setNeededMode, setOpCode, setRetries, setTimeout
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface jmri.jmrix.can.CanFrame
appendHexElements, dataFramesEqual, extendedOrRtr, getToString, isEqual, monString
-
Methods inherited from interface jmri.jmrix.can.CanMutableFrame
setData, setData
-
-
-
-
Field Detail
-
_header
int _header
-
_isExtended
boolean _isExtended
-
_isRtr
boolean _isRtr
-
-
Constructor Detail
-
CanMessage
public CanMessage(int header)
Create a new CanMessage with 8 data bytes- Parameters:
header
- The CAN Frame header value
-
CanMessage
public CanMessage(int numDataBytes, int header)
Create a new CanMessage of given length- Parameters:
numDataBytes
- number of CAN Frame data bytes, max 8header
- The CAN Frame header value
-
CanMessage
public CanMessage(int[] d, int header)
Create a new CanMessage from an int array- Parameters:
d
- array of CAN Frame data bytes, max 8header
- The CAN Frame header value
-
CanMessage
public CanMessage(byte[] d, int header)
Create a new CanMessage from a byte array- Parameters:
d
- array of CAN Frame data bytes, max 8header
- The CAN Frame header value
-
CanMessage
public CanMessage(@Nonnull CanMessage m)
Create a new CanMessage from an existing CanMessage- Parameters:
m
- The existing CanMessage
-
CanMessage
public CanMessage(@Nonnull CanReply m)
Create a new CanMessage from an existing CanReply- Parameters:
m
- The existing CanReply
-
-
Method Detail
-
setTranslated
public void setTranslated(boolean translated)
Tag whether translation is needed. a "native" message has been converted already.- Parameters:
translated
- true or false to set flag as required
-
isTranslated
public boolean isTranslated()
Check if translation flag has been set.- Returns:
- false by default
-
hashCode
public int hashCode()
Hash on the header- Overrides:
hashCode
in classAbstractMessage
- Returns:
- hashcode from sum of elements.
-
equals
public boolean equals(java.lang.Object a)
Note that a CanMessage and a CanReply can be tested for equality.- Overrides:
equals
in classAbstractMessage
- Parameters:
a
- CanMessage or CanReply to test against
-
toString
public java.lang.String toString()
This format matchesCanReply
- Specified by:
toString
in interfaceMessage
- Overrides:
toString
in classAbstractMRMessage
-
toMonitorString
public java.lang.String toMonitorString()
This format matchesCanReply
- Specified by:
toMonitorString
in interfaceMessage
- Returns:
- a human-readable representation of the message.
-
replyExpected
public boolean replyExpected()
Is a reply expected to this message?By default, a reply is expected to every message; either a reply or a timeout is needed before the next message can be sent.
If this returns false, the transmit queue will immediately go on to transmit the next message (if any).
- Overrides:
replyExpected
in classAbstractMRMessage
- Returns:
- always false
-
getNumDataElements
public int getNumDataElements()
Get the number of data elements in a Message.- Specified by:
getNumDataElements
in interfaceCanFrame
- Specified by:
getNumDataElements
in interfaceMessage
- Overrides:
getNumDataElements
in classAbstractMessage
- Returns:
- number elements.
-
setNumDataElements
public final void setNumDataElements(int n)
Set the number of data elements in the main CAN Frame body.- Specified by:
setNumDataElements
in interfaceCanMutableFrame
- Parameters:
n
- true number of data bytes, 0-8
-
getElement
public int getElement(int n)
Get a particular element in a Message.- Specified by:
getElement
in interfaceCanFrame
- Specified by:
getElement
in interfaceMessage
- Overrides:
getElement
in classAbstractMessage
- Parameters:
n
- Element Index.- Returns:
- single element of message.
-
setElement
public void setElement(int n, int v)
Set a single Data Element at a particular index.- Specified by:
setElement
in interfaceCanMutableFrame
- Specified by:
setElement
in interfaceMessage
- Overrides:
setElement
in classAbstractMessage
- Parameters:
n
- index of element.v
- value of element.
-
getData
public int[] getData()
Get the data bytes in array form.- Returns:
- the actual int array
-
getHeader
public int getHeader()
Get the CAN Frame header.
-
setHeader
public void setHeader(int h)
Set the CAN Frame header.- Specified by:
setHeader
in interfaceCanMutableFrame
- Parameters:
h
- new header value
-
isExtended
public boolean isExtended()
Get if the CAN Frame has an extended header.- Specified by:
isExtended
in interfaceCanFrame
- Returns:
- true if extended, else false
-
setExtended
public void setExtended(boolean b)
Set if the CAN Frame header is extended.- Specified by:
setExtended
in interfaceCanMutableFrame
- Parameters:
b
- true if extended, else false
-
isRtr
public boolean isRtr()
Get if the CAN Frame is an RTR Frame.
-
setRtr
public void setRtr(boolean b)
Set if the CAN Frame is an RTR Frame.- Specified by:
setRtr
in interfaceCanMutableFrame
- Parameters:
b
- true if RTR, else false
-
-