When writing “Was that the CANlib channel number or Card Number?“, I got a comment asking for clarification about the difference between the CanHandle and channel number since they can both start at zero.
A CAN channel is opened using the function canOpenChannel()
in CANlib, passing the channel number as the first argument.
This will return a handle that can be any non-negative number (and it is often zero the first time you call it). The handle is used later e.g. when reading the next available CAN messages. If the call to canOpenChannel()
fails, a negative error code will be returned instead of the valid handle.
The handle is an internal positive number that should never be edited manually. As mentioned, the CanHandle
returned in CANlib could be mixed up with the channel number but later libraries, such as kvrlib, returns a much larger, more random, number in order to minimize the risk of confusion.
I suggest to get into the habit of always declaring the handle as a CanHandle
(instead of int) and thereby clearly marking it a handle. While making suggestions, may I also encourage you to always check the return status as we did on canRead()
above? This will shorten the debug time when things start to fail…