Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Keep periodic transmission on data update
Use BCM for setting duration
  • Loading branch information
christiansandberg committed Jun 9, 2018
commit 7f20963fd3933a1e74be5143caa4b05196b4e475
34 changes: 21 additions & 13 deletions can/interfaces/socketcan/socketcan_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"""

import logging
import threading

import os
import select
Expand Down Expand Up @@ -143,6 +142,10 @@ def split_time(value):
return build_bcm_header(opcode, flags, count, ival1_seconds, ival1_usec, ival2_seconds, ival2_usec, can_id, nframes)


def build_bcm_update_header(can_id, msg_flags):
return build_bcm_header(CAN_BCM_TX_SETUP, msg_flags, 0, 0, 0, 0, 0, can_id, 1)


def dissect_can_frame(frame):
can_id, can_dlc, flags = CAN_FRAME_HEADER_STRUCT.unpack_from(frame)
if len(frame) != CANFD_MTU:
Expand Down Expand Up @@ -224,22 +227,31 @@ class CyclicSendTask(SocketCanBCMBase, LimitedDurationCyclicSendTaskABC,

"""

def __init__(self, channel, message, period):
def __init__(self, channel, message, period, duration=None):
"""
:param channel: The name of the CAN channel to connect to.
:param message: The message to be sent periodically.
:param period: The rate in seconds at which to send the message.
"""
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

maybe add the param to the docs here

super(CyclicSendTask, self).__init__(channel, message, period, duration=None)
super(CyclicSendTask, self).__init__(channel, message, period, duration)
self.duration = duration
self._tx_setup(message)
self.message = message

def _tx_setup(self, message):
# Create a low level packed frame to pass to the kernel
self.can_id_with_flags = _add_flags_to_can_id(message)
self.flags = CAN_FD_FRAME if message.is_fd else 0
header = build_bcm_transmit_header(self.can_id_with_flags, 0, 0.0,
self.period, self.flags)
if self.duration:
count = int(self.duration / self.period)
ival1 = self.period
ival2 = 0
else:
count = 0
ival1 = 0
ival2 = self.period
header = build_bcm_transmit_header(self.can_id_with_flags, count, ival1,
ival2, self.flags)
frame = build_can_frame(message)
log.debug("Sending BCM command")
send_bcm(self.bcm_socket, header + frame)
Expand All @@ -262,7 +274,9 @@ def modify_data(self, message):
Note the Message must have the same :attr:`~can.Message.arbitration_id`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

... "like the one of the message given in the constructor"

"""
assert message.arbitration_id == self.can_id, "You cannot modify the can identifier"
self._tx_setup(message)
header = build_bcm_update_header(self.can_id_with_flags, self.flags)
frame = build_can_frame(message)
send_bcm(self.bcm_socket, header + frame)

def start(self):
self._tx_setup(self.message)
Expand Down Expand Up @@ -481,13 +495,7 @@ def send(self, msg, timeout=None):
raise can.CanError("can.socketcan_native failed to transmit: {}".format(error_message))

def send_periodic(self, msg, period, duration=None):
task = CyclicSendTask(self.channel, msg, period)

if duration is not None:
stop_timer = threading.Timer(duration, task.stop)
stop_timer.start()

return task
return CyclicSendTask(self.channel, msg, period, duration)

def _apply_filters(self, filters):
try:
Expand Down