circuitpython/tests/pyb/can.py

51 lines
841 B
Python
Raw Normal View History

2014-10-02 12:30:28 -04:00
from pyb import CAN
2014-11-21 17:24:23 -05:00
CAN.initfilterbanks(14)
2014-10-30 19:13:53 -04:00
can = CAN(1)
print(can)
can.init(CAN.LOOPBACK)
2014-10-02 12:30:28 -04:00
print(can)
print(can.any(0))
2014-10-30 19:13:53 -04:00
2014-11-21 17:24:23 -05:00
# Catch all filter
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))
2014-10-02 12:30:28 -04:00
can.send('abcd', 123)
print(can.any(0))
print(can.recv(0))
can.send('abcd', -1)
print(can.recv(0))
can.send('abcd', 0x7FF + 1)
print(can.recv(0))
2014-11-21 17:24:23 -05:00
# Test too long message
try:
can.send('abcdefghi', 0x7FF)
except ValueError:
print('passed')
else:
print('failed')
del can
2014-11-21 17:24:23 -05:00
# Testing extended IDs
can = CAN(1, CAN.LOOPBACK, extframe = True)
2014-11-21 17:24:23 -05:00
# Catch all filter
can.setfilter(0, CAN.MASK32, 0, (0, 0))
print(can)
try:
can.send('abcde', 0x7FF + 1)
except ValueError:
2014-11-21 17:24:23 -05:00
print('failed')
else:
r = can.recv(0)
if r[0] == 0x7FF+1 and r[3] == b'abcde':
print('passed')
else:
print('failed, wrong data received')