From 2d77a6991c066bf2edf65bf842b3e03c06e7e05b Mon Sep 17 00:00:00 2001 From: XanderHayhoe Date: Fri, 3 May 2024 20:23:22 -0400 Subject: [PATCH 1/9] testing --- core.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 core.py diff --git a/core.py b/core.py new file mode 100644 index 0000000..e69de29 From e7019fae54d348383e2dc3e1a80d0fb1b5fdd699 Mon Sep 17 00:00:00 2001 From: XanderHayhoe Date: Fri, 3 May 2024 20:46:22 -0400 Subject: [PATCH 2/9] POC --- core.py | 20 ++++++++++++++++++++ custom_listener.py | 25 +++++++++++++++++++++++++ mthread_listener.py | 20 ++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 custom_listener.py create mode 100644 mthread_listener.py diff --git a/core.py b/core.py index e69de29..ad68cb6 100644 --- a/core.py +++ b/core.py @@ -0,0 +1,20 @@ +import can + +import time + +with can.Bus() as bus: + msg = can.Message( + arbitration_id=0xC0FFEE, + data=[0, 25, 0, 1, 3, 1, 4, 1], + is_extended_id=True + ) + try: + bus.send(msg) + print(f"Message sent on {bus.channel_info}") + except can.CanError: + print("Message NOT sent") + + +with can.Bus() as bus: + for msg in bus: + print(msg.data) \ No newline at end of file diff --git a/custom_listener.py b/custom_listener.py new file mode 100644 index 0000000..63b4022 --- /dev/null +++ b/custom_listener.py @@ -0,0 +1,25 @@ +import can +import time + +# Define a listener class that can handle messages +class SomeListener(can.Listener): + def on_message_received(self, msg): + print(msg) + +# Setup the CAN bus +my_bus = can.Bus(channel='can0', bustype='socketcan', receive_own_messages=True) + +# Instantiate the listener object +listener = SomeListener() + +# Use a Notifier to handle messages asynchronously +notifier = can.Notifier(my_bus, [listener]) + +try: + # The script now runs and listens for messages indefinitely + while True: + time.sleep(1) # Pause the loop to prevent it from running too fast +except KeyboardInterrupt: + # Stop the notifier to clean up resources when you stop the script manually + notifier.stop() + print("Stopped listening on CAN bus.") diff --git a/mthread_listener.py b/mthread_listener.py new file mode 100644 index 0000000..186a16f --- /dev/null +++ b/mthread_listener.py @@ -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() \ No newline at end of file From 021d9605473b9583a1d845736de0fd7cd7bd9843 Mon Sep 17 00:00:00 2001 From: XanderHayhoe Date: Sun, 5 May 2024 02:37:29 -0400 Subject: [PATCH 3/9] pls --- core.py | 20 ++------------------ custom_listener.py | 26 +++----------------------- 2 files changed, 5 insertions(+), 41 deletions(-) diff --git a/core.py b/core.py index ad68cb6..36c45cc 100644 --- a/core.py +++ b/core.py @@ -1,20 +1,4 @@ import can -import time - -with can.Bus() as bus: - msg = can.Message( - arbitration_id=0xC0FFEE, - data=[0, 25, 0, 1, 3, 1, 4, 1], - is_extended_id=True - ) - try: - bus.send(msg) - print(f"Message sent on {bus.channel_info}") - except can.CanError: - print("Message NOT sent") - - -with can.Bus() as bus: - for msg in bus: - print(msg.data) \ No newline at end of file +bus = can.interface.Bus(channel="can0", bustype="socketcan_native") +notifier = can.Notifier(bus, [can.Printer()]) \ No newline at end of file diff --git a/custom_listener.py b/custom_listener.py index 63b4022..bc42304 100644 --- a/custom_listener.py +++ b/custom_listener.py @@ -1,25 +1,5 @@ import can -import time -# Define a listener class that can handle messages -class SomeListener(can.Listener): - def on_message_received(self, msg): - print(msg) - -# Setup the CAN bus -my_bus = can.Bus(channel='can0', bustype='socketcan', receive_own_messages=True) - -# Instantiate the listener object -listener = SomeListener() - -# Use a Notifier to handle messages asynchronously -notifier = can.Notifier(my_bus, [listener]) - -try: - # The script now runs and listens for messages indefinitely - while True: - time.sleep(1) # Pause the loop to prevent it from running too fast -except KeyboardInterrupt: - # Stop the notifier to clean up resources when you stop the script manually - notifier.stop() - print("Stopped listening on CAN bus.") +bus = can.interface.Bus(channel="can0", bustype="socketcan_native") +msg = can.Message(arbitration_id=0x7de,data=[0, 25, 0, 1, 3, 1, 4, 1]) +bus.send(msg) \ No newline at end of file From c54d32c2aca58a9971b4c72e73af1e01d97aed0f Mon Sep 17 00:00:00 2001 From: XanderHayhoe Date: Sun, 5 May 2024 02:41:34 -0400 Subject: [PATCH 4/9] pls --- core.py | 2 +- custom_listener.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core.py b/core.py index 36c45cc..9b2f5d0 100644 --- a/core.py +++ b/core.py @@ -1,4 +1,4 @@ import can -bus = can.interface.Bus(channel="can0", bustype="socketcan_native") +bus = can.interface.Bus(channel="can0", bustype="socketcan") notifier = can.Notifier(bus, [can.Printer()]) \ No newline at end of file diff --git a/custom_listener.py b/custom_listener.py index bc42304..99288af 100644 --- a/custom_listener.py +++ b/custom_listener.py @@ -1,5 +1,5 @@ import can -bus = can.interface.Bus(channel="can0", bustype="socketcan_native") +bus = can.interface.Bus(channel="can0", bustype="socketcan") msg = can.Message(arbitration_id=0x7de,data=[0, 25, 0, 1, 3, 1, 4, 1]) bus.send(msg) \ No newline at end of file From 5f1828fa6bc37d660dacb07eb23e93ab6c60de94 Mon Sep 17 00:00:00 2001 From: XanderHayhoe Date: Sun, 5 May 2024 02:44:15 -0400 Subject: [PATCH 5/9] pls --- core.py | 18 ++++++++++++++++-- custom_listener.py | 4 +++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/core.py b/core.py index 9b2f5d0..b7da5e0 100644 --- a/core.py +++ b/core.py @@ -1,4 +1,18 @@ import can +import time -bus = can.interface.Bus(channel="can0", bustype="socketcan") -notifier = can.Notifier(bus, [can.Printer()]) \ No newline at end of file +def main(): + bus = can.interface.Bus(channel='can0', bustype='socketcan') + 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() diff --git a/custom_listener.py b/custom_listener.py index 99288af..9d6535a 100644 --- a/custom_listener.py +++ b/custom_listener.py @@ -2,4 +2,6 @@ bus = can.interface.Bus(channel="can0", bustype="socketcan") msg = can.Message(arbitration_id=0x7de,data=[0, 25, 0, 1, 3, 1, 4, 1]) -bus.send(msg) \ No newline at end of file +bus.send(msg) + +bus.shutdown() \ No newline at end of file From 5129038ddffb34aa9145036dfa67c97d026cd862 Mon Sep 17 00:00:00 2001 From: XanderHayhoe Date: Sun, 5 May 2024 02:49:27 -0400 Subject: [PATCH 6/9] pls --- core.py | 2 +- custom_listener.py | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/core.py b/core.py index b7da5e0..55738c7 100644 --- a/core.py +++ b/core.py @@ -2,7 +2,7 @@ import time def main(): - bus = can.interface.Bus(channel='can0', bustype='socketcan') + bus = can.interface.Bus(channel="can0", bustype="socketcan", loopback=True) notifier = can.Notifier(bus, [can.Printer()]) try: diff --git a/custom_listener.py b/custom_listener.py index 9d6535a..52272dc 100644 --- a/custom_listener.py +++ b/custom_listener.py @@ -1,7 +1,12 @@ import can -bus = can.interface.Bus(channel="can0", bustype="socketcan") -msg = can.Message(arbitration_id=0x7de,data=[0, 25, 0, 1, 3, 1, 4, 1]) -bus.send(msg) +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) -bus.shutdown() \ No newline at end of file +try: + bus.send(msg) + print("Message sent") +except can.CanError as e: + print("Message failed to send", e) + +bus.shutdown() From 3877cb815ea3f444b6ec5049ee51d45d1f98636d Mon Sep 17 00:00:00 2001 From: XanderHayhoe Date: Sun, 5 May 2024 02:50:20 -0400 Subject: [PATCH 7/9] pls --- core.py | 1 + 1 file changed, 1 insertion(+) diff --git a/core.py b/core.py index 55738c7..eb719bc 100644 --- a/core.py +++ b/core.py @@ -2,6 +2,7 @@ import time def main(): + time.sleep(5) bus = can.interface.Bus(channel="can0", bustype="socketcan", loopback=True) notifier = can.Notifier(bus, [can.Printer()]) From 80ee5a4ee9813637825990dfee746c08e280df42 Mon Sep 17 00:00:00 2001 From: XanderHayhoe Date: Sun, 5 May 2024 02:51:21 -0400 Subject: [PATCH 8/9] pls --- custom_listener.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/custom_listener.py b/custom_listener.py index 52272dc..c373818 100644 --- a/custom_listener.py +++ b/custom_listener.py @@ -4,8 +4,9 @@ msg = can.Message(arbitration_id=0x7de, data=[0, 25, 0, 1, 3, 1, 4, 1], is_extended_id=False) try: - bus.send(msg) - print("Message sent") + while True: + bus.send(msg) + print("Message sent") except can.CanError as e: print("Message failed to send", e) From 95aa98e662ffbf38f08b0cf7eb48a081ebd75846 Mon Sep 17 00:00:00 2001 From: XanderHayhoe Date: Sun, 5 May 2024 02:52:00 -0400 Subject: [PATCH 9/9] pls --- custom_listener.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_listener.py b/custom_listener.py index c373818..29e4537 100644 --- a/custom_listener.py +++ b/custom_listener.py @@ -1,10 +1,11 @@ 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: