You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library implements the [plugin interface](https://python-can.readthedocs.io/en/stable/plugin-interface.html) in [python-can](https://pypi.org/project/python-can/), aiming to replace the [gs_usb](https://python-can.readthedocs.io/en/stable/interfaces/gs_usb.html) interface within it.
33
+
### Send and Receive
50
34
51
35
```python
52
36
import can
53
-
from candle import CandleBus
54
-
55
-
bus: CandleBus # This line is added to provide type hints.
56
-
57
-
# Create a CandleBus instance in the python-can API.
58
-
with can.Bus(interface='candle', channel=0, fd=True, bitrate=1000000, data_bitrate=5000000) as bus:
59
-
# Note that bus is an instance of CandleBus.
60
-
assertisinstance(bus, CandleBus)
37
+
from candle.candle_bus import CandleBus
61
38
39
+
# Create a CandleBus instance.
40
+
with CandleBus(channel=0, fd=True, bitrate=1000000, data_bitrate=5000000) as bus:
error_state_indicator=True, data=[i for i inrange(64)]))
51
+
bus.send(can.Message(arbitration_id=4, is_extended_id=False, is_fd=True, bitrate_switch=True, error_state_indicator=True, data=[i for i inrange(64)]))
75
52
76
53
# Read messages from bus.
77
54
for message in bus:
78
55
print(message)
79
56
```
80
57
81
-
### Using candle-api directly
58
+
### Using with python-can
82
59
83
-
Using the API directly can be very cumbersome. However, we still provide a simple example for developers to refer to.
60
+
This library implements the [plugin interface](https://python-can.readthedocs.io/en/stable/plugin-interface.html) in [python-can](https://pypi.org/project/python-can/), aiming to replace the [gs_usb](https://python-can.readthedocs.io/en/stable/interfaces/gs_usb.html) interface within it.
84
61
85
62
```python
86
-
from candle.candle_api import CandleDevice, GSHostFrame, GSHostFrameHeader, GSCANFlag, GSCANIDFlag
87
-
88
-
# Scan available devices.
89
-
available_devices =list(CandleDevice.scan())
90
-
91
-
# Select a device.
92
-
for i, device inenumerate(available_devices):
93
-
print(f'{i}: {device}')
94
-
device = available_devices[int(input('Select a device by index: '))]
95
-
96
-
# Select a interface.
97
-
# Only single interface devices are supported currently.
98
-
interface = device[0]
99
-
100
-
# Select a channel.
101
-
for i inrange(len(interface)):
102
-
print(f'{i}: channel {i}')
103
-
channel = interface[int(input(f'Select a channel by index: '))]
104
-
105
-
# Set bit timing.
106
-
# channel.set_bit_timing(...)
107
-
# channel.set_data_bit_timing(...)
108
-
109
-
# Open the channel.
110
-
channel.open(fd=channel.is_fd_supported)
111
-
112
-
# Send a normal frame.
113
-
channel.write(
114
-
GSHostFrame(
115
-
header=GSHostFrameHeader(
116
-
echo_id=0,
117
-
can_id=1,
118
-
can_dlc=8,
119
-
channel=channel.index,
120
-
flags=GSCANFlag(0)
121
-
),
122
-
data=bytes([i for i inrange(8)])
123
-
)
124
-
)
125
-
126
-
# Send a can fd frame with extended id.
127
-
channel.write(
128
-
GSHostFrame(
129
-
header=GSHostFrameHeader(
130
-
echo_id=0,
131
-
can_id=1| GSCANIDFlag.EFF,
132
-
can_dlc=15,
133
-
channel=channel.index,
134
-
flags=GSCANFlag.FD| GSCANFlag.BRS| GSCANFlag.ESI
135
-
),
136
-
data=bytes([i for i inrange(64)])
137
-
)
138
-
)
139
-
140
-
try:
141
-
whileTrue:
142
-
# Polling message.
143
-
device.polling()
144
-
145
-
# Receive a frame.
146
-
frame = channel.read()
147
-
if frame isnotNone:
148
-
print(frame.data)
149
-
exceptKeyboardInterrupt:
150
-
pass
151
-
152
-
# Close the channel.
153
-
channel.close()
63
+
import can
64
+
from candle.candle_bus import CandleBus
65
+
66
+
# Create a CandleBus instance in the python-can API.
67
+
with can.Bus(interface='candle', channel=0, fd=True, bitrate=1000000, data_bitrate=5000000, ignore_config=True) as bus:
68
+
# Bus is an instance of CandleBus.
69
+
assertisinstance(bus, CandleBus)
70
+
```
71
+
72
+
### Performance
73
+
74
+
The communication layer is implemented based on pybind11 with libusb. You can run the following script to evaluate the performance.
0 commit comments