Repository navigation
Expand file tree
/
Copy pathsend_message.py
More file actions
65 lines (50 loc) · 1.71 KB
/
Copy pathsend_message.py
File metadata and controls
65 lines (50 loc) · 1.71 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
#!/usr/bin/env python
"""
This example shows how sending a single message works.
"""
import time
import can.interfaces.slcan
def send_one():
"""Sends a single message."""
slcan_device = '/dev/tty.usbserial-58971291291'
# this uses the default configuration (for example from the config file)
# see https://python-can.readthedocs.io/en/stable/configuration.html
bus = can.interfaces.slcan.slcanBus(channel=slcan_device,
ttyBaudrate=115200,
rtscts=True,
bitrate=1000000)
# print(bus.serialPortOrig.getBaudRate())
time.sleep(1)
bus.serialPortOrig.setRTS(False)
bus.serialPortOrig.setDTR(False)
time.sleep(1)
bus.serialPortOrig.write(b"V\r")
time.sleep(1)
bus.serialPortOrig.write(b"V\r")
time.sleep(1)
bus.serialPortOrig.write(b"V\r")
time.sleep(1)
bus.serialPortOrig.write(b"V\r")
time.sleep(1)
print(bus.get_serial_number(1))
time.sleep(1)
msg = can.Message(arbitration_id=0x141, data=[0xAA, 0xBB, 0xCC])
try:
print("Listening for CAN messages on", slcan_device)
while True:
try:
bus.send(msg)
print(f"Message sent on {bus.channel_info}")
except can.CanError:
print("Message NOT sent")
# Read a message from the CAN bus
message = bus.recv()
if message is not None:
print(f"Received: {message}")
time.sleep(1)
except KeyboardInterrupt:
print("Stopped by user")
except can.CanError as e:
print(f"CAN error: {e}")
if __name__ == "__main__":
send_one()