“How to send or receive CAN data”的版本间的差异
来自百问网嵌入式Linux wiki
Zhouyuebiao(讨论 | 贡献) |
|||
(未显示同一用户的1个中间版本) | |||
第1行: | 第1行: | ||
+ | {{DISPLAYTITLE:如何发送或接收CAN数据}} | ||
+ | |||
+ | =如何发送或接收 CAN 数据= | ||
+ | ==目的== | ||
+ | : 本文介绍如何使用 can-utils [1 ]包在 SocketCAN 接口上发送/接收数据。 | ||
+ | : can-utils 包含一些用于 Linux®SocketCAN 子系统的用户空间实用程序。它已集成在STM32微处理器系列的 SDK 中。在此示例中,仅使用到 cansend 和 candump,但软件包中还提供了许多其他的工具。 | ||
+ | |||
+ | ==准备条件== | ||
+ | : 至少需要两个设备才能在 CAN 网络上进行通信。要实现这种配置,请在同一 CAN 总线上连接两个设备。然后,从一个节点发送数据,并在另一节点上接收数据。 | ||
+ | : 在开始通信之前,必须在每块板上配置并启用 SocketCAN 接口。[https://wiki.st.com/stm32mpu/wiki/How_to_set_up_a_SocketCAN_interface 请参阅如何设置 SocketCAN 接口]。 | ||
+ | |||
+ | ==在CAN总线上发送数据== | ||
+ | : 要发送单个帧,请使用 cansend 实用程序: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | Board $> cansend can0 123#1122334455667788 | ||
+ | </syntaxhighlight> | ||
+ | : 获取 cansend 实用程序中的使用帮助信息: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | Board $> cansend -h | ||
+ | Usage: cansend <device> <can_frame> | ||
+ | </syntaxhighlight> | ||
+ | : 如果出现帧错误,请尝试下面的操作: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | <can_id>#{R|data} for CAN 2.0 frames | ||
+ | <can_id>##<flags>{data} for CAN FD frames | ||
+ | |||
+ | <can_id> can have 3 (SFF) or 8 (EFF) hex chars | ||
+ | {data} has 0..8 (0..64 CAN FD) ASCII hex-values (optionally separated by '.') | ||
+ | <flags> a single ASCII Hex value (0 .. F) which defines canfd_frame.flags | ||
+ | |||
+ | e.g. 5A1#11.2233.44556677.88/123#DEADBEEF/5AA#/123##1/213##311 | ||
+ | 1F334455#1122334455667788/123#R for remote transmission request. | ||
+ | </syntaxhighlight> | ||
+ | ==在CAN总线上接收数据== | ||
+ | : 要实时显示总线上收到的消息列表,请使用 candump 实用程序: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | Board $> candump can0 | ||
+ | can0 123 [8] 11 22 33 44 55 66 77 88 | ||
+ | </syntaxhighlight> | ||
+ | : 在 candump 实用程序上获取帮助信息: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | Board $> candump -h | ||
+ | |||
+ | Usage: candump [options] <CAN interface>+ | ||
+ | (use CTRL-C to terminate candump) | ||
+ | |||
+ | Options: -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date) | ||
+ | -c (increment color mode level) | ||
+ | -i (binary output - may exceed 80 chars/line) | ||
+ | -a (enable additional ASCII output) | ||
+ | -S (swap byte order in printed CAN data[] - marked with '`' ) | ||
+ | -s <level> (silent mode - 0: off (default) 1: animation 2: silent) | ||
+ | -b <can> (bridge mode - send received frames to <can>) | ||
+ | -B <can> (bridge mode - like '-b' with disabled loopback) | ||
+ | -u <usecs> (delay bridge forwarding by <usecs> microseconds) | ||
+ | -l (log CAN-frames into file. Sets '-s 2' by default) | ||
+ | -L (use log file format on stdout) | ||
+ | -n <count> (terminate after receiption of <count> CAN frames) | ||
+ | -r <size> (set socket receive buffer to <size>) | ||
+ | -D (Don't exit if a "detected" can device goes down. | ||
+ | -d (monitor dropped CAN frames) | ||
+ | -e (dump CAN error frames in human-readable format) | ||
+ | -x (print extra message infos, rx/tx brs esi) | ||
+ | -T <msecs> (terminate after <msecs> without any reception) | ||
+ | |||
+ | Up to 16 CAN interfaces with optional filter sets can be specified on the commandline in the form: <ifname>[,filter]* | ||
+ | |||
+ | Comma separated filters can be specified for each 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) | ||
+ | #<error_mask> (set error frame filter, see include/linux/can/error.h) | ||
+ | [j|J] (join the given CAN filters - logical AND semantic) | ||
+ | |||
+ | CAN IDs, masks and data content are given and expected in hexadecimal values. | ||
+ | When can_id and can_mask are both 8 digits, they are assumed to be 29 bit EFF. | ||
+ | Without any given filter all data frames are received ('0:0' default filter). | ||
+ | |||
+ | Use interface name 'any' to receive from all CAN interfaces. | ||
+ | |||
+ | Examples: | ||
+ | candump -c -c -ta can0,123:7FF,400:700,#000000FF can2,400~7F0 can3 can8 | ||
+ | candump -l any,0~0,#FFFFFFFF (log only error frames but no(!) data frames) | ||
+ | candump -l any,0:0,#FFFFFFFF (log error frames and also all data frames) | ||
+ | candump vcan2,92345678:DFFFFFFF (match only for extended CAN ID 12345678) | ||
+ | candump vcan2,123:7FF (matches CAN ID 123 - including EFF and RTR frames) | ||
+ | candump vcan2,123:C00007FF (matches CAN ID 123 - only SFF and non-RTR frames) | ||
+ | </syntaxhighlight> | ||
+ | ==硬件自检== | ||
+ | : 在内部环回测试模式下,FDCAN将发送的消息作为接收消息进行处理。此选项用于硬件自检(无需在 CAN 总线上连接外部 CAN 节点)。 | ||
+ | |||
+ | : 要在回环模式下配置和启用 SocketCAN,请执行以下操作: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | Board $> ip link set can0 up type can bitrate 1000000 dbitrate 2000000 fd on loopback on | ||
+ | [ 78.700698] m_can 4400e000.can can0: bitrate error 0.3% | ||
+ | [ 78.704568] m_can 4400e000.can can0: bitrate error 1.6% | ||
+ | [ 78.710140] IPv6: ADDRCONF(NETDEV_CHANGE): can0: link becomes ready | ||
+ | </syntaxhighlight> | ||
+ | : 要在同一接口上发送和接收消息,请按照下列步骤操作: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | Board $> candump can0 -L & | ||
+ | [1] 475 | ||
+ | Board $> cansend can0 300#AC.AB.AD.AE.75.49.AD.D1 | ||
+ | (1539944874.949723) can0 300#ACABADAE7549ADD1 | ||
+ | (1539944874.949683) can0 300#ACABADAE7549ADD1 | ||
+ | </syntaxhighlight> | ||
+ | ==参考== | ||
+ | : [https://github.com/linux-can/can-utils/blob/master/README.md CAN-UTILS信息],github.com上的README | ||
+ | |||
[[Category:Linux_Operating_System]] | [[Category:Linux_Operating_System]] | ||
[[Category:Networking]] | [[Category:Networking]] | ||
[[Category:Netdev]] | [[Category:Netdev]] | ||
[[Category:CAN]] | [[Category:CAN]] |
2020年1月6日 (一) 15:01的最新版本
如何发送或接收 CAN 数据
目的
- 本文介绍如何使用 can-utils [1 ]包在 SocketCAN 接口上发送/接收数据。
- can-utils 包含一些用于 Linux®SocketCAN 子系统的用户空间实用程序。它已集成在STM32微处理器系列的 SDK 中。在此示例中,仅使用到 cansend 和 candump,但软件包中还提供了许多其他的工具。
准备条件
- 至少需要两个设备才能在 CAN 网络上进行通信。要实现这种配置,请在同一 CAN 总线上连接两个设备。然后,从一个节点发送数据,并在另一节点上接收数据。
- 在开始通信之前,必须在每块板上配置并启用 SocketCAN 接口。请参阅如何设置 SocketCAN 接口。
在CAN总线上发送数据
- 要发送单个帧,请使用 cansend 实用程序:
Board $> cansend can0 123#1122334455667788
- 获取 cansend 实用程序中的使用帮助信息:
Board $> cansend -h
Usage: cansend <device> <can_frame>
- 如果出现帧错误,请尝试下面的操作:
<can_id>#{R|data} for CAN 2.0 frames
<can_id>##<flags>{data} for CAN FD frames
<can_id> can have 3 (SFF) or 8 (EFF) hex chars
{data} has 0..8 (0..64 CAN FD) ASCII hex-values (optionally separated by '.')
<flags> a single ASCII Hex value (0 .. F) which defines canfd_frame.flags
e.g. 5A1#11.2233.44556677.88/123#DEADBEEF/5AA#/123##1/213##311
1F334455#1122334455667788/123#R for remote transmission request.
在CAN总线上接收数据
- 要实时显示总线上收到的消息列表,请使用 candump 实用程序:
Board $> candump can0
can0 123 [8] 11 22 33 44 55 66 77 88
- 在 candump 实用程序上获取帮助信息:
Board $> candump -h
Usage: candump [options] <CAN interface>+
(use CTRL-C to terminate candump)
Options: -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)
-c (increment color mode level)
-i (binary output - may exceed 80 chars/line)
-a (enable additional ASCII output)
-S (swap byte order in printed CAN data[] - marked with '`' )
-s <level> (silent mode - 0: off (default) 1: animation 2: silent)
-b <can> (bridge mode - send received frames to <can>)
-B <can> (bridge mode - like '-b' with disabled loopback)
-u <usecs> (delay bridge forwarding by <usecs> microseconds)
-l (log CAN-frames into file. Sets '-s 2' by default)
-L (use log file format on stdout)
-n <count> (terminate after receiption of <count> CAN frames)
-r <size> (set socket receive buffer to <size>)
-D (Don't exit if a "detected" can device goes down.
-d (monitor dropped CAN frames)
-e (dump CAN error frames in human-readable format)
-x (print extra message infos, rx/tx brs esi)
-T <msecs> (terminate after <msecs> without any reception)
Up to 16 CAN interfaces with optional filter sets can be specified on the commandline in the form: <ifname>[,filter]*
Comma separated filters can be specified for each 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)
#<error_mask> (set error frame filter, see include/linux/can/error.h)
[j|J] (join the given CAN filters - logical AND semantic)
CAN IDs, masks and data content are given and expected in hexadecimal values.
When can_id and can_mask are both 8 digits, they are assumed to be 29 bit EFF.
Without any given filter all data frames are received ('0:0' default filter).
Use interface name 'any' to receive from all CAN interfaces.
Examples:
candump -c -c -ta can0,123:7FF,400:700,#000000FF can2,400~7F0 can3 can8
candump -l any,0~0,#FFFFFFFF (log only error frames but no(!) data frames)
candump -l any,0:0,#FFFFFFFF (log error frames and also all data frames)
candump vcan2,92345678:DFFFFFFF (match only for extended CAN ID 12345678)
candump vcan2,123:7FF (matches CAN ID 123 - including EFF and RTR frames)
candump vcan2,123:C00007FF (matches CAN ID 123 - only SFF and non-RTR frames)
硬件自检
- 在内部环回测试模式下,FDCAN将发送的消息作为接收消息进行处理。此选项用于硬件自检(无需在 CAN 总线上连接外部 CAN 节点)。
- 要在回环模式下配置和启用 SocketCAN,请执行以下操作:
Board $> ip link set can0 up type can bitrate 1000000 dbitrate 2000000 fd on loopback on
[ 78.700698] m_can 4400e000.can can0: bitrate error 0.3%
[ 78.704568] m_can 4400e000.can can0: bitrate error 1.6%
[ 78.710140] IPv6: ADDRCONF(NETDEV_CHANGE): can0: link becomes ready
- 要在同一接口上发送和接收消息,请按照下列步骤操作:
Board $> candump can0 -L &
[1] 475
Board $> cansend can0 300#AC.AB.AD.AE.75.49.AD.D1
(1539944874.949723) can0 300#ACABADAE7549ADD1
(1539944874.949683) can0 300#ACABADAE7549ADD1
参考
- CAN-UTILS信息,github.com上的README