-
Notifications
You must be signed in to change notification settings - Fork 12
/
demo.py
73 lines (62 loc) · 1.98 KB
/
demo.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
65
66
67
68
69
70
71
72
73
import time
from gs_usb.gs_usb import GsUsb
from gs_usb.gs_usb_frame import GsUsbFrame
from gs_usb.constants import (
CAN_EFF_FLAG,
CAN_ERR_FLAG,
CAN_RTR_FLAG,
)
def main():
# Find our device
devs = GsUsb.scan()
if len(devs) == 0:
print("Can not find gs_usb device")
return
dev = devs[0]
# Configuration
if not dev.set_bitrate(250000):
print("Can not set bitrate for gs_usb")
return
# Start device
dev.start()
# Prepare frames
data = b"\x12\x34\x56\x78\x9A\xBC\xDE\xF0"
sff_frame = GsUsbFrame(can_id=0x7FF, data=data)
sff_frame2 = GsUsbFrame(can_id=0x270, data=[0x00, 0x02, 0x4f, 0x55])
sff_frame3 = GsUsbFrame(can_id=0x350, data=[0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa])
sff_none_data_frame = GsUsbFrame(can_id=0x7FF)
err_frame = GsUsbFrame(can_id=0x7FF | CAN_ERR_FLAG, data=data)
eff_frame = GsUsbFrame(can_id=0x12345678 | CAN_EFF_FLAG, data=data)
eff_none_data_frame = GsUsbFrame(can_id=0x12345678 | CAN_EFF_FLAG)
rtr_frame = GsUsbFrame(can_id=0x7FF | CAN_RTR_FLAG)
rtr_with_eid_frame = GsUsbFrame(can_id=0x12345678 | CAN_RTR_FLAG | CAN_EFF_FLAG)
rtr_with_data_frame = GsUsbFrame(can_id=0x7FF | CAN_RTR_FLAG, data=data)
frames = [
sff_frame,
sff_frame2,
sff_frame3,
sff_none_data_frame,
err_frame,
eff_frame,
eff_none_data_frame,
rtr_frame,
rtr_with_eid_frame,
rtr_with_data_frame,
]
# Read all the time and send message in each second
end_time, n = time.time() + 1, -1
while True:
iframe = GsUsbFrame()
if dev.read(iframe, 1):
print("RX {}".format(iframe))
if time.time() - end_time >= 0:
end_time = time.time() + 1
n += 1
n %= len(frames)
if dev.send(frames[n]):
print("TX {}".format(frames[n]))
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass