-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
75 lines (55 loc) · 2.59 KB
/
test.py
File metadata and controls
75 lines (55 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
##############################################################################
## Description : This codes is for test two USB2CAN module commuincation
## on one Raspberry Pi.
## CAN0 send "0x55, 0xaa, 0x5a,0xa5" to CAN1
## After that, CAN1 will "0x55, 0xaa, 0x5a,0xa5" to CAN0
## Finally, Print the test result
## Author : Calvin (calvin@inno-maker.com)/ www.inno-maker.com
## Date : 2019.11.30
## Environment : Hardware ---------------------- Raspberry Pi 4
## SYstem of RPI ---------------------- 2019-09-26-raspbian-buster-full.img
## Version of Python ---------------------- Python 3.7.3(Default in the system)
## Toinstall dependencies:
## sudo pip install python-can
###############################################################################
import os
import can
#Set CAN0 speed to 1M bps
os.system('sudo ip link set can0 type can bitrate 1000000')
os.system('sudo ifconfig can0 up')
#Set CAN1 speed to 1M bps
os.system('sudo ip link set can1 type can bitrate 1000000')
os.system('sudo ifconfig can1 up')
can0 = can.interface.Bus(channel = 'can0', bustype = 'socketcan')
can1 = can.interface.Bus(channel = 'can1', bustype = 'socketcan')
def rs485_enable():
## CAN0 SEND AND CAN1 RECEIVE
msg = can.Message(arbitration_id=0x123, data=[0x55,0xaa,0x5a,0xa5],
extended_id=False)
can0.send(msg)
msg = can1.recv(10.0)
if((msg.data[0]==0x55)and(msg.data[1]==0xaa)and
(msg.data[2]==0x5a)and(msg.data[3]==0xa5)):
## CAN1 SEND AND CAN0 RECEIVE
msg = can.Message(arbitration_id=0x123, data=[0x55,0xaa,0x5a,0xa5],
extended_id=False)
print(msg)
can1.send(msg)
msg = can0.recv(30.0)
if((msg.data[0]==0x55)and(msg.data[1]==0xaa)and(msg.data[2]==0x5a)and(msg.data[3]==0xa5)):
print(msg)
print('Both USB2CAN module communication test successful')
elif msg is None: ## No data
print('Timeout USB2CAN module test failure.\r\n'*3)
else :
print('Data validation errors\r\n'*3)
print(msg) ##print the err data
elif msg is None: ## No data
print('Timeout USB2CAN module test failure.\r\n'*3)
else : ##print the err data
print('Data validation errors\r\n'*3)
print(msg)
if __name__ == '__main__':
rs485_enable()
os.system('sudo ifconfig can1 down')
os.system('sudo ifconfig can0 down')