Skip to content

Canbus poc #3

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import can
import time

def main():
time.sleep(5)
bus = can.interface.Bus(channel="can0", bustype="socketcan", loopback=True)
notifier = can.Notifier(bus, [can.Printer()])

try:
# Simulate doing some work
time.sleep(10)
finally:
# Ensure the notifier is stopped first
notifier.stop()
# Then shut down the bus
bus.shutdown()

if __name__ == "__main__":
main()
14 changes: 14 additions & 0 deletions custom_listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import can
import time
bus = can.interface.Bus(channel="can0", bustype="socketcan", receive_own_messages=True)
msg = can.Message(arbitration_id=0x7de, data=[0, 25, 0, 1, 3, 1, 4, 1], is_extended_id=False)

try:
while True:
time.sleep(1)
bus.send(msg)
print("Message sent")
except can.CanError as e:
print("Message failed to send", e)

bus.shutdown()
20 changes: 20 additions & 0 deletions mthread_listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python

import time
import can


def main():
with can.Bus(receive_own_messages=True) as bus:
print_listener = can.Printer()
can.Notifier(bus, [print_listener])

bus.send(can.Message(arbitration_id=1, is_extended_id=True))
bus.send(can.Message(arbitration_id=2, is_extended_id=True))
bus.send(can.Message(arbitration_id=1, is_extended_id=False))

time.sleep(1.0)


if __name__ == "__main__":
main()