can.py 3.9 KB
Newer Older
Damien George's avatar
Damien George committed
1
from pyb import CAN
2
import pyb
Damien George's avatar
Damien George committed
3

Henrik Sölver's avatar
Henrik Sölver committed
4
CAN.initfilterbanks(14)
5
6
7
8
can = CAN(1)
print(can)

can.init(CAN.LOOPBACK)
Damien George's avatar
Damien George committed
9
10
print(can)
print(can.any(0))
11

Henrik Sölver's avatar
Henrik Sölver committed
12
13
14
# Catch all filter
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))

15
can.send('abcd', 123, timeout=5000)
Damien George's avatar
Damien George committed
16
17
print(can.any(0))
print(can.recv(0))
18

19
can.send('abcd', -1, timeout=5000)
20
21
print(can.recv(0))

22
can.send('abcd', 0x7FF + 1, timeout=5000)
23
24
print(can.recv(0))

Henrik Sölver's avatar
Henrik Sölver committed
25
# Test too long message
26
try:
27
    can.send('abcdefghi', 0x7FF, timeout=5000)
28
29
30
31
32
33
34
except ValueError:
    print('passed')
else:
    print('failed')

del can

Henrik Sölver's avatar
Henrik Sölver committed
35
# Testing extended IDs
36
can = CAN(1, CAN.LOOPBACK, extframe = True)
Henrik Sölver's avatar
Henrik Sölver committed
37
38
39
# Catch all filter
can.setfilter(0, CAN.MASK32, 0, (0, 0))

40
41
42
print(can)

try:
43
    can.send('abcde', 0x7FF + 1, timeout=5000)
44
except ValueError:
Henrik Sölver's avatar
Henrik Sölver committed
45
    print('failed')
46
47
48
49
50
51
else:
    r = can.recv(0)
    if r[0] == 0x7FF+1 and r[3] == b'abcde':
        print('passed')
    else:
        print('failed, wrong data received')
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

del can

# Test RxCallbacks
can = CAN(1, CAN.LOOPBACK)
can.setfilter(0, CAN.LIST16, 0, (1, 2, 3, 4))
can.setfilter(1, CAN.LIST16, 1, (5, 6, 7, 8))
def cb0(bus, reason):
    print('cb0')
    if reason == 0:
        print('pending')
    if reason == 1:
        print('full')
    if reason == 2:
        print('overflow')

def cb1(bus, reason):
    print('cb1')
    if reason == 0:
        print('pending')
    if reason == 1:
        print('full')
    if reason == 2:
        print('overflow')

def cb0a(bus, reason):
    print('cb0a')
    if reason == 0:
        print('pending')
    if reason == 1:
        print('full')
    if reason == 2:
        print('overflow')

def cb1a(bus, reason):
    print('cb1a')
    if reason == 0:
        print('pending')
    if reason == 1:
        print('full')
    if reason == 2:
        print('overflow')


can.rxcallback(0, cb0)
can.rxcallback(1, cb1)

99
100
101
can.send('11111111',1, timeout=5000)
can.send('22222222',2, timeout=5000)
can.send('33333333',3, timeout=5000)
102
can.rxcallback(0, cb0a)
103
can.send('44444444',4, timeout=5000)
104

105
106
107
can.send('55555555',5, timeout=5000)
can.send('66666666',6, timeout=5000)
can.send('77777777',7, timeout=5000)
108
can.rxcallback(1, cb1a)
109
can.send('88888888',8, timeout=5000)
110
111
112
113
114
115
116
117

print(can.recv(0))
print(can.recv(0))
print(can.recv(0))
print(can.recv(1))
print(can.recv(1))
print(can.recv(1))

118
119
can.send('11111111',1, timeout=5000)
can.send('55555555',5, timeout=5000)
120
121
122
123

print(can.recv(0))
print(can.recv(1))

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
del can

# Testing asyncronous send
can = CAN(1, CAN.LOOPBACK)
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))

while can.any(0):
    can.recv(0)

can.send('abcde', 1, timeout=0)
print(can.any(0))
while not can.any(0):
    pass

print(can.recv(0))

try:
    can.send('abcde', 2, timeout=0)
    can.send('abcde', 3, timeout=0)
    can.send('abcde', 4, timeout=0)
    can.send('abcde', 5, timeout=0)
except OSError as e:
    if str(e) == '16':
        print('passed')
    else:
        print('failed')

pyb.delay(500)
while can.any(0):
    print(can.recv(0))
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

# Testing rtr messages
bus1 = CAN(1, CAN.LOOPBACK)
bus2 = CAN(2, CAN.LOOPBACK, extframe = True)
while bus1.any(0):
    bus1.recv(0)
while bus2.any(0):
    bus2.recv(0)
bus1.setfilter(0, CAN.LIST16, 0, (1, 2, 3, 4))
bus1.setfilter(1, CAN.LIST16, 0, (5, 6, 7, 8), rtr=(True, True, True, True))
bus1.setfilter(2, CAN.MASK16, 0, (64, 64, 32, 32), rtr=(False, True))
bus2.setfilter(0, CAN.LIST32, 0, (1, 2), rtr=(True, True))
bus2.setfilter(1, CAN.LIST32, 0, (3, 4), rtr=(True, False))
bus2.setfilter(2, CAN.MASK32, 0, (16, 16), rtr=(False,))
bus2.setfilter(2, CAN.MASK32, 0, (32, 32), rtr=(True,))

bus1.send('',1,rtr=True)
print(bus1.any(0))
bus1.send('',5,rtr=True)
print(bus1.recv(0))
bus1.send('',6,rtr=True)
print(bus1.recv(0))
bus1.send('',7,rtr=True)
print(bus1.recv(0))
bus1.send('',16,rtr=True)
print(bus1.any(0))
bus1.send('',32,rtr=True)
print(bus1.recv(0))

bus2.send('',1,rtr=True)
print(bus2.recv(0))
bus2.send('',2,rtr=True)
print(bus2.recv(0))
bus2.send('',3,rtr=True)
print(bus2.recv(0))
bus2.send('',4,rtr=True)
print(bus2.any(0))