Skip to content

Commit 8a20273

Browse files
authored
Merge pull request #2492 from adafruit/picowbell_canbus
adding CP example for PiCowbell
2 parents fa59f83 + 2735149 commit 8a20273

File tree

1 file changed

+36
-0
lines changed
  • PiCowbell_CAN_Bus_Examples/CircuitPython_PiCowbell_CAN_Bus

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
'''Simple Test for the PiCowbell CAN Bus with Raspberry Pi Pico'''
5+
6+
from time import sleep
7+
import board
8+
import busio
9+
from digitalio import DigitalInOut
10+
from adafruit_mcp2515.canio import Message, RemoteTransmissionRequest
11+
from adafruit_mcp2515 import MCP2515 as CAN
12+
13+
14+
cs = DigitalInOut(board.GP20)
15+
cs.switch_to_output()
16+
spi = busio.SPI(board.GP18, board.GP19, board.GP16)
17+
18+
can_bus = CAN(
19+
spi, cs, loopback=True, silent=True
20+
) # use loopback to test without another device
21+
while True:
22+
with can_bus.listen(timeout=1.0) as listener:
23+
24+
message = Message(id=0x1234ABCD, data=b"adafruit", extended=True)
25+
send_success = can_bus.send(message)
26+
print("Send success:", send_success)
27+
message_count = listener.in_waiting()
28+
print(message_count, "messages available")
29+
for _i in range(message_count):
30+
msg = listener.receive()
31+
print("Message from ", hex(msg.id))
32+
if isinstance(msg, Message):
33+
print("message data:", msg.data)
34+
if isinstance(msg, RemoteTransmissionRequest):
35+
print("RTR length:", msg.length)
36+
sleep(1)

0 commit comments

Comments
 (0)