forked from skpang/PiCAN-Python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_tx_test.py
64 lines (52 loc) · 1.31 KB
/
simple_tx_test.py
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
#!/usr/bin/python3
#
# simple_tx_test.py
#
# This python3 sent CAN messages out, with byte 7 increamenting each time.
# For use with PiCAN boards on the Raspberry Pi
# http://skpang.co.uk/catalog/pican2-canbus-board-for-raspberry-pi-2-p-1475.html
#
# Make sure Python-CAN is installed first http://skpang.co.uk/blog/archives/1220
#
# 01-02-16 SK Pang
#
#
#
import RPi.GPIO as GPIO
import can
import time
import os
led = 22
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(led,GPIO.OUT)
GPIO.output(led,True)
count = 0
print('\n\rCAN Rx test')
print('Bring up CAN0....')
# Bring up can0 interface at 500kbps
os.system("sudo /sbin/ip link set can0 up type can bitrate 500000")
time.sleep(0.1)
print('Press CTL-C to exit')
try:
bus = can.interface.Bus(channel='can0', bustype='socketcan_native')
except OSError:
print('Cannot find PiCAN board.')
GPIO.output(led,False)
exit()
# Main loop
try:
while True:
GPIO.output(led,True)
msg = can.Message(arbitration_id=0x7de,data=[0x00,0x01,0x02, 0x03, 0x04, 0x05,0x06, count & 0xff],extended_id=False)
bus.send(msg)
count +=1
time.sleep(0.1)
GPIO.output(led,False)
time.sleep(0.1)
print(count)
except KeyboardInterrupt:
#Catch keyboard interrupt
GPIO.output(led,False)
os.system("sudo /sbin/ip link set can0 down")
print('\n\rKeyboard interrtupt')