🙆♂️我是纯良思安,爱技术、爱分享,更爱生活🙆♂️
💖喜欢的朋友可以关注一下,下次更新不迷路💖
前言
作为一名车载测试人员,你是否遇到过使用CANoe时发现没有带License的尴尬场景?或者你们公司仅有几个license,使用时需要借来借去?
如果你掌握了Python-can的使用,那么这个问题就可以得到解决
一、can.viewer是什么?
Python-can库提供的脚本can.viewer提供了可视化的CAN报文收发界面,允许我们直接通过命令行窗口收发并过滤报文
通过Python-can可以接调用Vector的VNbox实现与CAN-BUS的交互,此外支持包括Vector、PCAN、SocketCAN、ETAS等多个硬件接口的使用,可以参看Python-can库的使用(5)——API库之Hardware Interfaces
二、使用方法
1.参数详解
通过 python -m can.viewer -h可以获取完整的使用及参数信息如下:
$ python -m can.viewer -h
ldf is not supported
xls is not supported
xlsx is not supported
yaml is not supported
Usage: python -m can.viewer [-c CHANNEL]
[-i {canalystii,cantact,etas,gs_usb,iscan,ixxat,kvaser,neousys,neovi,nican,nixnet,pcan,robotell,seeedstudio,serial,slcan,socketcan,socketcand,systec,udp_multicast,usb2can,vector,virtual}]
[-b BITRATE] [--fd] [--data_bitrate DATA_BITRATE]
[-h] [--version]
[-d ('{<id>:<format>,<id>:<format>:<scaling1>:...:<scalingN>,file.txt}',)]
[-f ('{<can_id>:<can_mask>,<can_id>~<can_mask>}',)]
[-v]
('extra_args',)
A simple CAN viewer terminal application written in Python
positional arguments:
extra_args The remaining arguments will be used for the interface
and logger/player initialisation. For example, `-i
vector -c 1 --app-name=MyCanApp` is the equivalent to
opening the bus with `Bus('vector', channel=1,
app_name='MyCanApp')
options:
-c, --channel CHANNEL
Most backend interfaces require some sort of channel.
For example with the serial interface the channel
might be a rfcomm device: "/dev/rfcomm0". With the
socketcan interface valid channel examples include:
"can0", "vcan0".
-i, --interface {canalystii,cantact,etas,gs_usb,iscan,ixxat,kvaser,neousys,neovi,nican,nixnet,pcan,robotell,seeedstudio,serial,slcan,socketcan,socketcand,systec,udp_multicast,usb2can,vector,virtual}
Specify the backend CAN interface to use. If left
blank, fall back to reading from configuration files.
-b, --bitrate BITRATE
Bitrate to use for the CAN bus.
--fd Activate CAN-FD support
--data_bitrate DATA_BITRATE
Bitrate to use for the data phase in case of CAN-FD.
Optional arguments:
-h, --help Show this help message and exit
--version Show program's version number and exit
-d, --decode ('{<id>:<format>,<id>:<format>:<scaling1>:...:<scalingN>,file.txt}',)
Specify how to convert the raw bytes into real values.
The ID of the frame is given as the first argument and the format as the second.
The Python struct package is used to unpack the received data
where the format characters have the following meaning:
< = little-endian, > = big-endian
x = pad byte
c = char
? = bool
b = int8_t, B = uint8_t
h = int16, H = uint16
l = int32_t, L = uint32_t
q = int64_t, Q = uint64_t
f = float (32-bits), d = double (64-bits)
Fx to convert six bytes with ID 0x100 into uint8_t, uint16 and uint32_t:
$ python -m can.viewer -d "100:<BHL"
Note that the IDs are always interpreted as hex values.
An optional conversion from integers to real units can be given
as additional arguments. In order to convert from raw integer
values the values are divided with the corresponding scaling value,
similarly the values are multiplied by the scaling value in order
to convert from real units to raw integer values.
Fx lets say the uint8_t needs no conversion, but the uint16 and the uint32_t
needs to be divided by 10 and 100 respectively:
$ python -m can.viewer -d "101:<BHL:1:10.0:100.0"
Be aware that integer division is performed if the scaling value is an integer.
Multiple arguments are separated by spaces:
$ python -m can.viewer -d "100:<BHL" "101:<BHL:1:10.0:100.0"
Alternatively a file containing the conversion strings separated by new lines
can be given as input:
$ cat file.txt
100:<BHL
101:<BHL:1:10.0:100.0
$ python -m can.viewer -d file.txt
-f, --filter ('{<can_id>:<can_mask>,<can_id>~<can_mask>}',)
Space separated CAN filters for the given CAN interface:
<can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)
<can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)
Fx to show only frames with ID 0x100 to 0x103 and 0x200 to 0x20F:
python -m can.viewer -f 100:7FC 200:7F0
Note that the ID and mask are always interpreted as hex values
-v How much information do you want to see at the command
line? You can add several of these e.g., -vv is DEBUG
Shortcuts:
+---------+-------------------------------+
| Key | Description |
+---------+-------------------------------+
| ESQ/q | Exit the viewer |
| c | Clear the stored frames |
| s | Sort the stored frames |
| h | Toggle highlight byte changes |
| SPACE | Pause the viewer |
| UP/DOWN | Scroll the viewer |
+---------+-------------------------------+
2.使用步骤
以Vector的VNbox盒子为例,具体使用方法步骤如下:
- 连接CANoe盒子(如VN1630A\VN1640A等所有Vector产品均可)至被测件
- 安装Python-can库,参考Python-can库的使用(1)——简介与安装
- 配置Vector Hardware Configuration中app_name到指定需要录log的通道参考Python-can库的使用(2)——配置
- 输入下方指令(将app_name及通道改为自己配置好的即可),之后会自动弹出窗口开始获取CAN bus上的报文
python -m can.viewer -i vector -c 0 --app-name=Python_can -v -b 500000 --fd --data_bitrate 2000000
can.viewer使用演示
上方视频展示了一些使用的快捷键,具体如下:
- 通过空格键可以暂停报文的获取
- 如果报文过多,通过上下键可以翻页
- c键可以清理当前屏幕
- s键会根据报文ID大小自动排序
- h键用于切换高亮显示字节更改
- Esc或q可以退出当前录制屏幕
🎈如果文章对您有帮助,您可以“点赞、收藏、关注”,这也是我创作动力的源泉🎈
💘感谢支持💘