Skip to content

Commit 13533a5

Browse files
committed
move to c api to speedup transition performance
1 parent 897a5c8 commit 13533a5

9 files changed

Lines changed: 330 additions & 1395 deletions

File tree

README.md

Lines changed: 25 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,16 @@
88

99
</div>
1010

11-
CAN driver for Geschwister Schneider USB/CAN devices.
11+
Full featured CAN driver for Geschwister Schneider USB/CAN devices.
1212

13-
## Feature
14-
15-
Support **multichannel** and **can fd**.
13+
Support **Multichannel** and **CAN FD**.
1614

1715
## Installation
1816

1917
```shell
2018
pip install python-can-candle
2119
```
2220

23-
If your system does not have any usb backend installed, you can borrow a libusb1 backend from [libusb](https://pypi.org/project/libusb/).
24-
25-
```shell
26-
pip install python-can-candle[libusb]
27-
```
28-
2921
We also provide a handy GUI tool, which you can install with the following command.
3022

3123
```shell
@@ -36,29 +28,16 @@ Once installed you can launch it via `candle_viewer` or `python -m candle.candle
3628

3729
![Candle Viewer](https://raw.githubusercontent.com/BIRLab/python-can-candle/main/assets/viewer.png)
3830

39-
(unstable) Installing a backend written in C can improve performance.
40-
41-
```shell
42-
pip install "candle_api @ git+https://github.com/BIRLab/candle_api.git@main"
43-
```
44-
4531
## Example
4632

47-
### Using with python-can (recommended)
48-
49-
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
5034

5135
```python
5236
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-
assert isinstance(bus, CandleBus)
37+
from candle.candle_bus import CandleBus
6138

39+
# Create a CandleBus instance.
40+
with CandleBus(channel=0, fd=True, bitrate=1000000, data_bitrate=5000000) as bus:
6241
# Send normal can message without data.
6342
bus.send(can.Message(arbitration_id=1, is_extended_id=False))
6443

@@ -69,92 +48,38 @@ with can.Bus(interface='candle', channel=0, fd=True, bitrate=1000000, data_bitra
6948
bus.send(can.Message(arbitration_id=3, is_extended_id=False, data=[i for i in range(8)]))
7049

7150
# Send can fd message.
72-
if bus.is_fd_supported:
73-
bus.send(can.Message(arbitration_id=4, is_extended_id=False, is_fd=True, bitrate_switch=True,
74-
error_state_indicator=True, data=[i for i in range(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 in range(64)]))
7552

7653
# Read messages from bus.
7754
for message in bus:
7855
print(message)
7956
```
8057

81-
### Using candle-api directly
58+
### Using with python-can
8259

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.
8461

8562
```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 in enumerate(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 in range(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 in range(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 in range(64)])
137-
)
138-
)
139-
140-
try:
141-
while True:
142-
# Polling message.
143-
device.polling()
144-
145-
# Receive a frame.
146-
frame = channel.read()
147-
if frame is not None:
148-
print(frame.data)
149-
except KeyboardInterrupt:
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+
assert isinstance(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.
75+
76+
```shell
77+
python -m candle.stress
15478
```
15579

15680
## Reference
15781

15882
- [linux gs_usb driver](https://github.com/torvalds/linux/blob/master/drivers/net/can/usb/gs_usb.c)
15983
- [python gs_usb driver](https://github.com/jxltom/gs_usb)
16084
- [candleLight firmware](https://github.com/candle-usb/candleLight_fw)
85+
- [candle_api](https://github.com/BIRLab/candle_api)

0 commit comments

Comments
 (0)