python-can
python-can copied to clipboard
interface for changing bitrate
Quick implementation of changing the bitrate at runtime. I just want to get a feel if this is the interface we would like or if there is something better.
I just implemented the Vector interface for now but I can add socketcan eventually (would close #549 ).
This might be a start for implementing autobaud as well.
TODO
- need to add error handling
Codecov Report
Merging #1030 (48b8f8c) into develop (7389b23) will decrease coverage by
2.12%
. The diff coverage is39.21%
.
@@ Coverage Diff @@
## develop #1030 +/- ##
===========================================
- Coverage 66.51% 64.38% -2.13%
===========================================
Files 78 79 +1
Lines 7561 7651 +90
===========================================
- Hits 5029 4926 -103
- Misses 2532 2725 +193
Wouldn't it be nice to implement a more generic change_parameters()
method that takes any parameters (just like on bus init)? The method can then complain whether that specific setting is supported to be changed or not.
Your proposal for implementing it as a property looks really nice! However, as a change to the core class of the library this should get more feedback.
(Side note: This could also be used in other places since it modularizes the configuration of the bus. Sometimes, that might make things more readable.)
Some questions:
- How would you deal with CAN FD?
- Regarding the Vector interface: what happens if you do not have Init Access?
- How would you deal with CAN FD?
I guess we would not have to implement it right away. Besides, wouldn't the bitrate be handled automatically?
- Regarding the Vector interface: what happens if you do not have Init Access?
I'd propose to simply throw one of the new exceptions, like a CanOperationError
.
- How would you deal with CAN FD?
I guess we would not have to implement it right away. Besides, wouldn't the bitrate be handled automatically?
This will only set the arbitration bitrate. Would probably need a separate property for the data bitrate.
- Regarding the Vector interface: what happens if you do not have Init Access?
I'd propose to simply throw one of the new exceptions, like a
CanOperationError
.
Check out the latest commit. I think it should handle these issues. I still need to add unit tests but testing from home this all works fine. I'll need to test on my work setup too next week.
@hartkopp and @lumagi : What do you think about the socketcan part of this PR? Is that a viable solution to implement bit timing configuration for socketcan?
@hartkopp and @lumagi : What do you think about the socketcan part of this PR? Is that a viable solution to implement bit timing configuration for socketcan?
The bitrate setting of 'real' CAN interfaces can only be performed as network-admin (aka root) on Linux. That's why the ip
tool is mostly used with sudo ip
...
And the setting affects all users on the system (not only python-can users), as the interface has to be shut down to change the bitrates (and has to be set to "up" afterwards). Shutting down the interfaces leads to -ENETDOWN return values for all user space applications working on that interface. Additionally the python-can application needs to be run as root to manipulate the interface status and the bitrate.
So reading the interface properties via ip
tool or the netlink socket is fine - but setting new bitrates from python-can is probably not as feasible as suggested by the patch.
@hartkopp Thank you for your input. It's always neat to have the expert here 👍
It also seems like libsocketcan
is not maintained anymore. The last commit is from 2020 and it does not support CAN FD. So it's probably not a good foundation to build upon.
It also seems like
libsocketcan
is not maintained anymore. The last commit is from 2020 and it does not support CAN FD. So it's probably not a good foundation to build upon.
@marckleinebudde : Is there any ongoing activity planned for libsocketcan
?
I agree with @hartkopp. The executing process must be root or at least have the NET_ADMIN
capability to change the interface. Neither one of those things is something you want to do to your Python interpreter on a regular basis.
There is no immediate activity planned for libsocketcan
, but patches are welcome. However I'd rather have a proper (dbus?) interface for systemd-networkd
to configure CAN (and networking in general).