Skip to content

Commit

Permalink
feat(can): raise error when an error frame is detected. #8458
Browse files Browse the repository at this point in the history
  • Loading branch information
amitlissack authored Oct 5, 2021
1 parent 8c008c4 commit c038929
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions hardware/opentrons_hardware/drivers/can_bus/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from .arbitration_id import ArbitrationId
from .message import CanMessage
from .errors import ErrorFrameCanError


if platform.system() == "Darwin":
# TODO (amit, 2021-09-29): remove hacks to support `pcan` when we don't
Expand Down Expand Up @@ -112,9 +114,15 @@ async def read(self) -> CanMessage:
Returns:
A can message
Raises:
ErrorFrameCanError
"""
...
m: Message = await self._reader.get_message()
if m.is_error_frame:
raise ErrorFrameCanError(message=repr(m))

return CanMessage(
arbitration_id=ArbitrationId(id=m.arbitration_id), data=m.data
)
Expand Down
15 changes: 15 additions & 0 deletions hardware/opentrons_hardware/drivers/can_bus/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Can bus errors."""


class CanError(Exception):
"""Can bus error."""

def __init__(self, message: str) -> None:
"""Constructor."""
super().__init__(message)


class ErrorFrameCanError(CanError):
"""An error frame was received on the can bus."""

pass
15 changes: 15 additions & 0 deletions hardware/tests/opentrons_hardware/drivers/can_bus/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from can import Bus, Message

from opentrons_hardware.drivers.can_bus import CanDriver, ArbitrationId, CanMessage
from opentrons_hardware.drivers.can_bus.errors import ErrorFrameCanError


@pytest.fixture
Expand Down Expand Up @@ -89,3 +90,17 @@ async def test_receive_iter(subject: CanDriver, can_bus: Bus) -> None:
assert received[0].arbitration_id.id == 0x1FFFFFFF
assert received[1].data == bytearray([4, 3, 2, 1])
assert received[1].arbitration_id.id == 0


async def test_raise_error_frame_error(subject: CanDriver, can_bus: Bus) -> None:
"""It should raise an error when an error frame is received."""
m = Message(
arbitration_id=0x1FFFFFFF,
is_extended_id=True,
is_error_frame=True,
is_fd=True,
data=bytearray([1, 2, 3, 4]),
)
can_bus.send(m)
with pytest.raises(ErrorFrameCanError):
await subject.read()

0 comments on commit c038929

Please sign in to comment.