This is the second of two articles that introduce Kvaser CANlib and the Python CANlib Wrapper.
In this article, we will introduce some typical scenarios involving CAN, learn about different commands and functions while using CAN and gain a wider understanding of CAN, canlib and Kvaser Interfaces.
Writer: Anton Carlsson, Software Developer
Cowriter: L-G Fredriksson, Field Application Engineer
Version: 2021-08-09A
[/vc_column_text][/vc_column][/vc_row][vc_row css=".vc_custom_1649371866326{margin-top: 25px !important;}"][vc_column][vc_header_raket header_type="h4" header="Who should read this?"][vc_column_text]This guide has been written for those who want to set up and use a Kvaser CAN interface with CANlib and the Python CANlib package/wrapper.
To use this guide, the user needs to be somewhat familiar with (or able to look up) the following:
[/vc_column_text][vc_column_text]Please read the paper: Kvaser CANlib & Python Part 1, Initial setup
Please note: Some of the samples are written for three interfaces.
[/vc_column_text][vc_raw_html]JTNDYnIlMjAlMkYlM0U=[/vc_raw_html][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="Quick start procedure, reminder"][vc_column_text]If you have taken a break or just forgotten the used commands/procedure, please use this little list of powerful commands and instructions.
[/vc_column_text][vc_column_text]powershell.exe
cd
(if you already have a virtual environment and permission to run it, skip the next two steps)py -3 -m venv .venv --prompt.
Set-ExecutionPolicy Unrestricted -Scope CurrentUser
.\.venv\Scripts\activate
pip install canlib
py check_ch.py
”(check_ch.py is always good to run once to make sure the wanted interfaces are connected)python -m idlelib
python -m idlelib check_ch.py
deactivate
(This information is a copy from the paper:Kvaser CANlib & Python Part 1, First setup)
[/vc_column_text][/vc_column][/vc_row][vc_row css=".vc_custom_1649377624425{margin-top: 25px !important;}"][vc_column][vc_header_raket header_type="h3" header="Simple approach"][vc_column_text]To send a basic CAN message, create a script named send_msg.py containing the following code (make sure that the script is located within the same folder as the virtual environment):
[/vc_column_text][vc_code_raket language="python" code="JTIzc2VuZF9tc2clMEElMjNUaGUlMjBDQU5saWIlMjBsaWJyYXJ5JTIwaXMlMjBpbml0aWFsaXplZCUyMHdoZW4lMjB0aGUlMjBjYW5saWIlMjBtb2R1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JTIwd2VudCUyMC5idXNPbiUyOCUyOSUwQWNoX2EuYnVzT2ZmJTI4JTI5JTBBY2hfYi5idXNPZmYlMjglMjklRTIlODAlQTglMEElMjMlMjBMYXN0bHklMkMlMjBjbG9zZSUyMGFsbCUyMGNoYW5uZWxzJTIwd2l0aCUyMGNsb3NlJTI4JTI5JTIwdG8lMjBmaW5pc2glMjB1cC4lMEFjaF9hLmNsb3NlJTI4JTI5JTBBY2hfYi5jbG9zZSUyOCUyOSUwQSUwQSUyMyUyMERlcGVuZGluZyUyMG9uJTIwdGhlJTIwc2l0dWF0aW9uJTIwaXQlMjBpcyUyMG5vdCUyMGFsd2F5cyUyMG5lY2Vzc2FyeSUyMG9yJTIwcHJlZmVyYWJsZSUyMHRvJTIwZ28lMjBvZiUwQSUyMyUyMHRoZSUyMGJ1cyUyMHdpdGglMjB0aGUlMjBjaGFubmVscyUyMGFuZCUyQyUyMGluc3RlYWQlMjBvbmx5JTIwdXNlJTIwY2xvc2UlMjglMjkuJTIwQnV0JTIwdGhpcyUyMHdpbGwlMjBiZSUwQSUyMyUyMHRhbGtlZCUyMG1vcmUlMjBhYm91dCUyMGxhdGVyLg=="][vc_column_text]To run the previous program, activate your virtual environment and run the script using powershell. Running the program will result in something like the following:
[/vc_column_text][vc_code_raket language="t" code="JTNFJTIwLiU1Qy52ZW52JTVDU2NyaXB0cyU1Q2FjdGl2YXRlJTBBJTI4cHlwcm9qJTI5JTNFJTIwcHklMjBzZW5kX21zZy5weSUwQUZyYW1lJTI4aWQlM0QxMjMlMkMlMjBkYXRhJTNEYnl0ZWFycmF5JTI4YiUyN0hFTExPJTIxJTI3JTI5JTJDJTIwZGxjJTNENiUyQyUyMGZsYWdzJTNEJTNDTWVzc2FnZUZsYWcuU1REJTNBJTIwMiUzRSUyQyUyMHRpbWVzdGFtcCUzRDklMjk="][/vc_column][/vc_row][vc_row css=".vc_custom_1649377610352{margin-top: 25px !important;}"][vc_column][vc_header_raket header_type="h3" header="Simple approach using virtual interfaces"][vc_column_text]If we want to use the virtual interface and the virtual channels, we need to change the openchannel commands. Firstly we need to change the numbers from 0 and 1 to 2 and 3 to represent the virtual channels. Then we need to add a flag (another input) that says ACCEPT_VIRTUAL to define that a virtual channel will be used and accepted. The command will now look like:
[/vc_column_text][vc_code_raket language="t" code="JTA5Y2FubGliLm9wZW5DaGFubmVsJTI4Y2hhbm5lbCUzRDIlMkMlMjBmbGFncyUzRGNhbmxpYi5vcGVuLkFDQ0VQVF9WSVJUVUFMJTI5"][vc_column_text]If we do not add the ACCEPT_VIRTUAL flag we will receive a Specified device not found (-3). It is also important to note that a virtual channel can only communicate with another virtual channel.
[/vc_column_text][/vc_column][/vc_row][vc_row css=".vc_custom_1649377601710{margin-top: 25px !important;}"][vc_column][vc_header_raket header_type="h3" header="A more Pythonic way"][vc_column_text]The code used in send a CAN message is a very standard and straightforward way to write the script we want. Using Python however we can write the code in a more “Pythonic way”. This will lead to the following script names send_msg_pyt.py:
[/vc_column_text][vc_code_raket language="python" code="JTIzJTIwc2VuZF9tc2dfcHl0JTBBZnJvbSUyMGNhbmxpYiUyMGltcG9ydCUyMGNhbmxpYiUyQyUyMEZyYW1lJTBBJTBBJTIzJTIwaW5zdGVhZCUyMG9mJTIwb3BlbmluZyUyMHRoZSUyMHR3byUyMGNoYW5uZWxzJTIwYW5kJTIwY2xvc2luZyUyMHRoZW0lMjBvbmUlMjBieSUyMG9uZSUyQyUyMHdlJTIwd2lsbCUyMHVzZSUyMGElMEElMjMlMjB3aXRoJTIwc3RhdGVtZW50LiUyMFVzaW5nJTIwdGhlJTIwd2l0aCUyMHN0YXRlbWVudCUyMHRvJTIwb3BlbiUyMG9uZSUyMG9yJTIwbW9yZSUyMGNoYW5uZWxzJTIwd2l0aCUwQSUyMyUyMGNhbmxpYi5vcGVuQ2hhbm5lbCUyOGklMjklMjBhcyUyMGNoX3guJTIwV2l0aGluJTIwdGhpcyUyMHdpdGglMjBzdGF0ZW1lbnQlMjB3ZSUyMHdpbGwlMj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lNUI3MiUyQyUyMDY5JTJDJTIwNzYlMkMlMjA3NiUyQyUyMDc5JTJDJTIwMzMlNUQlMjklMEElMjAlMjAlMjAlMjBjaF9hLndyaXRlJTI4ZnJhbWUlMjklMEElMEElMjAlMjAlMjAlMjBtc2clMjAlM0QlMjBjaF9iLnJlYWQlMjh0aW1lb3V0JTNENTAwJTI5JTBBJTIwJTIwJTIwJTIwcHJpbnQlMjhtc2clMjklMEElMEElMjMlMjBBZnRlciUyMHdlJTIwcnVuJTIwb3V0JTIwb2YlMjBjb2RlJTIwd2l0aGluJTIwdGhlJTIwd2l0aCUyMHN0YXRlbWVudCUyMGFuZCUyMGV4aXQlMjBpdCUyQyUyMHdlJTIwZG9uJUUyJTgwJTk5dCUwQSUyMyUyMG5lZWQlMjB0byUyMG1hbnVhbGx5JTIwY2xvc2UlMjBpdCUyMG9yJTIwZ28lMjBvZmYlMjBidXMuJTIwVGhlJTIwY2hhbm5lbHMlMjB0aGF0JTIwd2VyZSUyMG9wZW4lMjB1c2luZyUwQSUyMyUyMHRoZSUyMHdpdGglMjBzdGF0ZW1lbnQlMjB3aWxsJTIwYmUlMjBhdXRvbWF0aWNhbGx5JTIwY2xvc2VkJTJDJTIwYW5kJTIwd2l0aCUyMHRoZSUyMGNoYW5uZWxzJTIwYmVpbmclMEElMjMlMjBjbG9zZWQlMjB0aGV5JTIwYWxzbyUyMHdlbnQlMjBvZmYlMjB0aGUlMjBidXMu"][/vc_column][/vc_row][vc_row css=".vc_custom_1649377592106{margin-top: 25px !important;}"][vc_column][vc_header_raket header_type="h3" header="Wait"][vc_column_text]While writing several messages, we might want to confirm that they were received properly. To do this we will make use of the command writeWait(). writeWait does the same as write when it comes to sending a message, but it also gives a timeout limit for how long it will wait for the message to be sent. The command may look like writeWait(frame, timeout=500), this will send a message with the details given by frame and wait 500 milliseconds for it to be sent before returning an error message Timeout occurred (-7). WriteWait can be used when sending one or multiple messages. When sending several messages we can use writeWait on every message, but this will take time to execute and is not very efficient. Instead we can use write on all messages, apart from the last one which will be writeWait. If one message fails to be sent properly, all after will fail and the timeout error Timeout occured (-7) will be raised by writeWait.
To test writeWait, we will make two scripts called send_msg_wait.py and send_msgs_wait.py which will be based on the send message code but with some changes. The first script send_msg_wait.py will send a message using writeWait to send one successful message and one unsuccessful message:
[/vc_column_text][vc_code_raket language="python" code="JTIzJTIwc2VuZF9tc2dfd2FpdCUwQWZyb20lMjBjYW5saWIlMjBpbXBvcnQlMjBjYW5saWIlMkMlMjBGcmFtZSUwQSUwQSUyMyUyMFdlJTIwd2lsbCUyMG5vdyUyMG9wZW4lMjB0aHJlZSUyMGNoYW5uZWxzJTJDJTIwdHdvJTIwZnJvbSUyMHRoZSUyMFVTQmNhbiUyMGFuZCUyMG9uZSUyMG9uJTBBJTIzJTIwdGhlJTIwbGVhZiUyMHBybyUyMHdoaWNoJTIwd2UlMjB3aWxsJTIwbm90JTIwY29ubmVjdCUyMHRvJTIwdGhlJTIwVC1jYW5uZWN0b3IuJTIwV2UlMjB3aWxsJTIwdXNlJTIwdGhlJTBBJTIzJTIwbGVhZiUyMHBybyUyMGNoYW5uZWwlMjBjaF9jJTIwdG8lMjBzZW5kJTIwZXJyb3JmcmFtZXMuJTBBd2l0aCUyMGNhbmxpYi5vcGVuQ2hhbm5lbCUyODIlMjklMjBhcyUyMGNoX2ElMkMlMjBjYW5saWIub3BlbkNoYW5uZWwlMjgzJTI5JTIwYXMlMjBjaF9iJTJDJTIwY2FubGliLm9wZW5DaGFubmVsJTI4NCUyOSUyMGFzJTIwY2hfYyUzQSUwQSUyMCUyMCUyMCUyMGZvciUyMGNoJTIwaW4lMjAlNUJjaF9hJTJDJTIwY2hfYiUyQyUyMGNoX2MlNUQlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBjaC5idXNPbiUyOCUyOSUwQSUwQSUyMCUyMCUyMCUyMGZyYW1lJTIwJTNEJTIwRnJhbWUlMjhpZF8lM0QxMjMlMkMlMjBkYXRhJTNEJTVCNzIlMkMlMjA2OSUyQyUyMDc2JTJDJTIwNzYlMkMlMjA3OSUyQyUyMDMzJTVEJTI5JTBBJTIwJTIwJTIwJTIwJTBBJTIzJTIwSW5zdGVhZCUyMG9mJTIwdXNpbmclMjB3cml0ZSUyMHdlJTIwd2lsbCUyMG5vdyUyMHVzZSUyMHdyaXRlV2FpdCUyOCUyOS4lMjBXZSUyMHdpbGwlMjBhdHRlbXB0JTIwdG8lMjBzZW5kJTBBJTIzJTIwYSUyMG1lc3NhZ2UlMjBmb3IlMjA1MDAlMjBtaWxsaXNlY29uZHMlMkMlMjBpZiUyMHRoZSUyMG1lc3NhZ2UlMjBpcyUyMG5vdCUyMHNlbnQlMjB3ZSUyMHdpbGwlMjByZWNlaXZlJTIwYSUwQSUyMyUyMFRpbWVvdXQlMjBvY2N1cmVkJTIwZXJyb3IuJTBBJTIwJTIwJTIwJTIwY2hfYS53cml0ZVdhaXQlMjhmcmFtZSUyQyUyMHRpbWVvdXQlM0Q1MDAlMjklMEElMEElMjMlMjBXZSUyMHdpbGwlMjBub3clMjB0cnklMjB0byUyMHNlbmQlMjBhJTIwbWVzc2FnZSUyMHdpdGglMjB0aGUlMjBjaGFubmVsJTIwbm90JTIwY29ubmVjdGVkJTIwdG8lMjB0aGUlMEElMjMlMjBULWNhbm5lY3Rvci4lMjBUaGlzJTIwc2hvdWxkJTIwcmVzdWx0JTIwaW4lMjBhJTIwVGltZW91dCUyMG9jY3VycmVkJTIwZXJyb3IuJTBBJTIwJTIwJTIwJTIwY2hfYy53cml0ZVdhaXQlMjhmcmFtZSUyQyUyMHRpbWVvdXQlM0Q1MDAlMjklMEElMEElMjAlMjAlMjAlMjBtc2clMjAlM0QlMjBjaF9iLnJlYWQlMjh0aW1lb3V0JTNENTAwJTI5JTBBJTIwJTIwJTIwJTIwcHJpbnQlMjhtc2clMjk="][vc_column_text]In the next script send_msgs_wait-py we will send multiple messages and using waitWrite to make sure that the messages were sent:
[/vc_column_text][vc_code_raket language="python" code="JTIzJT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nQlMjhtc2clMjk="][vc_raw_html]JTNDYnIlMjAlMkYlM0U=[/vc_raw_html][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="Required hardware"][vc_column_text]For part two of this guide we will need at least three channels, so if the device we used for Part One does not have three channels, we need another device. In this guide a Kvaser Leaf Pro HS v2 will be used, in addition to the Kvaser USBcan Pro 2xHS v2.
[/vc_column_text][vc_raw_html]JTNDYnIlMjAlMkYlM0U=[/vc_raw_html][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="Silent mode"][vc_column_text]While receiving messages with CANlib using the CAN channels that are onbus (channels that have gone on bus with busOn), there are two possible modes to choose from. The two modes are Normal and Silent mode and are called bus modes. To set the bus driver mode use “Channel.setBusOutputControl”.
[/vc_column_text][/vc_column][/vc_row][vc_row css=".vc_custom_1649377491870{margin-top: 25px !important;}"][vc_column][vc_header_raket header_type="h3" header="Normal mode"][vc_column_text]Normal mode is the default mode for Kvaser interfaces and can be manually set to “canlib.canlib.Driver.NORMAL” using setBusOutputControl. Normal mode means that the driver is a standard push-pull, which means that it can both send (push) and receive (pull) messages on the bus.
[/vc_column_text][/vc_column][/vc_row][vc_row css=".vc_custom_1649377486241{margin-top: 25px !important;}"][vc_column][vc_header_raket header_type="h3" header="Silent mode"][vc_column_text]Silent mode is supported by some controllers and can be set to “canlib.canlib.driver.SILENT” using setBusOutputControl. While in silent mode, the driver will be set to receive only. This means that the controller will not transmit anything on the bus, not even ack (acknowledgement) bits. This can be useful when listening to a CAN bus without wanting to interfere in any way. The silent channel can still be used when calling .write, but the message will not be sent properly. Instead the messages will go into the “transmit queue” without being sent. This will become apparent when there are too many messages in the transmit queue and we get a Transmit buffer overflow error.
Note that not all devices support silent mode. However when we try to set a device that does not support silent mode, we will not receive any indication that the device did not change its mode. Instead, the device will send messages and act like a device on normal mode. This can be a problem if we switch around the devices, without changing their channels. Therefore, before setting a channel to silent mode we want to make sure that the channel/device supports silent mode. There are two ways of doing this. Either we can get the user guide from the Kvaser website, go to Technical data and look in the table to see if it is supported or not, or more preferable, use channel_data. To get the capabilities of the channel, use channel_data.channel_cap. Then create an if statement to see if ChannelCap.SILENT_MODE (silent mode) is included in channel_data.channel_cap.
[/vc_column_text][/vc_column][/vc_row][vc_row css=".vc_custom_1649377478737{margin-top: 25px !important;}"][vc_column][vc_header_raket header_type="h3" header="Example"][vc_column_text]To show how silent mode is used we will reuse the example for sending a CAN message and add to it. The comments used previously have been removed and new comments have been added for the new code.
The first example shows that the silent channel does not interfere with a sent message from another channel. Use the following code in a script:
[/vc_column_text][vc_code_raket language="python" code="JTIzJTIwc2lsZW50X21vZGUlMEFmcm9tJTIwY2FubGliJTIwaW1wb3J0JTIwY2FubGliJTJDJTIwRnJhbWUlMEElMEFjaF9hJTIwJTNEJTIwY2FubGliLm9wZW5DaGFubmVsJTI4Y2hhbm5lbCUzRDAlMjklMEFjaF9iJTIwJTNEJTIwY2FubGliLm9wZW5DaGFubmVsJTI4Y2hhbm5lbCUzRDElMjklMEElMEElMjMlMjBVc2luZyUyMHRoZSUyMHNldEJ1c091dHB1dENvbnRyb2wlMjBtZXRob2QlMjB3ZSUyMHNldCUyMGNoX2ElMjAlMjhjaGFubmVsJTIwMCUyOSUyMHRvJTIwbm9ybWFsLiUyMFRoaXMlMjAlMEElMjMlMjBsaW5lJTIwaXMlMjBub3QlMjBuZWNlc3NhcnklMkMlMjBhcyUyMG5vcm1hbCUyMGlzJTIwdGhlJTIwZGVmYXVsdCUyMG1vZGUlMkMlMjBidXQlMjBmb3IlMjB0aGUlMjBzYWtlJTIwb2YlMEElMjMlMjBjbGFyaXR5JTIwd2UlMjB3aWxsJTIwYWRkJTIwaXQlMjBhbnl3YXkuJTIwVGhlbiUyMHdlJTIwZG8lMjB0aGUlMjBzYW1lJTIwdG8lMjB0aGUlMjBjaGFubmVsJTIwY2hfYiUwQSUyMyUyMCUyOGNoYW5uZWwlMjAxJTI5JTIwYnV0JTIwc2V0JTIwaXQlMjB0byUyMHNpbG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jklMEFwcmludCUyOG1zZyUyOSUwQSUwQWNoX2EuYnVzT2ZmJTI4JTI5JTBBY2hfYi5idXNPZmYlMjglMjklMEElMEFjaF9hLmNsb3NlJTI4JTI5JTBBY2hfYi5jbG9zZSUyOCUyOQ=="][vc_column_text]Running this silent_mode script results in the following error frame (CAN error message):
[/vc_column_text][vc_code_raket language="t" code="JTI4cHlwcm9qJTI5JTNFJTIwcHklMjBzaWxlbnRfbW9kZS5weSUwQUZyYW1lJTI4aWQlM0QwJTJDJTIwZGF0YSUzRGJ5dGVhcnJheSUyOGIlMjclMjclMjklMkMlMjBkbGMlM0QwJTJDJTIwZmxhZ3MlM0QlM0NNZXNzYWdlRmxhZy5FUlJPUl9GUkFNRSUzQSUyMDMyJTNFJTJDJTIwdGltZXN0YW1wJTNEMTUlMjklMEE="][vc_column_text]The message (frame) is sent by the channel ch_a but during the message send, it is turned into an error message (error frame). This meant that no other channel received the message and therefore the acknowledgement bit was not added. This shows that even though the channel ch_b read the message, it did not add an acknowledgement bit, which proves that a silent channel will not interfere with any traffic that is sent on the bus.
The next test is to send a message with a silent channel and observe the CAN interface. This time we will not write a script but instead use the python interpreter. To launch the python interpreter and send a message, type the following into powershell:
[/vc_column_text][vc_code_raket language="t" code="JTNFJTIwLiU1Qy52ZW52JTVDU2NyaXB0cyU1Q2FjdGl2YXRlJTBBJTI4cHlwcm9qJTI5JTIwUFMlMjBDJTNBJTVDVXNlcnMlNUNleHRhYyU1Q1B5cHJvaiUzRSUyMHB5JTBBUHl0aG9uJTIwMy45LjUlMjAlMjh0YWdzJTJGdjMuOS41JTNBMGE3ZGNiZCUyQyUyME1heSUyMCUyMDMlMjAyMDIxJTJDJTIwMTclM0EyNyUzQTUyJTI5JTIwJTVCTVNDJTIwdi4xOTI4JTIwNjQlMjBiaXQlMjAlMjhBTUQ2NCUyOSU1RCUyMG9uJTIwd2luMzIlMEFUeXBlJTIwJTIyaGVscCUyMiUyQyUyMCUyMmNvcHlyaWdodCUyMiUyQyUyMCUyMmNyZWRpdHMlMjIlMjBvciUyMCUyMmxpY2Vuc2UlMjIlMjBmb3IlMjBtb3JlJTIwaW5mb3JtYXRpb24uJTBBJTNFJTNFJTNFJTIwZnJvbSUyMGNhbmxpYiUyMGltcG9ydCUyMGNhbmxpYiUyQyUyMEZyYW1lJTBBJTNFJTNFJTNFJTIwY2glMjAlM0QlMjBjYW5saWIub3BlbkNoYW5uZWwlMjhjaGFubmVsJTNEMSUyOSUwQSUzRSUzRSUzRSUyMGNoLnNldEJ1c091dHB1dENvbnRyb2wlMjhjYW5saWIuRHJpdmVyLlNJTEVOVCUyOSUwQSUzRSUzRSUzRSUyMGNoLmJ1c09uJTI4JTI5JTBBJTNFJTNFJTNFJTIwZnJhbWUlMjAlM0QlMjBGcmFtZSUyOGlkXyUzRDEyMyUyQyUyMGRhdGElM0QlNUI3MiUyQyUyMDY5JTJDJTIwNzYlMkMlMjA3NiUyQyUyMDc5JTJDJTIwMzMlNUQlMjklMEElM0UlM0UlM0UlMjBjaC53cml0ZSUyOGZyYW1lJTI5"][vc_column_text]This will not result in any errors in the python interpreter, but if we look at the Kvaser CAN interface, the channel led will flash red indicating an error (see the picture below, which led that flashes depends on which channel encountered the error). When we are finished looking at the flashing light, go off the bus and close it with the following:
[/vc_column_text][vc_code_raket language="python" code="JTNFJTNFJTNFJTIwY2guYnVzT2ZmJTI4JTI5JTBBJTNFJTNFJTNFJTIwY2guY2xvc2UlMjglMjk="][/vc_column][/vc_row][vc_row][vc_column width="1/2"][vc_single_image image="38145"][/vc_column][vc_column width="1/2"][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]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):
[/vc_column_text][vc_code_raket language="t" code="JTNFJTIwLiU1Qy52ZW52JTVDU2NyaXB0cyU1Q2FjdGl2YXRlJTBBJTI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yMCUyMiUzQ3N0ZGluJTNFJTIyJTJDJTIwbGluZSUyMDIlMkMlMjBpbiUyMCUzQ21vZHVsZSUzRSUwQSUyMCUyMEZpbGUlMjAlMjJDJTNBJTVDVXNlcnMlNUNleHRhYyU1Q1B5cHJvaiU1Qy52ZW52JTVDbGliJTVDc2l0ZS1wYWNrYWdlcyU1Q2NhbmxpYiU1Q2NhbmxpYiU1Q2NoYW5uZWwucHklMjIlMkMlMjBsaW5lJTIwNzE4JTJDJTIwaW4lMjB3cml0ZSUwQSUyMCUyMCUyMCUyMGRsbC5jYW5Xcml0ZSUyOHNlbGYuaGFuZGxlJTJDJTIwZnJhbWUuaWQlMkMlMjBieXRlcyUyOGZyYW1lLmRhdGElMjklMkMlMjBmcmFtZS5kbGMlMkMlMjBmcmFtZS5mbGFncyUyOSUwQSUyMCUyMEZpbGUlMjAlMjJDJTNBJTVDVXNlcnMlNUNleHRhYyU1Q1B5cHJvaiU1Qy52ZW52JTVDbGliJTVDc2l0ZS1wYWNrYWdlcyU1Q2NhbmxpYiU1Q2NhbmxpYiU1Q2RsbC5weSUyMiUyQyUyMGxpbmUlMjAxNzclMkMlMjBpbiUyMF9lcnJvcl9jaGVjayUwQSUyMCUyMCUyMCUyMHJhaXNlJTIwY2FuX2Vycm9yJTI4cmVzdWx0JTI5JTBBY2FubGliLmNhbmxpYi5leGNlcHRpb25zLkNhbkdlbmVyYWxFcnJvciUzQSUyMFRyYW5zbWl0JTIwYnVmZmVyJTIwb3ZlcmZsb3clMjAlMjgtMTMlMjk="][vc_column_text]Once again, remember to go off the bus and close the channel when finished.
[/vc_column_text][vc_code_raket language="python" code="JTNFJTNFJTNFJTIwY2guYnVzT2ZmJTI4JTI5JTBBJTNFJTNFJTNFJTIwY2guY2xvc2UlMjglMjk="][vc_column_text]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:
[/vc_column_text][vc_code_raket code="JTA5JTI4cHlwcm9qJTI5JTNFJTIwcHklMjBjaGVja19jaC5weSUwQSUwOUZvdW5kJTIwNSUyMGNoYW5uZWxzJTBBJTA5MC4lMjBLdmFzZXIlMjBVU0JjYW4lMjBQcm8lMjAyeEhTJTIwdjIlMjAlMjhjaGFubmVsJTIwMCUyOSUyMCUyODAwNzUyLTklM0ExMzQwNiUyRjAlMjklMEElMDkxLiUyMEt2YXNlciUyMFVTQmNhbiUyMFBybyUyMDJ4SFMlMjB2MiUyMCUyOGNoYW5uZWwlMjAxJTI5JTIwJTI4MDA3NTItOSUzQTEzNDA2JTJGMSUyOSUwQSUwOTIuJTIwS3Zhc2VyJTIwTGVhZiUyMFBybyUyMEhTJTIwdjIlMjAlMjhjaGFubmVsJTIwMCUyOSUyMCUyODAwODQzLTQlM0ExMDAxMiUyRjAlMjklMEElMDkzLiUyMEt2YXNlciUyMFZpcnR1YWwlMjBDQU4lMjBEcml2ZXIlMjAlMjhjaGFubmVsJTIwMCUyOSUyMCUyODAwMDAwLTAlM0EwJTJGMCUyOSUwQSUwOTQuJTIwS3Zhc2VyJTIwVmlydHVhbCUyMENBTiUyMERyaXZlciUyMCUyOGNoYW5uZWwlMjAxJTI5JTIwJTI4MDAwMDAtMCUzQTAlMkYxJTI5JTBB"][vc_column_text]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:
[/vc_column_text][vc_code_raket language="python" code="JTIzJTIwc2lsZW50X2xpc3RlbiUwQWZyb20lMjBjYW5saWIlMjBpbXBvcnQlMjBjYW5saWIlMkMlMjBGcmFtZSUwQSUwQSUyMyUyME9wZW4lMjBhJTIwdGhpcmQlMjBjaGFubmVsJTIwJTI4Y2hh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iUyQyUyMDc5JTJDJTIwMzMlNUQlMjklMEFjaF9hLndyaXRlJTI4ZnJhbWUlMjklMEElMEElMjMlMjBBZGQlMjBjaF9jJTIwdG8lMjByZWFkJTIwdGhlJTIwbWVzc2FnZSUyMGFzJTIwdGhlJTIwc2lsZW50JTIwY2hhbm5lbCUyMHRvJTIwcmVhZCUyMHRoZSUyMG1lc3NhZ2UuJTBBbXNnX2MlMjAlM0QlMjBjaF9jLnJlYWQlMjh0aW1lb3V0JTNENTAwJTI5JTBBbXNnX2IlMjAlM0QlMjBjaF9iLnJlYWQlMjh0aW1lb3V0JTNENTAwJTI5JTBBJTBBJTIzJTIwUHJpbnQlMjBib3RoJTIwbWVzc2FnZXMlMjB0byUyMGNvbXBhcmUlMjB0aGVtLiUwQXByaW50JTI4JTIybXNnJTIwYyUzQSVFMiU4MCU5RCUyOSUwQXByaW50JTI4bXNnX2MlMjklMEFwcmludCUyOCUyMm1zZyUyMGIlM0ElRTIlODAlOUQlMjklMEFwcmludCUyOG1zZ19iJTI5JTBBJTBBJTIzJTIwR28lMjBvZmYlMjBidXMlMjB3aXRoJTIwYWxsJTIwdGhyZWUlMjBjaGFubmVscy4lMEFjaF9hLmJ1c09mZiUyOCUyOSUwQWNoX2IuYnVzT2ZmJTI4JTI5JTBBY2hfYy5idXNPZmYlMjglMjklMEElMEElMjMlMjBMYXN0bHklMkMlMjBjbG9zZSUyMGFsbCUyMGNoYW5uZWxzLiUwQWNoX2EuY2xvc2UlMjglMjklMEFjaF9iLmNsb3NlJTI4JTI5JTBBY2hfYy5jbG9zZSUyOCUyOQ=="][vc_column_text]Running the script in powershell will result in the following:
[/vc_column_text][vc_code_raket language="t" code="JTI4cHlwcm9qJTI5JTNFJTIwcHklMjBzaWxlbnRfbGlzdGVuLnB5JTBBbXNnJTIwYyUzQSUwQUZyYW1lJTI4aWQlM0QxMjMlMkMlMjBkYXRhJTNEYnl0ZWFycmF5JTI4YiUyN0hFTExPJTIxJTI3JTI5JTJDJTIwZGxjJTNENiUyQyUyMGZsYWdzJTNEJTNDTWVzc2FnZUZsYWcuU1REJTNBJTIwMiUzRSUyQyUyMHRpbWVzdGFtcCUzRDIlMjklMEFtc2clMjBiJTNBJTBBRnJhbWUlMjhpZCUzRDEyMyUyQyUyMGRhdGElM0RieXRlYXJyYXklMjhiJTI3SEVMTE8lMjElMjclMjklMkMlMjBkbGMlM0Q2JTJDJTIwZmxhZ3MlM0QlM0NNZXNzYWdlRmxhZy5TVEQlM0ElMjAyJTNFJTJDJTIwdGltZXN0YW1wJTNENSUyOQ=="][vc_column_text]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.
[/vc_column_text][vc_raw_html]JTNDYnIlMjAlMkYlM0U=[/vc_raw_html][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="Setting bus parameters"][vc_column_text]There are multiple ways to set bus parameters that can be set on the bus. In this guide we will only focus on setting, checking and changing the CAN Bitrate of the channels using predefined bus parameters, for a list of all predefined parameters go to pycanlib.readthedocs canlib.canlib.Bitrate. To set the bitrate use setBusParams() and canlib.Bitrate.BITRATE_xK as the input, where x is the bitrate wanted. Before setting the bitrate we will use getBusParams() before to get the standard parameters and after to see that the parameters have changed.
Note that both the transmitting and receiving channel must use the same bitrate. Otherwise we will receive an error message and the red light will start to flash. In the following example script we will use the send message example, add the setBusParams function and call it change_bitrate.
[/vc_column_text][vc_code_raket language="python" code="JTIzJTIwY2hhbmdlX2JpdHJhdGUlMEFmcm9tJTIwY2FubGliJTIwaW1wb3J0JTIwY2FubGliJTJDJTIwRnJhbWUlMEElMEFjaF9hJTIwJTNEJTIwY2FubGliLm9wZW5DaGFubmVsJTI4Y2hhbm5lbCUzRDAlMjklMEFjaF9iJTIwJTNEJTIwY2FubGliLm9wZW5DaGFubmVsJTI4Y2hhbm5lbCUzRDElMjklMEElMEElMjMlMjBVc2UlMjBnZXRCdXNQYXJhbXMlMjBhbmQlMj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F0YSUzRCU1QjcyJTJDJTIwNjklMkMlMjA3NiUyQyUyMDc2JTJDJTIwNzklMkMlMjAzMyU1RCUyOSUwQWNoX2Eud3JpdGUlMjhmcmFtZSUyOSUwQSUwQW1zZyUyMCUzRCUyMGNoX2IucmVhZCUyOHRpbWVvdXQlM0Q1MDAlMjklMEFwcmludCUyOG1zZyUyOSUwQSUwQWNoX2EuYnVzT2ZmJTI4JTI5JTBBY2hfYi5idXNPZmYlMjglMjklMEElMEFjaF9hLmNsb3NlJTI4JTI5JTBBY2hfYi5jbG9zZSUyOCUyOQ=="][vc_column_text]Launching this script within the virtual environment will result in the following:
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTIwLiU1Qy52ZW52JTVDU2NyaXB0cyU1Q2FjdGl2YXRlJTBBJTA5JTI4cHlwcm9qJTI5JTNFJTIwcHklMjBjaGFuZ2VfYml0cmF0ZS5weSUwQSUwOSUyODUwMDAwMCUyQyUyMDQlMkMlMjAzJTJDJTIwMSUyQyUyMDElMkMlMjAxJTI5JTBBJTA5JTI4NTAwMDAwJTJDJTIwNCUyQyUyMDMlMkMlMjAxJTJDJTIwMSUyQyUyMDElMjklMEElMDklMjgxMDAwMDAlMkMlMjAxMSUyQyUyMDQlMkMlMjAxJTJDJTIwMyUyQyUyMDElMjklMEElMDklMjgxMDAwMDAlMkMlMjAxMSUyQyUyMDQlMkMlMjAxJTJDJTIwMyUyQyUyMDElMjklMEElMDlGcmFtZSUyOGlkJTNEMTIzJTJDJTIwZGF0YSUzRGJ5dGVhcnJheSUyOGIlMjdIRUxMTyUyMSUyNyUyOSUyQyUyMGRsYyUzRDYlMkMlMjBmbGFncyUzRCUzQ01lc3NhZ2VGbGFnLlNURCUzQSUyMDIlM0UlMkMlMjB0aW1lc3RhbXAlM0QzJTI5"][vc_column_text]We can now clearly see that both channels have the same preset parameters before changing and then after the change they still have the same parameters. Normally the bitrate is not changed, so as a shortcut the bitrate can be set whilst calling openChannel as seen here:
[/vc_column_text][vc_code_raket language="t" code="Y2hfYSUyMCUzRCUyMGNhbmxpYi5vcGVuQ2hhbm5lbCUyOGNoYW5uZWwlM0QyJTJDJTIwYml0cmF0ZSUzRGNhbmxpYi5CaXRyYXRlLkJJVFJBVEVfMTAwSyUyOSUwQQ=="][vc_raw_html]JTNDYnIlMjAlMkYlM0U=[/vc_raw_html][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="LEDs"][vc_column_text]All Kvaser interfaces have LEDs that are used to indicate when the interface is working or having errors. We can test these LEDs with flashLeds and different actions and LEDs. The actions include turning all LeDs on and off or turning all LEDs on or off. For a full list of available actions go to pycanlib.readthedocs canlib.canlic.LEDAction. Note that not all actions will work on all Kvaser interfaces since the interfaces have different numbers of LEDs and channels. We will use a Kvaser USBcan pro 2xHS v2 to test flashLeds with a python interpreter running the following commands (make sure to look at the CAN interface when executing the flashLed commands to see the LEDs):
[/vc_column_text][vc_code_raket code="JTA5JTNFJTIwLiU1Qy52ZW52JTVDU2NyaXB0cyU1Q2FjdGl2YXRlJTBBJTA5JTI4cHlwcm9qJTI5JTNFJTIwcHklMEElMDlQeXRob24lMjAzLjkuNSUyMCUyOHRhZ3MlMkZ2My45LjUlM0EwYTdkY2JkJTJDJTIwTWF5JTIwJTIwMyUyMDIwMjElMkMlMjAxNyUzQTI3JTNBNTIlMjklMjAlNUJNU0MlMjB2LjE5MjglMjA2NCUyMGJpdCUyMCUyOEFNRDY0JTI5JTVEJTIwb24lMjB3aW4zMiUwQSUwOVR5cGUlMjAlMjJoZWxwJTIyJTJDJTIwJTIyY29weXJpZ2h0JTIyJTJDJTIwJTIyY3JlZGl0cyUyMiUyMG9yJTIwJTIybGljZW5zZSUyMiUyMGZvciUyMG1vcmUlMjBpbmZvcm1hdGlvbi4lMEElMDklM0UlM0UlM0UlMjBmcm9tJTIwY2FubGliJTIwaW1wb3J0JTIwY2FubGliJTBBJTA5JTNFJTNFJTNFJTIwY2glMjAlM0QlMjBjYW5saWIub3BlbkNoYW5uZWwlMjhjaGFubmVsJTNEMCUyOSUwQSUwOSUzRSUzRSUzRSUyMGNoLmZsYXNobGVkcyUyOGNhbmxpYi5MRURBY3Rpb24uQUxMX0xFRFNfT04lMkMlMjAxMDAwMCUyOSUwQSUwOSUzRSUzRSUzRSUyMGNoLmZsYXNobGVkcyUyOGNhbmxpYi5MRURBY3Rpb24uQUxMX0xFRFNfT0ZGJTJDJTIwMTAwMDAlMjklMEE="][vc_column_text]In the previous code we executed the actions 0 and 1. The action 0 turns on all LEDs while action 1 turns off all LEDs. How long the LEDs “flash” depends on the second integer. In this example we have 10000 which represents 10000 ms which is 10 seconds. This gives us enough time to clearly see that the LEDs function correctly.
Next we will turn on one LED at a time. Doing this we will show that each “hole” for the LEDs has two LEDs with different colours. In the picture below we can see the different LEDs (the ones in white) as they look on the circuit board inside the cover.
[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width="2/3"][vc_single_image image="38146"][/vc_column][vc_column width="1/3"][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]The inside of a Kvaser CAN interface where we can see the six LEDs on this particular interface that has two channels (two LED pair’s per channel and two PWR LEDs). The designated number for each LED is shown in the picture.
In this example we will turn on LED 0 and 3 which are the two LEDs for CAN 1, these two LEDs are the LED 2 and 3, with the colours red and yellow respectively. Use the following code in the python interpreter:
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTNFJTNFJTIwY2guZmxhc2hMZWRzJTI4Y2FubGliLkxFREFjdGlvbi5MRURfMl9PTiUyQyUyMDEwMDAwJTI5JTBBJTA5JTNFJTNFJTNFJTIwY2guZmxhc2hMZWRzJTI4Y2FubGliLkxFREFjdGlvbi5MRURfM19PTiUyQyUyMDEwMDAwJTI5JTBB"][vc_column_text]If we instead were to use the LEDs for the PWR the colours would be green and yellow (LED_0_ON and LED_1_ON respectively).
[/vc_column_text][vc_raw_html]JTNDYnIlMjAlMkYlM0U=[/vc_raw_html][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="Finding device"][vc_column_text]Within canlib there is a class called “Device” that can be used to find and keep track of a physical device. The device class represents a physical device regardless of whether it is currently connected or not, and on which channel it is connected. If the device is connected, use Device.find to find a device and get a device object. To search for the device both EAN and the serial number can be used. Open the virtual environment, start the python interpreter and run the following:
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTIwLiU1Qy52ZW52JTVDU2NyaXB0cyU1Q2FjdGl2YXRlJTBBJTA5JTI4cHlwcm9qJTI5JTNFJTIwcHklMEElMDklM0UlM0UlM0UlMjBmcm9tJTIwY2FubGliJTIwaW1wb3J0JTIwRGV2aWNlJTJDJTIwRUFOJTBBJTA5JTNFJTNFJTNFJTIwRGV2aWNlLmZpbmQlMjhlYW4lM0RFQU4lMjglMjc3My0zMDEzMC0wMDc1Mi05JTI3JTI5JTI5JTBBJTA5RGV2aWNlJTI4ZWFuJTNEJTNDRUFOJTNBJTIwNzMtMzAxMzAtMDA3NTItOSUzRSUyQyUyMHNlcmlhbCUzRDEzNDA2JTI5JTBBJTA5JTNFJTNFJTNFJTIwRGV2aWNlLmZpbmQlMjhzZXJpYWwlM0QxMzQwNiUyOSUwQSUwOURldmljZSUyOGVhbiUzRCUzQ0VBTiUzQSUyMDczLTMwMTMwLTAwNzUyLTklM0UlMkMlMjBzZXJpYWwlM0QxMzQwNiUyOQ=="][vc_column_text]Device.find will search for, and return the first device that matches the input arguments. In the previous example we searched for the first device with an EAN of 73-30130-00752-9 and the serial number 13406.
If the wanted device is not currently connected, a device object can be created with their EAN and serial number (the minimal information needed to uniquely identify a specific device). With the EAN number only the last six numbers are necessary, since the first seven are default and the same on all interfaces. To create a device run the following code in the python interpreter:
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTNFJTNFJTIwZGV2JTIwJTNEJTIwRGV2aWNlJTI4ZWFuJTNERUFOJTI4JTI3Njc4OTAtMSUyNyUyOSUyQyUyMHNlcmlhbCUzRDQyJTI5"][vc_column_text]After the new device has been created and connected to the PC via a USB we can get its (or any other devices) information with probe_info:
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTNFJTNFJTIwcHJpbnQlMjhkZXYucHJvYmVfaW5mbyUyOCUyOSUyOSUwQSUwOUNBTmxpYiUyMENoYW5uZWwlM0ElMjAyJTBBJTA5Q2FyZCUyME51bWJlciUyMCUyMCUyMCUzQSUyMDAlMEElMDlEZXZpY2UlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlM0ElMjBLdmFzZXIlMjBVU0JjYW4lMjBQcm8lMjAyeEhTJTIwdjIlMjAlMjhjaGFubmVsJTIwMCUyOSUwQSUwOURyaXZlciUyME5hbWUlMjAlMjAlMjAlM0ElMjBrY2FueTBhJTBBJTA5RUFOJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTNBJTIwNzMtMzAxMzAtMDA3NTItOSUwQSUwOUZpcm13YXJlJTIwJTIwJTIwJTIwJTIwJTIwJTNBJTIwMy4yNS4wLjc1MyUwQSUwOVNlcmlhbCUyME51bWJlciUyMCUzQSUyMDEzNDA2"][vc_column_text]Device.find can also be used to open a channel on a specific connected interface irregardless of on which CANlib channel number it was assigned. To do this call on Device.find and then open_channel. This will automatically open the first local channel on the interface:
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTNFJTNFJTIwZGV2JTIwJTNEJTIwRGV2aWNlLmZpbmQlMjhlYW4lM0RFQU4lMjglRTIlODAlOTgwMDc1Mi05JUUyJTgwJTk5JTI5JTI5JTBBJTA5JTNFJTNFJTNFJTIwY2glMjAlM0QlMjBkZXYub3Blbl9jaGFubmVsJTI4JTI5"][vc_column_text]Ch can now be used the same way as any other channel opened with canlib.openChannel(channel=x).
When opening the channel we can simultaneously specify which channel on the interface to use. This is done with chan_no_on_card, which specifies the local channel on the interface. Make sure that the chan_no_on_card integer is less than the number of CANs on the interface (if there are two channels the integer should be 0 or 1).
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTNFJTNFJTIwY2glMjAlM0QlMjBkZXYub3Blbl9jaGFubmVsJTI4Y2hhbl9ub19vbl9jYXJkJTNEMSUyOQ=="][vc_column_text]And the last step is to set the bitrate at the same time as opening the channel.
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTNFJTNFJTIwY2glMjAlM0QlMjBkZXYub3Blbl9jaGFubmVsJTI4Y2hhbl9ub19vbl9jYXJkJTNEMSUyQyUyMGJpdHJhdGUlM0RjYW5saWIuQml0cmF0ZS5CSVRSQVRFXzEwMEslMjk="][vc_raw_html]JTNDYnIlMjAlMkYlM0U=[/vc_raw_html][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="Channel and handle data"][vc_column_text]Probe_info can be used on a device object to get information about a connected interface. Probe_info uses the ChannelData function which we can use directly, such as we did in the check_ch script. Along with ChannelData we can also use two more ways of getting information about a device or interface.
The first way we can use to get the channel data is to use canlib.ChannelData(x) where x is the channel we want data about. To run this command we need to start the virtual environment and the python interpreter before starting the ChannelData. In this example we will get the data from channel 0:
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTIwLiU1Qy52ZW52JTVDU2NyaXB0cyU1Q2FjdGl2YXRlJTBBJTA5JTI4cHlwcm9qJTI5JTNFJTIwcHklMEElMDlQeXRob24lMjAzLjkuNSUyMCUyOHRhZ3MlMkZ2My45LjUlM0EwYTdkY2JkJTJDJTIwTWF5JTIwJTIwMyUyMDIwMjElMkMlMjAxNyUzQTI3JTNBNTIlMjklMjAlNUJNU0MlMjB2LjE5MjglMjA2NCUyMGJpdCUyMCUyOEFNRDY0JTI5JTVEJTIwb24lMjB3aW4zMiUwQSUwOVR5cGUlMjAlMjJoZWxwJTIyJTJDJTIwJTIyY29weXJpZ2h0JTIyJTJDJTIwJTIyY3JlZGl0cyUyMiUyMG9yJTIwJTIybGljZW5zZSUyMiUyMGZvciUyMG1vcmUlMjBpbmZvcm1hdGlvbi4lMEElMDklM0UlM0UlM0UlMjBmcm9tJTIwY2FubGliJTIwaW1wb3J0JTIwY2FubGliJTBBJTA5JTNFJTNFJTNFJTIwZGF0JTIwJTNEJTIwY2FubGliLkNoYW5uZWxEYXRhJTI4MCUyOSUwQQ=="][vc_column_text]After the data object has been created and named dat, we can use that to get any information we want from channeldata. For a full list of available data go to pycanlib readthedocs canlib.canlib.ChannelData.
Using canlib.ChannelData however requires that we know which channel the interface is connected to. This can become problematic since when removing and inserting interfaces they will most likely change channel numbers. So if we do not know the channel we can not use ChannelData. Instead there are two other ways to get the data object. One way is to find or create a device named dev and using dev.channel_dev(), and the other is opening a channel and using ch.channel_data(). Both ways use the Python interpreter:
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTNFJTNFJTIwZGV2JTIwJTNEJTIwRGV2aWNlLmZpbmQlMjhlYW4lM0RFQU4lMjglRTIlODAlOTgwMDc1Mi05JUUyJTgwJTk5JTI5JTI5JTBBJTA5JTNFJTNFJTNFJTIwZGF0JTIwJTNEJTIwZGV2LmNoYW5uZWxfZGF0YSUyOCUyOSUwQSUwQSUwOSUzRSUzRSUzRSUyMGNoJTIwJTNEJTIwY2FubGliLm9wZW5DaGFubmVsJTI4Y2hhbm5lbCUzRDAlMjklMjAlMEElMDklM0UlM0UlM0UlMjBkYXQlMjAlM0QlMjBjaC5jaGFubmVsX2RhdGElMjglMjk="][vc_column_text]Out of these two options dev.channel is always right and easiest to use. Getting dat through openChannel has the same result as the script check_ch.py script used earlier.
[/vc_column_text][vc_raw_html]JTNDYnIlMjAlMkYlM0U=[/vc_raw_html][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="Enumerate"][vc_column_text]Once we have imported canlib.canlib, which enumerates the connected Kvaser CAN devices, we can call getNumberOfChannels to get the number of enumerated channels in our system. If we are connecting and disconnecting interfaces simultaneously while running programs, a problem can arise. Inside of the program we use the channels when referring to CAN channels and interfaces. But if we add or remove any of the interfaces whilst the program is running, the changes won’t affect the program. Or alternatively the wrong interface will be used when referencing a certain channel. Note that the following is only necessary if devices are continuously being connected and disconnected while the code is running.
To fix this problem we will manually enumerate the available CAN channels. The function is canlib.enumerate_hardware and is used to create a completely new set of CANlib channel numbers based on all currently connected devices. The currently opened channel handles are still valid and usable. However, with using this function we need to stop referring to devices based on CANlib channel number, and instead use the channel class. Since the number will change every time enumerate_hardware is called. Instead to retrieve information about a specific channel. Use Channel.channel_data.
[/vc_column_text][/vc_column][/vc_row][vc_row css=".vc_custom_1649377393731{margin-top: 25px !important;}"][vc_column][vc_header_raket header_type="h3" header="Adding a device"][vc_column_text]If we connect a device while a program is being run, the program will simply not recognise that a new device was connected. We can see this by using canlib.getNumberOfChannels inside of the virtual environment and the python interpreter (in this example we have a USBcan connected and will connect a leaflight):
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTIwLiU1Qy52ZW52JTVDU2NyaXB0cyU1Q2FjdGl2YXRlJTBBJTA5JTI4cHlwcm9qJTI5JTNFJTIwcHklMEElMDlQeXRob24lMjAzLjkuNSUyMCUyOHRhZ3MlMkZ2My45LjUlM0EwYTdkY2JkJTJDJTIwTWF5JTIwJTIwMyUyMDIwMjElMkMlMjAxNyUzQTI3JTNBNTIlMjklMjAlNUJNU0MlMjB2LjE5MjglMjA2NCUyMGJpdCUyMCUyOEFNRDY0JTI5JTVEJTIwb24lMjB3aW4zMiUwQSUwOVR5cGUlMjAlMjJoZWxwJTIyJTJDJTIwJTIyY29weXJpZ2h0JTIyJTJDJTIwJTIyY3JlZGl0cyUyMiUyMG9yJTIwJTIybGljZW5zZSUyMiUyMGZvciUyMG1vcmUlMjBpbmZvcm1hdGlvbi4lMEElMDklM0UlM0UlM0UlMjBmcm9tJTIwY2FubGliJTIwaW1wb3J0JTIwY2FubGliJTBBJTA5JTNFJTNFJTNFJTIwY2FubGliLmdldE51bWJlck9mQ2hhbm5lbHMlMjglMjklMEElMDk0"][vc_column_text]Next connect the leaflight and run the getNumberOfChannels again:
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTNFJTNFJTIwY2FubGliLmdldE51bWJlck9mQ2hhbm5lbHMlMjglMjklMEElMDk0"][vc_column_text]If we now manually run enumerate_hardware and then run getNumberOfChannels we get:
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTNFJTNFJTIwY2FubGliLmVudW1lcmF0ZV9oYXJkd2FyZSUwQSUwOTUlMEElMDklM0UlM0UlM0UlMjBjYW5saWIuZ2V0TnVtYmVyT2ZDaGFubmVscyUyOCUyOSUwQSUwOTU="][vc_column_text]We can now see that without restarting the program, the new channel was recognised and can now be used.
[/vc_column_text][/vc_column][/vc_row][vc_row css=".vc_custom_1649377358498{margin-top: 25px !important;}"][vc_column][vc_header_raket header_type="h3" header="Removing a device"][vc_column_text]If we remove a device we will encounter some issues. Firstly, as when connecting a device, the program will not recognise that a device was removed with getNumberOfChannels until the connected devices have been re-enumerated (enumerate has been run):
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTNFJTNFJTIwY2FubGliLmdldE51bWJlck9mQ2hhbm5lbHMlMjglMjklMEElMDk1JTBBJTA5JTNFJTNFJTNFJTIwY2FubGliLmVudW1lcmF0ZV9oYXJkd2FyZSUwQSUwOTQlMEElMDklM0UlM0UlM0UlMjBjYW5saWIuZ2V0TnVtYmVyT2ZDaGFubmVscyUyOCUyOSUwQSUwOTQ="][vc_column_text]If a channel was opened to the device before the device was removed, the program will still attempt to interact with the device using the commands run by the program. Running most of these commands will result in an error, however there are a couple of commands that will still work. To show this we will now attempt to write and read with an interface we remove after opening the channels. Before running both the read and write command the setup code needs to be run in the Python interpreter:
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTNFJTNFJTIwZnJvbSUyMGNhbmxpYiUyMGltcG9ydCUyMGNhbmxpYiUyQyUyMEZyYW1lJTBBJTA5JTNFJTNFJTNFJTIwY2hfYSUyMCUzRCUyMGNhbmxpYi5vcGVuQ2hhbm5lbCUyODAlMjklMEElMDklM0UlM0UlM0UlMjBjaF9hLmJ1c09uJTI4JTI5JTBBJTA5JTNFJTNFJTNFJTIwY2hfYiUyMCUzRCUyMGNhbmxpYi5vcGVuQ2hhbm5lbCUyODElMjklMEElMDklM0UlM0UlM0UlMjBjaF9iLmJ1c09uJTI4JTI5JTBBJTA5JTNFJTNFJTNFJTIwZnJhbWUlMjAlM0QlMjBGcmFtZSUyOGlkXyUzRDEyMyUyQyUyMGRhdGElM0QlNUI3MiUyQyUyMDY5JTJDJTIwNzYlMkMlMjA3NiUyQyUyMDc5JTJDJTIwMzMlNUQlMjk="][vc_column_text]If we now disconnect the CAN interface and attempt to write a frame with one of the removed channels, we will get a hardware error exception:
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTNFJTNFJTIwY2hfYS53cml0ZSUyOGZyYW1lJTI5JTBBJTA5VHJhY2ViYWNrJTIwJTI4bW9zdCUyMHJlY2VudCUyMGNhbGwlMjBsYXN0JTI5JTNBJTBBJTA5JTIwJTIwRmlsZSUyMCUyMiUzQ3N0ZGluJTNFJTIyJTJDJTIwbGluZSUyMDElMkMlMjBpbiUyMCUzQ21vZHVsZSUzRSUwQSUwOSUyMCUyMEZpbGUlMjAlMjJDJTNBJTVDVXNlcnMlNUNleHRhYyU1Q1B5cHJvaiU1Qy52ZW52JTVDbGliJTVDc2l0ZS1wYWNrYWdlcyU1Q2NhbmxpYiU1Q2NhbmxpYiU1Q2NoYW5uZWwucHklMjIlMkMlMjBsaW5lJTIwNzE4JTJDJTIwaW4lMjB3cml0ZSUwQSUwOSUyMCUyMCUyMCUyMGRsbC5jYW5Xcml0ZSUyOHNlbGYuaGFuZGxlJTJDJTIwZnJhbWUuaWQlMkMlMjBieXRlcyUyOGZyYW1lLmRhdGElMjklMkMlMjBmcmFtZS5kbGMlMkMlMjBmcmFtZS5mbGFncyUyOSUwQSUwOSUyMCUyMEZpbGUlMjAlMjJDJTNBJTVDVXNlcnMlNUNleHRhYyU1Q1B5cHJvaiU1Qy52ZW52JTVDbGliJTVDc2l0ZS1wYWNrYWdlcyU1Q2NhbmxpYiU1Q2NhbmxpYiU1Q2RsbC5weSUyMiUyQyUyMGxpbmUlMjAxNzclMkMlMjBpbiUyMF9lcnJvcl9jaGVjayUwQSUwOSUyMCUyMCUyMCUyMHJhaXNlJTIwY2FuX2Vycm9yJTI4cmVzdWx0JTI5JTBBJTA5Y2FubGliLmNhbmxpYi5leGNlcHRpb25zLkNhbkdlbmVyYWxFcnJvciUzQSUyMEElMjBoYXJkd2FyZSUyMGVycm9yJTIwd2FzJTIwZGV0ZWN0ZWQlMjAlMjgtMTUlMjk="][vc_column_text]To attempt to read a message with a removed channel we need to run the setup code once more before removing the interface and running the read code.
[/vc_column_text][vc_code_raket language="t" code="JTA5JTNFJTNFJTNFJTIwY2hfYS5yZWFkJTI4JTI5JTBBJTA5VHJhY2ViYWNrJTIwJTI4bW9zdCUyMHJlY2VudCUyMGNhbGwlMjBsYXN0JTI5JTNBJTBBJTA5JTIwJTIwRmlsZSUyMCUyMiUzQ3N0ZGluJTNFJTIyJTJDJTIwbGluZSUyMDElMkMlMjBpbiUyMCUzQ21vZHVsZSUzRSUwQSUwOSUyMCUyMEZpbGUlMjAlMjJDJTNBJTVDVXNlcnMlNUNleHRhYyU1Q1B5cHJvaiU1Qy52ZW52JTVDbGliJTVDc2l0ZS1wYWNrYWdlcyU1Q2NhbmxpYiU1Q2NhbmxpYiU1Q2NoYW5uZWwucHklMjIlMkMlMjBsaW5lJTIwNzE4JTJDJTIwaW4lMjB3cml0ZSUwQSUwOSUyMCUyMCUyMCUyMGRsbC5jYW5Xcml0ZSUyOHNlbGYuaGFuZGxlJTJDJTIwZnJhbWUuaWQlMkMlMjBieXRlcyUyOGZyYW1lLmRhdGElMjklMkMlMjBmcmFtZS5kbGMlMkMlMjBmcmFtZS5mbGFncyUyOSUwQSUwOSUyMCUyMEZpbGUlMjAlMjJDJTNBJTVDVXNlcnMlNUNleHRhYyU1Q1B5cHJvaiU1Qy52ZW52JTVDbGliJTVDc2l0ZS1wYWNrYWdlcyU1Q2NhbmxpYiU1Q2NhbmxpYiU1Q2RsbC5weSUyMiUyQyUyMGxpbmUlMjAxNzclMkMlMjBpbiUyMF9lcnJvcl9jaGVjayUwQSUwOSUyMCUyMCUyMCUyMHJhaXNlJTIwY2FuX2Vycm9yJTI4cmVzdWx0JTI5JTBBJTA5Y2FubGliLmNhbmxpYi5leGNlcHRpb25zLkNhbkdlbmVyYWxFcnJvciUzQSUyMEElMjBoYXJkd2FyZSUyMGVycm9yJTIwd2FzJTIwZGV0ZWN0ZWQlMjAlMjgtMTUlMjk="][vc_column_text]När man stoppar i ny enhet syns den inte om man redan startatt programmet
Istället för att starta om, använd enumerate
Visa att Canlib channel number “hoppar” men ch fungerar fortfarande.
[/vc_column_text][vc_raw_html]JTNDYnIlMjAlMkYlM0U=[/vc_raw_html][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="Custom channel name"][vc_column_text]When we are using several interfaces with several channels it may become difficult to remember which channel is which. To help with this problem we can rename channels and give them a custom name. To give the channels the custom name we need to use the Kvaser Device Guide. Inside of the Kvaser Device Guide right-click the channel we want to apply the custom name to, and click Edit Channel Name. A window will pop up where the channel name can be imputed, to apply the name click the “ok” button. To remove the name, simply open the change channel name window again and remove the inserted name. Note that the change will affect all programs that use the channel name to identify a device. The device will also reboot to set the name.
The next step is to get the custom name and use it via Python. To get the name use canlib.getChannelData().custom_name. We will now create a script called get_cus_name.py to get the custom name from all connected devices. Within the script write the following code:
[/vc_column_text][vc_code_raket language="python" code="JTIzJTIwZ2V0X2N1c19uYW1lLnB5JTBBZnJvbSUyMGNhbmxpYiUyMGltcG9ydCUyMGNhbmxpYiUwQSUwQW51bV9jaGFubmVscyUyMCUzRCUyMGNhbmxpYi5nZXROdW1iZXJPZkNoYW5uZWxzJTI4JTI5JTBBZm9yJTIwY2glMjBpbiUyMHJhbmdlJTI4bnVtX2NoYW5uZWxzJTI5JTNBJTBBJTIwJTIwJTIwJTIwY2hkJTIwJTNEJTIwY2FubGliLkNoYW5uZWxEYXRhJTI4Y2glMjklMEElMjAlMjAlMjAlMjBwcmludCUyOGYlMjIlN0JjaCU3RC4lMjAlN0JjaGQuY2hhbm5lbF9uYW1lJTdEJTJDJTIwQ3VzdG9tJTIwbmFtZSUzQSUyMCU3QmNoZC5jdXN0b21fbmFtZSU3RCUyMiUyOQ=="][vc_column_text]Which will result in the following if a Kvaser CANusb is installed with channel 0 having the custom name “Green” and channel 1 the custom name “Red”:
[/vc_column_text][vc_code_raket language="t" code="MC4lMjBLdmFzZXIlMjBVU0JjYW4lMjBQcm8lMjAyeEhTJTIwdjIlMjAlMjhjaGFubmVsJTIwMCUyOSUyQyUyMEN1c3RvbSUyMG5hbWUlM0ElMjBHcmVlbiUwQTEuJTIwS3Zhc2VyJTIwVVNCY2FuJTIwUHJvJTIwMnhIUyUyMHYyJTIwJTI4Y2hhbm5lbCUyMDElMjklMkMlMjBDdXN0b20lMjBuYW1lJTNBJTIwUmVkJTBBMi4lMjBLdmFzZXIlMjBWaXJ0dWFsJTIwQ0FOJTIwRHJpdmVyJTIwJTI4Y2hhbm5lbCUyMDAlMjklMkMlMjBDdXN0b20lMjBuYW1lJTNBJTBBMy4lMjBLdmFzZXIlMjBWaXJ0dWFsJTIwQ0FOJTIwRHJpdmVyJTIwJTI4Y2hhbm5lbCUyMDElMjklMkMlMjBDdXN0b20lMjBuYW1lJTNB"][vc_column_text]We can also use the custom name to open a specific channel. To do this write the following script called open_channel_by_name, which will include the function of the same name and a test to make sure it works properly. Within this test we have a kvaser USBcan with the custom name USBone and a leaflight with the custom name My leaf. Within the script write the following code:
[/vc_column_text][vc_code_raket language="python" code="JTIzb3Blbl9fY2hhbm5lbF9ieV9uYW1lJTBBZnJvbSUyMGNhbmxpYiUyMGltcG9ydCUyMGNhbmxpYiUyQyUyMEZyYW1lJTBBJTBBJTBBJTIzJTIwVGhlJTIwZnVuY3Rpb24lMjB3ZSUyMHdpbGwlMjB1c2UlMjB0byUyMG9wZW4lMjBhJTIwY2hhbm5lbCUyMHVzaW5nJTIwdGhlJTIwY3VzdG9tJTIwY2hhbm5lbCUyMG5hbWUuJTBBJT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ZW5fY2hhbm5lbF9ieV9uYW1lJTI4JTIyVVNCb2UlMjIlMjk="][vc_raw_html]JTNDYnIlMjAlMkYlM0U=[/vc_raw_html][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="Troubleshooting, basic errors"][vc_column_text]While working with Python canlib and CAN interfaces, there are multiple errors and problems we can be faced with. We will now go through a couple of common error messages and problems:
[/vc_column_text][vc_column_text]To fix this, firstly go offbus with the channel that received the error to stop the blinking red light. Then make sure there are at least two channels connected to the T-cannector and on bus in normal mode, one to send the message and one to receive it.
When looking for information there are multiple ways to obtain the information needed. The first and best way to get direct information about canlib and commands used with canlib is pycanlib.readthedocs.io. If readthedocs does not work, then on Kvasers website Kvaser.com there are multiple blogs aimed at explaining canlib. To access the blogs go to Kvaser.com blogs and search for the topic of interest. On the Kvaser website we can also find more information under the support heading and in the canlib webhelp. For example we can find basic resources to get started with Kvaser hardware, documentation such as use guides, developer tools and lastly calculators for calculating bitrate available or easy use. If you do not find what you are looking for anywhere in the above then support is always available, just email your issues, problems and questions to [email protected].
For example, in the previous script we want to read a message. But we do not want to move on to the next line until the message has been properly received and read. Furthermore we also want the program to stop and return an error message if the message could not be read. To find the best way to accomplish this we need to look up the documentation at pycanlib.readthedocs.
To find the right documentation we first expand the “Using canlib” tab, since we are using canlib. Next step is how we are using it and what for. Currently we want to send a message and luckily there is a tab called “Send and Receive”, so we can expand it. Next we see that Reading Messages is also a heading so we click on it to go directly to that heading. While reading under the “read“ heading we eventually come to a list of functions. One of these states the following: “If you want to wait until a message arrives (or a timeout occurs) and then read it, call read with a timeout”. This sounds like what we are looking for. To read more about the function we can go directly to the “read” function by clicking the link labeled “read”. We can now see that “read” has a parameter called “timeout” which is an integer. This integer dictates how many milliseconds the program will wait for the message to be delivered before returning a timeout error. We have now found what we were looking for and can enter this into our code as channel.read(timeout = 500) for the program to wait 500 milliseconds.
[/vc_column_text][/vc_column][/vc_row] [post_title] => KVASER CANlib and Python Part 2: Tests and Samples [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => kvaser-canlib-and-python-part-2-tests-and-samples [to_ping] => [pinged] => [post_modified] => 2023-08-21 22:35:55 [post_modified_gmt] => 2023-08-21 22:35:55 [post_content_filtered] => [post_parent] => 0 [guid] => https://www.kvaser.com/?post_type=developer_blog&p=38144 [menu_order] => 0 [post_type] => developer_blog [post_mime_type] => [comment_count] => 0 [filter] => raw ) [6] => WP_Post Object ( [ID] => 36500 [post_author] => 40108 [post_date] => 2021-12-14 20:47:27 [post_date_gmt] => 2021-12-14 20:47:27 [post_content] => [vc_row][vc_column][vc_column_text]This is the first of two articles introducing Kvaser CANlib SDK and the Python package canlib. In this part of the guide we will go through the setup needed to begin understanding and working with the Python CANlib Wrapper. This includes downloading the required software, connecting the necessary hardware and the basic coding used while sending messages with Kvaser interfaces.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]Writer: Anton Carlsson, Software Developer Cowriter: L-G Fredriksson, Field Application Engineer Version: 2021-08-09A[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]With this step-by-step tutorial, we will guide you through sending and receiving messages using Python, Kvaser CAN interfaces and the Python package lib. Kvaser CANlib allows you to control most aspects of any Kvaser CAN interface. The canlib package is a Python wrapper that allows the user to use Kvaser CANlib with Python. This guide will be limited to Windows and Linux where Kvaser CANlib is supported. A command-line application will often be used, and in this guide we will use Windows PowerShell, which is automatically installed on Windows 7 SP1 and Windows Server 2008 R2 SP1 or any later versions. If you want to use another application instead of PowerShell, you are welcome to do so. [/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="Who should read this?"][vc_column_text]This guide has been written for those who want to set up and use a Kvaser CAN interface with CANlib and the Python CANlib package/wrapper. To use this guide, the user needs to be somewhat familiar with (or able to look up) the following:.\.venv\Scripts\activate (pyproj)> pip install canlibTo make sure that canlib was installed correctly, run the following code in a Python interpreter. To open a Python interpreter simply type python or py into PowerShell. To read more about Python interpreters visit Python documentation: (pyproj)> python 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 >>> canlib.prodversion() Which should result in something like: VersionNumber(major=5, minor=36, release=None, build=None) After canlib has been downloaded, if the following message is shown: WARNING: You are using pip version 21.1.1; however, version 21.1.2 is available. You should consider upgrading via the ' C:\Users\extac\Pyproj\.venv\scripts\python.exe -m pip install --upgrade pip' command. Simply run the command in PowerShell as suggested:
C:\Users\extac\Pyproj\.venv\scripts\python.exe -m pip install --upgrade pipThis will upgrade the pip version to the latest version.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="3 Install Hardware"][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h3" header="3.1 Installing Kvaser CAN interfaces"][vc_column_text](It is possible to use any single, dual or quad Kvaser CAN interface. A Kvaser Memorator can also be used in interface mode.) The hardware used in this guide includes a Kvaser USBcan Pro 2xHS v2 and a Kvaser T-cannector v2. Before plugging the hardware together, make sure that the T-cannector is set to 60 ohm (using the switch on the box). The internal termination needed depends on the number of nodes. A load of 60 Ohm will result in a proper recessive level for any number nodes connected to the bus. To read more about termination visit Kvaser.com termination. To install the USBcan, simply plug it into the computer and the hardware will do the rest. Both CAN contacts need to be plugged into the T-cannector, otherwise channels will not be able to send messages between each other. After the message is sent, any CAN controller that received the message correctly adds an acknowledgement bit at the end of the message. If the message is not acknowledged, the sender will resend the message until it is acknowledged. Therefore, to be able to correctly send a message, it needs to be received. To read more about acknowledgement and CAN messages visit Kvaser.com can messages.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h3" header="3.2 Installing virtual interfaces"][vc_column_text]If no hardware is available, then virtual interfaces can be used for the first part of this guide. Two virtual channels are installed automatically on Windows when installing the Kvaser drivers. If you want to read more on how to install additional virtual channels visit Kvaser.com canlib help. In Linux, to download virtual devices, go to linuxcan\virtualcan\virtualcan.h and edit the define statements: #define NR_CHANNELS 2 #define NR_VIRTUAL_DEV 1 You can read more about installing virtual interfaces at Kvaser.com install Linux, if you want to read more about virtual busses in general, visit Kvaser.com virtual device channel. How to use the virtual interface will be presented later in the guide.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="4 Verify the installation"][vc_column_text]To test that all of the software and hardware has been installed correctly, write the following code in the code editor of your choice, making sure the file is located in the same directory as the virtual environment. If you do not have a code editor, idle will have been installed with Python on Windows. To use idle with the virtual environment run the following command in PowerShell: (pyproj)> python -m idlelib This will open an idle window in which you can open your code file and run the code with f5, or via the run tab. If we instead open idle by right-clicking the .py file and choosing edit with idle, we will not be able to run the code directly with f5 or via the run tab. The script we need to run within the virtual environment is check_ch and includes the following code:[/vc_column_text][vc_code_raket code="JTIzJTIwY2hlY2tfY2glMEElMjMlMjBGaXJzdGx5JTIwaW1wb3J0JTIwY2FubGliJTIwc28lMjB0aGF0JTIwaXQlMjBjYW4lMjBiZSUyMHVzZWQlMjBpbiUyMHRoZSUyMHNjcmlwdC4lMEFmcm9tJTIwY2FubGliJTIwaW1wb3J0JTIwY2FubGliJTBBJTBBJTIzJTIwLmdldE51bWJlck9mQ2hhbm5lbHMlMjglMjklMjBpcyUyMHVzZWQlMjB0byUyMGRldGVjdCUyMHRoZSUyMG51bWJlciUyMG9mJTIwY2hhbm5lbHMlMjBhbmQlMEElMjMlMjB0aGUlMjBudW1iZXIlMjBpcyUyMHNhdmVkJTIwaW4lMjB0aGUlMjB2YXJpYWJsZSUyMG51bV9jaGFubmVscy4lMEFudW1fY2hhbm5lbHMlMjAlM0QlMjBjYW5saWIuZ2V0TnVtYmVyT2ZDaGFubmVscyUyOCUyOSUwQSUwQSUyMyUyMG51bV9jaGFubmVscyUyMGlzJTIwcHJpbnRlZCUyMG91dCUyMGFzJTIwdGV4dCUyMHNvJTIwdGhhdCUyMHRoZSUyMHVzZXIlMjBjYW4lMjBzZWU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_image image="36519"][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]To run the code from PowerShell, go to the directory where the file and virtual environment are located. Start the virtual environment and launch the script called check_ch.py with:
.\.venv\Scripts\activate (pyproj)> py check_ch.pyThis should result in
Found 4 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 Virtual CAN Driver (channel 0) (00000-0:0/0) 3. Kvaser Virtual CAN Driver (channel 1) (00000-0:0/1)
Assuming that all of the software and hardware was installed correctly, 0-3 are the channels available on the CANusb. In this example, we will only use channel 0 and 1. On Linux there will be no virtual channel by default. The Kvaser Device Guide can also be used. Simply open the application and we should see something like the following:[/vc_column_text][vc_single_image image="36520"][vc_column_text]In this window we can see that the Kvaser USBcan Pro 2xHS v2 we plugged in appears at the top of the list.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="5 Send a CAN message"][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h3" header="5.1 Simple approach"][vc_column_text]To send a basic CAN message, create a script named send_msg.py containing the following code (make sure that the script is located within the same folder as the virtual environment):[/vc_column_text][vc_code_raket code="JTIzc2VuZF9tc2clMEElMjMlMjBUaGUlMjBDQU5saWIlMjBsaWJyYXJ5JTIwaXMlMjBpbml0aWFsaXplZCUyMHdoZW4lMjB0aGUlMjBjYW5saWIlMjBtb2R1bGUlMjBpcyUyMGltcG9ydGVkLiUyMFRvJTIwYmUlMEElMjMlMjBhYmxlJTIwdG8lMjBzZW5kJTIwYSUyMG1lc3NhZ2UlMkMlMjBGcmFtZSUyMGFsc28lMjBuZWVkcyUyMHRvJTIwYmUlMjBpbnN0YWxsZWQuJTBBZnJvbSUyMGNhbmxpYiUyMGltcG9ydCUyMGNhbmxpYiUyQyUyMEZyYW1lJTBBJTBBJTIzJTIwRmlyc3RseSUyQyUyMG9wZW4lMjB0d28lMjBDQU4lMjBjaGFubmVscyUy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SUyOCUyOSUwQSUwQSUyMyUyMERlcGVuZGluZyUyMG9uJTIwdGhlJTIwc2l0dWF0aW9uJTIwaXQlMjBpcyUyMG5vdCUyMGFsd2F5cyUyMG5lY2Vzc2FyeSUyMG9yJTIwcHJlZmVyYWJsZSUyMHRvJTIwZ28lMjBvZiUwQSUyMyUyMHRoZSUyMGJ1cyUyMHdpdGglMjB0aGUlMjBjaGFubmVscyUyMGFuZCUyQyUyMGluc3RlYWQlMjBvbmx5JTIwdXNlJTIwY2xvc2UlMjglMjkuJTIwQnV0JTIwdGhpcyUyMHdpbGwlMjBiZSUwQSUyMyUyMHRhbGtlZCUyMG1vcmUlMjBhYm91dCUyMGxhdGVyLiUwQSUwQQ=="][vc_column_text]To run the previous program, activate your virtual environment and run the script using powershell. Running the program will result in something like the following:[/vc_column_text][vc_code_raket code="JTNFJTIwLiU1Qy52ZW52JTVDU2NyaXB0cyU1Q2FjdGl2YXRlJTBBJTI4cHlwcm9qJTI5JTNFJTIwcHklMjBzZW5kX21zZy5weSUwQUZyYW1lJTI4aWQlM0QxMjMlMkMlMjBkYXRhJTNEYnl0ZWFycmF5JTI4YiUyN0hFTExPJTIxJTI3JTI5JTJDJTIwZGxjJTNENiUyQyUyMGZsYWdzJTNEJTNDTWVzc2FnZUZsYWcuU1REJTNBJTIwMiUzRSUyQyUyMHRpbWVzdGFtcCUzRDklMjk="][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h4" header="5.1.1 Opening virtual interfaces"][vc_column_text]If we want to use the virtual interface and the virtual channels, we need to change the openchannel commands. Firstly we need to change the numbers from 0 and 1 to 2 and 3 to represent the virtual channels. Then we need to add a flag (another input) that says ACCEPT_VIRTUAL to define that a virtual channel will be used and accepted. The command will now look like: canlib.openChannel(channel=2, flags=canlib.open.ACCEPT_VIRTUAL) If we do not add the ACCEPT_VIRTUAL flag we will receive a Specified device not found (-3). It is also important to note that a virtual channel can only communicate with another virtual channel.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h3" header="5.2 A more Pythonic way"][vc_column_text]The code used in send a CAN message is a very standard and straightforward way to write the script we want. Using Python, however, we can write the code in a more “Pythonic way”. This will lead to the following script names send_msg_pyt.py:[/vc_column_text][vc_code_raket code="JTIzJTIwc2VuZF9tc2dfcHl0JTBBZnJvbSUyMGNhbmxpYiUyMGltcG9ydCUyMGNhbmxpYiUyQyUyMEZyYW1lJTBBJTBBJTIzJTIwaW5zdGVhZCUyMG9mJTIwb3BlbmluZyUyMHRoZSUyMHR3byUyMGNoYW5uZWxzJTIwYW5kJTIwY2xvc2luZyUyMHRoZW0lMjBvbmUlMjBieSUyMG9uZSUyQyUyMHdlJTIwd2lsbCUyMHVzZSUyMGElMEElMjMlMjB3aXRoJTIwc3RhdGVtZW50LiUyMFVzaW5nJTIwdGhlJTIwd2l0aCUyMHN0YXRlbWVudCUyMHRvJTIwb3BlbiUyMG9uZSUyMG9yJTIwbW9yZSUyMGNoYW5uZWxzJTIwd2l0aCUwQSUyMyUyMGNhbmxpYi5vcGVuQ2hhbm5lbCUyOGklMjklMjBhcyUyMGNoX3guJTIwV2l0aGluJTIwdGhpcyUyMHdpdGglMjBzdGF0ZW1lbnQlMjB3ZSUyMHdpbGwlMjB3cml0ZSUyMHRoZSUwQSUyMyUyMHJlc3QlMjBvZiUyMHRoZSUyMGNvZGUuJTBBd2l0aCUyMGNhbmxpYi5vcGVuQ2hhbm5lbCUyODIlMjklMjBhcyUyMGNoX2ElMkMlMjBjYW5saWIub3BlbkNoYW5uZWwlMjgzJTI5JTIwYXMlMjBjaF9iJTNBJTBBJTBBJTIzJTIwSW5zdGVhZCUyMG9mJTIwZ29pbmclMjBvbiUyMGJ1cyUyMHdpdGglMjAlMjJjb3B5LXBhc3RlJTIyJTIwZm9yJTIwZWFjaCUyMGNoYW5uZWwlMkMlMjB3ZSUyMHdpbGwlMjB1c2UlMjBhJTBBJTIzJTIwZm9yLWxvb3AuJTIwV2l0aGluJTIwdGhpcyUyMGxv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lZWQlMjB0byUyMG1hbnVhbGx5JTIwY2xvc2UlMjBpdCUyMG9yJTIwZ28lMjBvZmYlMjBidXMuJTIwVGhlJTIwY2hhbm5lbHMlMjB0aGF0JTIwd2VyZSUyMG9wZW4lMjB1c2luZyUwQSUyMyUyMHRoZSUyMHdpdGglMjBzdGF0ZW1lbnQlMjB3aWxsJTIwYmUlMjBhdXRvbWF0aWNhbGx5JTIwY2xvc2VkJTJDJTIwYW5kJTIwd2l0aCUyMHRoZSUyMGNoYW5uZWxzJTIwYmVpbmclMEElMjMlMjBjbG9zZWQlMjB0aGV5JTIwYWxzbyUyMHdlbnQlMjBvZmYlMjB0aGUlMjBidXMuJTBBJTBB"][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h3" header="5.3 Using WriteWait"][vc_column_text]While writing several messages, we might want to confirm that they were sent properly. To do this we will make use of the command writeWait(). writeWait does the same as write when it comes to sending a message, but it also gives a timeout limit for how long it will wait for the message to be sent. The command may look like writeWait(frame, timeout=500), which will send a message with the details given by frame and wait 500 milliseconds for it to be sent before returning an error message Timeout occurred (-7). WriteWait can be used when sending one or several messages. When sending several messages we can use writeWait on every message, but this will take some time to execute and is not very efficient. Instead we can use write on all messages apart from the last one which will be writeWait. If one message fails to be sent properly, all after will fail and the timeout error Timeout occured (-7) will be raised by writeWait.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]To test writeWait we will make two scripts called send_msg_wait.py and send_msgs_wait.py which will be based on the send message code but with some changes. The first script send_msg_wait.py will send a message using writeWait to send one successful message and one unsuccessful message:[/vc_column_text][vc_code_raket code="JTIzJTIwc2VuZF9tc2dfd2FpdCUwQWZyb20lMjBjYW5saWIlMjBpbXBvcnQlMjBjYW5saWIlMkMlMjBGcmFtZSUwQSUwQSUyMyUyMFdlJTIwd2lsbCUyMG5vdyUyMG9wZW4lMjB0aHJlZSUyMGNoYW5uZWxzJTJDJTIwdHdvJTIwZnJvbSUyMHRoZSUyMFVTQmNhbiUyMGF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ciUyMGklMjBpbiUyMHJhbmdlJTI4MTk5JTI5JTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwY2hfYS53cml0ZSUyOGZyYW1lJTI5JTBBJTIwJTIwJTIwJTIwY2hfYS53cml0ZVdhaXQlMjhmcmFtZSUyQyUyMHRpbWVvdXQlM0Q1MDAlMjklMEElMEElMjAlMjAlMjAlMjBtc2clMjAlM0QlMjBjaF9iLnJlYWQlMjh0aW1lb3V0JTNENTAwJTI5JTBBJTIwJTIwJTIwJTIwcHJpbnQlMjhtc2clMjklMEElMEElMjMlMjBXZSUyMHdpbGwlMjBub3clMjBkbyUyMHRoZSUyMHNhbWUlMjB3aXRoJTIwdGhlJTIwY2hfYyUyMGNoYW5uZWwlMjBub3QlMjBjb25uZWN0ZWQlMjB0byUyMHRoZSUwQSUyMyUyMFQtY2FubmVjdG9yLiUyMFRoaXMlMjBzaG91bGQlMjByZXN1bHQlMjBpbiUyMGElMjB0aW1lb3V0JTIwZXJyb3IuJTBBJTIwJTIwJTIwJTIwZm9yJTIwaSUyMGluJTIwcmFuZ2UlMjgxOTklMjklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBjaF9jLndyaXRlJTI4ZnJhbWUlMjklMEElMjAlMjAlMjAlMjBjaF9jLndyaXRlV2FpdCUyOGZyYW1lJTJDJTIwdGltZW91dCUzRDEwMCUyOSUwQSUwQSUyMCUyMCUyMCUyMG1zZyUyMCUzRCUyMGNoX2IucmVhZCUyOHRpbWVvdXQlM0Q1MDAlMjklMEElMjAlMjAlMjAlMjBwcmludCUyOG1zZyUyOSUwQSUwQQ=="][/vc_column][/vc_row][vc_row][vc_column][vc_header_raket header_type="h2" header="6 Troubleshooting, basic errors"][vc_column_text]While working with Python canlib and CAN interfaces, there are multiple errors and problems we can be faced with. We will now go through a couple of common error messages and problems:Request more information before you complete the purchase.