Skip to content

Commit 492030c

Browse files
authored
Update neovi_bus.py
Fix formatting
1 parent fd2eb24 commit 492030c

File tree

1 file changed

+42
-59
lines changed

1 file changed

+42
-59
lines changed

can/interfaces/ics_neovi/neovi_bus.py

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Implementation references:
88
* https://github.com/intrepidcs/python_ics
99
"""
10+
1011
import functools
1112
import logging
1213
import os
@@ -135,6 +136,7 @@ def check_if_bus_open(func):
135136
136137
If the bus is not open, it raises a CanOperationError.
137138
"""
139+
138140
@functools.wraps(func)
139141
def wrapper(self, *args, **kwargs):
140142
"""
@@ -432,91 +434,72 @@ def _recv_internal(self, timeout=0.1):
432434

433435
@check_if_bus_open
434436
def send(self, msg, timeout=0):
435-
"""Transmit a message to the CAN bus.
436-
437-
:param Message msg: A message object.
438-
439-
:param float timeout:
440-
If > 0, wait up to this many seconds for message to be ACK'ed.
441-
If timeout is exceeded, an exception will be raised.
442-
None blocks indefinitely.
443-
444-
:raises ValueError:
445-
if the message is invalid
446-
:raises can.CanTimeoutError:
447-
if sending timed out
448-
:raises CanOperationError:
449-
If the bus is closed or the message could otherwise not be sent.
450-
May or may not be a :class:`~ICSOperationError`.
437+
"""
438+
Sends a message over the CAN bus.
439+
440+
:param msg: The CAN message to be sent.
441+
:type msg: can.Message
442+
:param timeout: The maximum amount of time to wait for the message to be sent.
443+
:type timeout: float
444+
445+
:raises CanOperationError: If the bus is not open.
446+
:raises ValueError: If the DLC of the message is greater than the maximum allowed.
447+
:raises ICSOperationError: If there is an error in transmitting the message.
448+
:raises CanTimeoutError: If the message could not be sent within the specified timeout.
451449
"""
452450
if not ics.validate_hobject(self.dev):
453451
raise CanOperationError("bus not open")
454452

455-
# Check for valid DLC to avoid passing extra long data to the driver
456-
if msg.is_fd:
457-
if msg.dlc > 64:
458-
raise ValueError(
459-
f"DLC was {msg.dlc} but it should be <= 64 for CAN FD frames"
460-
)
461-
elif msg.dlc > 8:
453+
if msg.is_fd and msg.dlc > 64 or not msg.is_fd and msg.dlc > 8:
462454
raise ValueError(
463-
f"DLC was {msg.dlc} but it should be <= 8 for normal CAN frames"
455+
f"DLC was {msg.dlc} but it should be <= {64 if msg.is_fd else 8}"
464456
)
465457

466458
message = ics.SpyMessage()
459+
message.ArbIDOrHeader = msg.arbitration_id
460+
message.NumberBytesData = msg.dlc
461+
message.Data = tuple(msg.data[:8])
462+
message.StatusBitField = 0
463+
message.StatusBitField2 = 0
464+
message.StatusBitField3 = 0
467465

468-
flag0 = 0
469466
if msg.is_extended_id:
470-
flag0 |= ics.SPY_STATUS_XTD_FRAME
467+
message.StatusBitField |= ics.SPY_STATUS_XTD_FRAME
471468
if msg.is_remote_frame:
472-
flag0 |= ics.SPY_STATUS_REMOTE_FRAME
473-
474-
flag3 = 0
469+
message.StatusBitField |= ics.SPY_STATUS_REMOTE_FRAME
475470
if msg.is_fd:
476471
message.Protocol = ics.SPY_PROTOCOL_CANFD
477472
if msg.bitrate_switch:
478-
flag3 |= ics.SPY_STATUS3_CANFD_BRS
473+
message.StatusBitField3 |= ics.SPY_STATUS3_CANFD_BRS
479474
if msg.error_state_indicator:
480-
flag3 |= ics.SPY_STATUS3_CANFD_ESI
481-
482-
message.ArbIDOrHeader = msg.arbitration_id
483-
msg_data = msg.data[: msg.dlc]
484-
message.NumberBytesData = msg.dlc
485-
message.Data = tuple(msg_data[:8])
486-
if msg.is_fd and len(msg_data) > 8:
487-
message.ExtraDataPtrEnabled = 1
488-
message.ExtraDataPtr = tuple(msg_data)
489-
message.StatusBitField = flag0
490-
message.StatusBitField2 = 0
491-
message.StatusBitField3 = flag3
492-
if msg.channel is not None:
493-
network_id = msg.channel
494-
elif len(self.channels) == 1:
495-
network_id = self.channels[0]
496-
else:
475+
message.StatusBitField3 |= ics.SPY_STATUS3_CANFD_ESI
476+
if len(msg.data) > 8:
477+
message.ExtraDataPtrEnabled = 1
478+
message.ExtraDataPtr = tuple(msg.data)
479+
480+
network_id = (
481+
msg.channel
482+
if msg.channel is not None
483+
else self.channels[0] if len(self.channels) == 1 else None
484+
)
485+
if network_id is None:
497486
raise ValueError("msg.channel must be set when using multiple channels.")
498-
499487
message.NetworkID, message.NetworkID2 = int(network_id & 0xFF), int(
500488
(network_id >> 8) & 0xFF
501489
)
502490

503491
if timeout != 0:
504492
msg_desc_id = next(description_id)
505493
message.DescriptionID = msg_desc_id
506-
receipt_key = (msg.arbitration_id, msg_desc_id)
507-
self.message_receipts[receipt_key].clear()
494+
self.message_receipts[(msg.arbitration_id, msg_desc_id)].clear()
508495

509496
try:
510497
ics.transmit_messages(self.dev, message)
511498
except ics.RuntimeError:
512499
raise ICSOperationError(*ics.get_last_api_error(self.dev)) from None
513500

514-
# If timeout is set, wait for ACK
515-
# This requires a notifier for the bus or
516-
# some other thread calling recv periodically
517-
if timeout != 0:
518-
got_receipt = self.message_receipts[receipt_key].wait(timeout)
519-
# We no longer need this receipt, so no point keeping it in memory
520-
del self.message_receipts[receipt_key]
521-
if not got_receipt:
522-
raise CanTimeoutError("Transmit timeout")
501+
if timeout != 0 and not self.message_receipts[
502+
(msg.arbitration_id, msg_desc_id)
503+
].wait(timeout):
504+
del self.message_receipts[(msg.arbitration_id, msg_desc_id)]
505+
raise CanTimeoutError("Transmit timeout")

0 commit comments

Comments
 (0)