Package com.tinkerforge
Class BrickletCAN
- java.lang.Object
-
- com.tinkerforge.DeviceBase
-
- com.tinkerforge.Device
-
- com.tinkerforge.BrickletCAN
-
public class BrickletCAN extends Device
Communicates with CAN bus devices
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
BrickletCAN.Configuration
class
BrickletCAN.ErrorLog
static interface
BrickletCAN.FrameReadableListener
This listener is triggered if a data or remote frame was received by the CAN transceiver.static interface
BrickletCAN.FrameReadListener
This listener is triggered if a data or remote frame was received by the CAN transceiver.class
BrickletCAN.ReadFilter
class
BrickletCAN.ReadFrame
-
Nested classes/interfaces inherited from class com.tinkerforge.Device
Device.Identity
-
-
Field Summary
-
Constructor Summary
Constructors Constructor Description BrickletCAN(java.lang.String uid, IPConnection ipcon)
Creates an object with the unique device ID \c uid.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addFrameReadableListener(BrickletCAN.FrameReadableListener listener)
Adds a FrameReadable listener.void
addFrameReadListener(BrickletCAN.FrameReadListener listener)
Adds a FrameRead listener.void
disableFrameReadCallback()
Disables theBrickletCAN.FrameReadListener
listener.void
enableFrameReadCallback()
Enables theBrickletCAN.FrameReadListener
listener.BrickletCAN.Configuration
getConfiguration()
Returns the configuration as set bysetConfiguration(short, short, int)
.BrickletCAN.ErrorLog
getErrorLog()
Returns information about different kinds of errors.boolean
getFrameReadableCallbackConfiguration()
Returns *true* if theBrickletCAN.FrameReadableListener
listener is enabled, *false* otherwise.Device.Identity
getIdentity()
Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.BrickletCAN.ReadFilter
getReadFilter()
Returns the read filter as set bysetReadFilter(short, long, long, long)
.boolean
isFrameReadCallbackEnabled()
Returns *true* if theBrickletCAN.FrameReadListener
listener is enabled, *false* otherwise.BrickletCAN.ReadFrame
readFrame()
Tries to read the next data or remote frame from the read buffer and return it.void
removeFrameReadableListener(BrickletCAN.FrameReadableListener listener)
Removes a FrameReadable listener.void
removeFrameReadListener(BrickletCAN.FrameReadListener listener)
Removes a FrameRead listener.void
setConfiguration(short baudRate, short transceiverMode, int writeTimeout)
Sets the configuration for the CAN bus communication.void
setFrameReadableCallbackConfiguration(boolean enabled)
Enables/disables theBrickletCAN.FrameReadableListener
listener.void
setReadFilter(short mode, long mask, long filter1, long filter2)
Set the read filter configuration.boolean
writeFrame(short frameType, long identifier, short[] data, short length)
Writes a data or remote frame to the write buffer to be transmitted over the CAN transceiver.-
Methods inherited from class com.tinkerforge.DeviceBase
getAPIVersion, getResponseExpected, setResponseExpected, setResponseExpectedAll
-
-
-
-
Field Detail
-
DEVICE_IDENTIFIER
public static final int DEVICE_IDENTIFIER
- See Also:
- Constant Field Values
-
DEVICE_DISPLAY_NAME
public static final java.lang.String DEVICE_DISPLAY_NAME
- See Also:
- Constant Field Values
-
FUNCTION_WRITE_FRAME
public static final byte FUNCTION_WRITE_FRAME
- See Also:
- Constant Field Values
-
FUNCTION_READ_FRAME
public static final byte FUNCTION_READ_FRAME
- See Also:
- Constant Field Values
-
FUNCTION_ENABLE_FRAME_READ_CALLBACK
public static final byte FUNCTION_ENABLE_FRAME_READ_CALLBACK
- See Also:
- Constant Field Values
-
FUNCTION_DISABLE_FRAME_READ_CALLBACK
public static final byte FUNCTION_DISABLE_FRAME_READ_CALLBACK
- See Also:
- Constant Field Values
-
FUNCTION_IS_FRAME_READ_CALLBACK_ENABLED
public static final byte FUNCTION_IS_FRAME_READ_CALLBACK_ENABLED
- See Also:
- Constant Field Values
-
FUNCTION_SET_CONFIGURATION
public static final byte FUNCTION_SET_CONFIGURATION
- See Also:
- Constant Field Values
-
FUNCTION_GET_CONFIGURATION
public static final byte FUNCTION_GET_CONFIGURATION
- See Also:
- Constant Field Values
-
FUNCTION_SET_READ_FILTER
public static final byte FUNCTION_SET_READ_FILTER
- See Also:
- Constant Field Values
-
FUNCTION_GET_READ_FILTER
public static final byte FUNCTION_GET_READ_FILTER
- See Also:
- Constant Field Values
-
FUNCTION_GET_ERROR_LOG
public static final byte FUNCTION_GET_ERROR_LOG
- See Also:
- Constant Field Values
-
FUNCTION_SET_FRAME_READABLE_CALLBACK_CONFIGURATION
public static final byte FUNCTION_SET_FRAME_READABLE_CALLBACK_CONFIGURATION
- See Also:
- Constant Field Values
-
FUNCTION_GET_FRAME_READABLE_CALLBACK_CONFIGURATION
public static final byte FUNCTION_GET_FRAME_READABLE_CALLBACK_CONFIGURATION
- See Also:
- Constant Field Values
-
FUNCTION_GET_IDENTITY
public static final byte FUNCTION_GET_IDENTITY
- See Also:
- Constant Field Values
-
FRAME_TYPE_STANDARD_DATA
public static final short FRAME_TYPE_STANDARD_DATA
- See Also:
- Constant Field Values
-
FRAME_TYPE_STANDARD_REMOTE
public static final short FRAME_TYPE_STANDARD_REMOTE
- See Also:
- Constant Field Values
-
FRAME_TYPE_EXTENDED_DATA
public static final short FRAME_TYPE_EXTENDED_DATA
- See Also:
- Constant Field Values
-
FRAME_TYPE_EXTENDED_REMOTE
public static final short FRAME_TYPE_EXTENDED_REMOTE
- See Also:
- Constant Field Values
-
BAUD_RATE_10KBPS
public static final short BAUD_RATE_10KBPS
- See Also:
- Constant Field Values
-
BAUD_RATE_20KBPS
public static final short BAUD_RATE_20KBPS
- See Also:
- Constant Field Values
-
BAUD_RATE_50KBPS
public static final short BAUD_RATE_50KBPS
- See Also:
- Constant Field Values
-
BAUD_RATE_125KBPS
public static final short BAUD_RATE_125KBPS
- See Also:
- Constant Field Values
-
BAUD_RATE_250KBPS
public static final short BAUD_RATE_250KBPS
- See Also:
- Constant Field Values
-
BAUD_RATE_500KBPS
public static final short BAUD_RATE_500KBPS
- See Also:
- Constant Field Values
-
BAUD_RATE_800KBPS
public static final short BAUD_RATE_800KBPS
- See Also:
- Constant Field Values
-
BAUD_RATE_1000KBPS
public static final short BAUD_RATE_1000KBPS
- See Also:
- Constant Field Values
-
TRANSCEIVER_MODE_NORMAL
public static final short TRANSCEIVER_MODE_NORMAL
- See Also:
- Constant Field Values
-
TRANSCEIVER_MODE_LOOPBACK
public static final short TRANSCEIVER_MODE_LOOPBACK
- See Also:
- Constant Field Values
-
TRANSCEIVER_MODE_READ_ONLY
public static final short TRANSCEIVER_MODE_READ_ONLY
- See Also:
- Constant Field Values
-
FILTER_MODE_DISABLED
public static final short FILTER_MODE_DISABLED
- See Also:
- Constant Field Values
-
FILTER_MODE_ACCEPT_ALL
public static final short FILTER_MODE_ACCEPT_ALL
- See Also:
- Constant Field Values
-
FILTER_MODE_MATCH_STANDARD
public static final short FILTER_MODE_MATCH_STANDARD
- See Also:
- Constant Field Values
-
FILTER_MODE_MATCH_STANDARD_AND_DATA
public static final short FILTER_MODE_MATCH_STANDARD_AND_DATA
- See Also:
- Constant Field Values
-
FILTER_MODE_MATCH_EXTENDED
public static final short FILTER_MODE_MATCH_EXTENDED
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BrickletCAN
public BrickletCAN(java.lang.String uid, IPConnection ipcon)
Creates an object with the unique device ID \c uid. and adds it to the IP Connection \c ipcon.
-
-
Method Detail
-
writeFrame
public boolean writeFrame(short frameType, long identifier, short[] data, short length) throws TinkerforgeException
Writes a data or remote frame to the write buffer to be transmitted over the CAN transceiver. The Bricklet supports the standard 11-bit (CAN 2.0A) and the additional extended 18-bit (CAN 2.0B) identifiers. For standard frames the Bricklet uses bit 0 to 10 from the ``identifier`` parameter as standard 11-bit identifier. For extended frames the Bricklet additionally uses bit 11 to 28 from the ``identifier`` parameter as extended 18-bit identifier. For remote frames the ``data`` parameter is ignored. Returns *true* if the frame was successfully added to the write buffer. Returns *false* if the frame could not be added because write buffer is already full. The write buffer can overflow if frames are written to it at a higher rate than the Bricklet can transmitted them over the CAN transceiver. This may happen if the CAN transceiver is configured as read-only or is using a low baud rate (seesetConfiguration(short, short, int)
). It can also happen if the CAN bus is congested and the frame cannot be transmitted because it constantly loses arbitration or because the CAN transceiver is currently disabled due to a high write error level (seegetErrorLog()
).- Throws:
TinkerforgeException
-
readFrame
public BrickletCAN.ReadFrame readFrame() throws TinkerforgeException
Tries to read the next data or remote frame from the read buffer and return it. If a frame was successfully read, then the ``success`` return value is set to *true* and the other return values contain the frame. If the read buffer is empty and no frame could be read, then the ``success`` return value is set to *false* and the other return values contain invalid data. The ``identifier`` return value follows the identifier format described forwriteFrame(short, long, short[], short)
. For remote frames the ``data`` return value always contains invalid data. A configurable read filter can be used to define which frames should be received by the CAN transceiver and put into the read buffer (seesetReadFilter(short, long, long, long)
). Instead of polling with this function, you can also use listeners. See theenableFrameReadCallback()
function and theBrickletCAN.FrameReadListener
listener.- Throws:
TinkerforgeException
-
enableFrameReadCallback
public void enableFrameReadCallback() throws TinkerforgeException
Enables theBrickletCAN.FrameReadListener
listener. By default the listener is disabled. Enabling this listener will disable theBrickletCAN.FrameReadableListener
listener.- Throws:
TinkerforgeException
-
disableFrameReadCallback
public void disableFrameReadCallback() throws TinkerforgeException
Disables theBrickletCAN.FrameReadListener
listener. By default the listener is disabled.- Throws:
TinkerforgeException
-
isFrameReadCallbackEnabled
public boolean isFrameReadCallbackEnabled() throws TinkerforgeException
Returns *true* if theBrickletCAN.FrameReadListener
listener is enabled, *false* otherwise.- Throws:
TinkerforgeException
-
setConfiguration
public void setConfiguration(short baudRate, short transceiverMode, int writeTimeout) throws TinkerforgeException
Sets the configuration for the CAN bus communication. The baud rate can be configured in steps between 10 and 1000 kbit/s. The CAN transceiver has three different modes: * Normal: Reads from and writes to the CAN bus and performs active bus error detection and acknowledgement. * Loopback: All reads and writes are performed internally. The transceiver is disconnected from the actual CAN bus. * Read-Only: Only reads from the CAN bus, but does neither active bus error detection nor acknowledgement. Only the receiving part of the transceiver is connected to the CAN bus. The write timeout has three different modes that define how a failed frame transmission should be handled: * One-Shot (= -1): Only one transmission attempt will be made. If the transmission fails then the frame is discarded. * Infinite (= 0): Infinite transmission attempts will be made. The frame will never be discarded. * Milliseconds (> 0): A limited number of transmission attempts will be made. If the frame could not be transmitted successfully after the configured number of milliseconds then the frame is discarded.- Throws:
TinkerforgeException
-
getConfiguration
public BrickletCAN.Configuration getConfiguration() throws TinkerforgeException
Returns the configuration as set bysetConfiguration(short, short, int)
.- Throws:
TinkerforgeException
-
setReadFilter
public void setReadFilter(short mode, long mask, long filter1, long filter2) throws TinkerforgeException
Set the read filter configuration. This can be used to define which frames should be received by the CAN transceiver and put into the read buffer. The read filter has five different modes that define if and how the mask and the two filters are applied: * Disabled: No filtering is applied at all. All frames are received even incomplete and defective frames. This mode should be used for debugging only. * Accept-All: All complete and error-free frames are received. * Match-Standard: Only standard frames with a matching identifier are received. * Match-Standard-and-Data: Only standard frames with matching identifier and data bytes are received. * Match-Extended: Only extended frames with a matching identifier are received. The mask and filters are used as bit masks. Their usage depends on the mode: * Disabled: Mask and filters are ignored. * Accept-All: Mask and filters are ignored. * Match-Standard: Bit 0 to 10 (11 bits) of mask and filters are used to match the 11-bit identifier of standard frames. * Match-Standard-and-Data: Bit 0 to 10 (11 bits) of mask and filters are used to match the 11-bit identifier of standard frames. Bit 11 to 18 (8 bits) and bit 19 to 26 (8 bits) of mask and filters are used to match the first and second data byte (if present) of standard frames. * Match-Extended: Bit 0 to 10 (11 bits) of mask and filters are used to match the standard 11-bit identifier part of extended frames. Bit 11 to 28 (18 bits) of mask and filters are used to match the extended 18-bit identifier part of extended frames. The mask and filters are applied in this way: The mask is used to select the identifier and data bits that should be compared to the corresponding filter bits. All unselected bits are automatically accepted. All selected bits have to match one of the filters to be accepted. If all bits for the selected mode are accepted then the frame is accepted and is added to the read buffer. \verbatim "Mask Bit", "Filter Bit", "Identifier/Data Bit", "Result" 0, X, X, Accept 1, 0, 0, Accept 1, 0, 1, Reject 1, 1, 0, Reject 1, 1, 1, Accept \endverbatim For example, to receive standard frames with identifier 0x123 only the mode can be set to Match-Standard with 0x7FF as mask and 0x123 as filter 1 and filter 2. The mask of 0x7FF selects all 11 identifier bits for matching so that the identifier has to be exactly 0x123 to be accepted. To accept identifier 0x123 and identifier 0x456 at the same time, just set filter 2 to 0x456 and keep mask and filter 1 unchanged.- Throws:
TinkerforgeException
-
getReadFilter
public BrickletCAN.ReadFilter getReadFilter() throws TinkerforgeException
Returns the read filter as set bysetReadFilter(short, long, long, long)
.- Throws:
TinkerforgeException
-
getErrorLog
public BrickletCAN.ErrorLog getErrorLog() throws TinkerforgeException
Returns information about different kinds of errors. The write and read error levels indicate the current level of checksum, acknowledgement, form, bit and stuffing errors during CAN bus write and read operations. When the write error level exceeds 255 then the CAN transceiver gets disabled and no frames can be transmitted or received anymore. The CAN transceiver will automatically be activated again after the CAN bus is idle for a while. The write and read error levels are not available in read-only transceiver mode (seesetConfiguration(short, short, int)
) and are reset to 0 as a side effect of changing the configuration or the read filter. The write timeout, read register and buffer overflow counts represents the number of these errors: * A write timeout occurs if a frame could not be transmitted before the configured write timeout expired (seesetConfiguration(short, short, int)
). * A read register overflow occurs if the read register of the CAN transceiver still contains the last received frame when the next frame arrives. In this case the newly arrived frame is lost. This happens if the CAN transceiver receives more frames than the Bricklet can handle. Using the read filter (seesetReadFilter(short, long, long, long)
) can help to reduce the amount of received frames. This count is not exact, but a lower bound, because the Bricklet might not able detect all overflows if they occur in rapid succession. * A read buffer overflow occurs if the read buffer of the Bricklet is already full when the next frame should be read from the read register of the CAN transceiver. In this case the frame in the read register is lost. This happens if the CAN transceiver receives more frames to be added to the read buffer than are removed from the read buffer using thereadFrame()
function. Using theBrickletCAN.FrameReadListener
listener ensures that the read buffer can not overflow.- Throws:
TinkerforgeException
-
setFrameReadableCallbackConfiguration
public void setFrameReadableCallbackConfiguration(boolean enabled) throws TinkerforgeException
Enables/disables theBrickletCAN.FrameReadableListener
listener. By default the listener is disabled. Enabling this listener will disable theBrickletCAN.FrameReadListener
listener. .. versionadded:: 2.0.1$nbsp;(Plugin)- Throws:
TinkerforgeException
-
getFrameReadableCallbackConfiguration
public boolean getFrameReadableCallbackConfiguration() throws TinkerforgeException
Returns *true* if theBrickletCAN.FrameReadableListener
listener is enabled, *false* otherwise. .. versionadded:: 2.0.1$nbsp;(Plugin)- Throws:
TinkerforgeException
-
getIdentity
public Device.Identity getIdentity() throws TinkerforgeException
Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier. The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an :ref:`Isolator Bricklet <isolator_bricklet>` is always at position 'z'. The device identifier numbers can be found :ref:`here <device_identifier>`. |device_identifier_constant|- Specified by:
getIdentity
in classDevice
- Throws:
TinkerforgeException
-
addFrameReadListener
public void addFrameReadListener(BrickletCAN.FrameReadListener listener)
Adds a FrameRead listener.
-
removeFrameReadListener
public void removeFrameReadListener(BrickletCAN.FrameReadListener listener)
Removes a FrameRead listener.
-
addFrameReadableListener
public void addFrameReadableListener(BrickletCAN.FrameReadableListener listener)
Adds a FrameReadable listener.
-
removeFrameReadableListener
public void removeFrameReadableListener(BrickletCAN.FrameReadableListener listener)
Removes a FrameReadable listener.
-
-