A Kvaser CAN interface connected to a computer (indicated by the green light for PWR) receiving an error on its second channel (indicated by the red light flashing on CAN 2).
To receive an error within the interpreter we need to send more messages at once. To do this we will surround the write() command with a for-loop (still using the Python interpreter within powershell):
> .\.venv\Scripts\activate
(pyproj) PS C:\Users\extac\Pyproj> py
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from canlib import canlib, Frame
>>> ch = canlib.openChannel(channel=1)
>>> ch.setBusOutputControl(canlib.Driver.SILENT)
>>> ch.busOn()
>>> frame = Frame(id_=123, data=[72, 69, 76, 76, 79, 33])
>>> for i in range(2000):
... ch.write(frame)
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "C:\Users\extac\Pyproj\.venv\lib\site-packages\canlib\canlib\channel.py", line 718, in write
dll.canWrite(self.handle, frame.id, bytes(frame.data), frame.dlc, frame.flags)
File "C:\Users\extac\Pyproj\.venv\lib\site-packages\canlib\canlib\dll.py", line 177, in _error_check
raise can_error(result)
canlib.canlib.exceptions.CanGeneralError: Transmit buffer overflow (-13)
Once again, remember to go off the bus and close the channel when finished.
>>> ch.busOff()
>>> ch.close()
For the next example we need to add a third channel to be able to send a message as well as reading it with a silent channel. Note that to be able to have a silent channel, there must be at least two other channels that can send and receive the message normally. In this guide we will add a Kvaser Leaf Pro HS v2, but any other CAN interface with at least one channel will do. To see that more than two channels are available, run the script check_ch once more, which for this example results in:
(pyproj)> py check_ch.py
Found 5 channels
0. Kvaser USBcan Pro 2xHS v2 (channel 0) (00752-9:13406/0)
1. Kvaser USBcan Pro 2xHS v2 (channel 1) (00752-9:13406/1)
2. Kvaser Leaf Pro HS v2 (channel 0) (00843-4:10012/0)
3. Kvaser Virtual CAN Driver (channel 0) (00000-0:0/0)
4. Kvaser Virtual CAN Driver (channel 1) (00000-0:0/1)
Compared to the previous time we ran check_ch, the virtual channels have dropped down from 2 and 3 to 3 and 4. The CAN channel 2 has been taken over by the new Kvaser Leaf Pro CAN interface that was added.
The next step is to send a message with two normal channels and listen with a third silent channel. Once again we will go back to the CAN message example and add code, this script will be called silent_listen:
# silent_listen
from canlib import canlib, Frame
# Open a third channel (channel 2) and name it ch_c.
ch_a = canlib.openChannel(channel=0)
ch_b = canlib.openChannel(channel=1)
ch_c = canlib.openChannel(channel=2)
# Set ch_a and ch_b to normal, again unnecessary but to clarify,
ch_a.setBusOutputControl(canlib.Driver.NORMAL)
ch_b.setBusOutputControl(canlib.Driver.NORMAL)
# and ch_c to silent.
if canlib.ChannelCap.SILENT_MODE in ch_c.channel_data.channel_cap:
ch_c.setBusOutputControl(canlib.Driver.SILENT)
else:
exit()
# Put the third channel ch_c on the bus.
ch_a.busOn()
ch_b.busOn()
ch_c.busOn()
frame = Frame(id_=123, data=[72, 69, 76, 76, 79, 33])
ch_a.write(frame)
# Add ch_c to read the message as the silent channel to read the message.
msg_c = ch_c.read(timeout=500)
msg_b = ch_b.read(timeout=500)
# Print both messages to compare them.
print("msg c:”)
print(msg_c)
print("msg b:”)
print(msg_b)
# Go off bus with all three channels.
ch_a.busOff()
ch_b.busOff()
ch_c.busOff()
# Lastly, close all channels.
ch_a.close()
ch_b.close()
ch_c.close()
Running the script in powershell will result in the following:
(pyproj)> py silent_listen.py
msg c:
Frame(id=123, data=bytearray(b'HELLO!'), dlc=6, flags=<MessageFlag.STD: 2>, timestamp=2)
msg b:
Frame(id=123, data=bytearray(b'HELLO!'), dlc=6, flags=<MessageFlag.STD: 2>, timestamp=5)
We can now see that the silent channel ch_c can read the message, and with ch_b also reading the message it no longer becomes an error message. In this example the silent channel is not necessary but it is still an example on how we can use it.