Skip to content

CanBusProxy

#include <mrx/can/CanBusProxy.hpp>
class mrx::can::CanBusProxy

Inherits: QObject

Sends and receives complete Qt CAN frames on one SocketCAN interface.

Proxies returned by CanBusFactory begin disconnected. The caller explicitly connects and disconnects them and controls their QObject lifetime. Transmit and receive delivery can be paused independently without disconnecting. Received frames count as bus activity even while receive delivery is paused. Activity starts after the first frame and expires after five seconds of silence by default; applications may override that interval with setActivityTimeout(). Disconnecting stops the activity timer and clears activity, emitting activityChanged(false) if the proxy was active.

ReturnNameDescription
~CanBusProxy override
QStringinterfaceName constReturns the SocketCAN interface name owned by this proxy.
QCanBusDevice::CanBusDeviceStatestate constReturns the underlying Qt SerialBus connection state.
QCanBusDevice::CanBusErrorerror constReturns the most recent Qt SerialBus error code.
QStringerrorString constReturns the most recent Qt SerialBus error description.
boolconnectBusExplicitly connects this proxy to its SocketCAN interface.
voiddisconnectBusDisconnects the interface and clears activity if active.
boolsendFrameSends a complete Qt CAN frame when transmitting is enabled and connected.
booltransmitEnabled constReturns whether outgoing frame delivery is enabled (initially true).
voidsetTransmitEnabledEnables or pauses outgoing frames without disconnecting the bus.
boolreceiveEnabled constReturns whether incoming frame delivery is enabled (initially true).
voidsetReceiveEnabledEnables or pauses incoming frame delivery without disconnecting.
QMetaObject::ConnectionlistenConnects a context-bound callback for frames with the given CAN ID.
boolisActive constReturns whether a frame arrived within the current activity window.
intactivityTimeout constReturns the inactivity timeout in milliseconds.
boolsetActivityTimeoutSets the inactivity timeout in milliseconds.

override
~CanBusProxy() override

const
QString interfaceName() const

Returns the SocketCAN interface name owned by this proxy.


const
QCanBusDevice::CanBusDeviceState state() const

Returns the underlying Qt SerialBus connection state.


const
QCanBusDevice::CanBusError error() const

Returns the most recent Qt SerialBus error code.


const
QString errorString() const

Returns the most recent Qt SerialBus error description.


bool connectBus()

Explicitly connects this proxy to its SocketCAN interface.

True when Qt SerialBus successfully opens the interface.


void disconnectBus()

Disconnects the interface and clears activity if active.


bool sendFrame(const QCanBusFrame& frame)

Sends a complete Qt CAN frame when transmitting is enabled and connected.

True when the connected Qt device accepts the frame for transmission.


const
bool transmitEnabled() const

Returns whether outgoing frame delivery is enabled (initially true).


void setTransmitEnabled(bool enabled)

Enables or pauses outgoing frames without disconnecting the bus.


const
bool receiveEnabled() const

Returns whether incoming frame delivery is enabled (initially true).


void setReceiveEnabled(bool enabled)

Enables or pauses incoming frame delivery without disconnecting.

Frames received while paused are drained and discarded, while activity is still tracked. Pending frames are drained before delivery is re-enabled.


QMetaObject::Connection listen(CAN_FRAME_ID frameId, FrameFormat format, QObject* context, const std::function<void(const QCanBusFrame&)>& handler)

Connects a context-bound callback for frames with the given CAN ID.

Matches data and remote request frames of the specified format; error frames are available through frameReceived instead. Delivery is paused together with frameReceived when receive delivery is disabled. The callback receives the original complete frame, regardless of payload length, and is disconnected when context is destroyed. A null context or empty callback produces an invalid connection.

A connection that may be explicitly disconnected by the caller.

NameTypeDescription
frameIdCAN_FRAME_IDCAN identifier to match.
formatFrameFormatStandard or extended CAN identifier format.
contextQObject*QObject that owns the callback’s lifetime.
handlerconst std::function<void(const QCanBusFrame&)>&Callback invoked for matching frames.

const
bool isActive() const

Returns whether a frame arrived within the current activity window.


const
int activityTimeout() const

Returns the inactivity timeout in milliseconds.

Defaults to 5000.


bool setActivityTimeout(int timeoutMs)

Sets the inactivity timeout in milliseconds.

Changing the timeout while active restarts the silence window. Zero is allowed and expires on the next event-loop turn; negative values are rejected without changing the current timeout.

True when timeoutMs is non-negative.

ReturnNameDescription
voidstateChangedForwarded when the underlying Qt device changes connection state.
voiderrorOccurredForwarded when the underlying Qt device reports an error.
voidframeReceivedEmitted for each received frame when receive delivery is enabled.
voidactivityChangedEmitted when the activity monitor changes active/inactive state.

void stateChanged(QCanBusDevice::CanBusDeviceState state)

Forwarded when the underlying Qt device changes connection state.


void errorOccurred(QCanBusDevice::CanBusError error)

Forwarded when the underlying Qt device reports an error.


void frameReceived(const QCanBusFrame& frame)

Emitted for each received frame when receive delivery is enabled.


void activityChanged(bool active)

Emitted when the activity monitor changes active/inactive state.

NameDescription
FrameFormatCAN identifier format used when matching received frames.

enum FrameFormat

CAN identifier format used when matching received frames.

ValueDescription
StandardStandard 11-bit identifier.
ExtendedExtended 29-bit identifier.