Skip to content

interface for changing bitrate #1030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions can/bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,25 @@ def __iter__(self) -> Iterator[Message]:
if msg is not None:
yield msg

@property
@abstractmethod
def bitrate(self) -> int:
raise NotImplementedError("Device must implement bitrate")

@bitrate.setter
@abstractmethod
def bitrate(self, bitrate: int) -> None:
"""Changes the bitrate of the underlying bus.

Override this method to enable runtime bitrate changes.

:param int bitrate: the new bitrate to set.

:raises can.CanError:
if the message could not be sent
"""
raise NotImplementedError("Device not capable of runtime bitrate change")

@property
def filters(self) -> Optional[can.typechecking.CanFilters]:
"""
Expand Down
10 changes: 10 additions & 0 deletions can/interfaces/vector/canlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def __init__(
)

if permission_mask.value == self.mask:
self.bitrate = bitrate
if fd:
self.canFdConf = xlclass.XLcanFdConf()
if bitrate:
Expand Down Expand Up @@ -515,6 +516,15 @@ def handle_canfd_event(self, event: xlclass.XLcanRxEvent) -> None:
def send(self, msg: Message, timeout: typing.Optional[float] = None):
self._send_sequence([msg])

@property
def bitrate(self) -> int:
return self.bitrate
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this recuse infinitely? Usually, I see this being implemented with a private attribute self._bitrate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops yeah I'll get that fixed. Just tossed this together quick to get some feedback.


@bitrate.setter
def bitrate(self, bitrate: int) -> None:
xldriver.xlCanSetChannelBitrate(self.port_handle, self.mask, bitrate)
LOG.info("SetChannelBitrate: baudr.=%u", bitrate)

def _send_sequence(self, msgs: typing.Sequence[Message]) -> int:
"""Send messages and return number of successful transmissions."""
if self.fd:
Expand Down