如何在linux上创建虚拟CAN端口?(C++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21022749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create virtual CAN port on linux? (C++)
提问by Micha? Zubrzycki
I want to create program that would emulate CAN port for testing purposes for another big application. Program should send previously recorded data through this virtual CAN. Anyone has any experience with such thing?
我想创建一个程序来模拟 CAN 端口,以用于另一个大型应用程序的测试目的。程序应该通过这个虚拟 CAN 发送先前记录的数据。任何人有这样的经验吗?
I'm thinking to establish virtual COM, and send through it data packed in CAN Frames. Could it work? And how could I establish virtual COM on linux? Found this thread Virtual Serial Port for Linuxbut sadly I don't get how could it be implemented into source code of program (beginner linux user and programmer under linux).
我正在考虑建立虚拟 COM,并通过它发送打包在 CAN 帧中的数据。它可以工作吗?以及如何在 linux 上建立虚拟 COM?发现这个线程Virtual Serial Port for Linux但遗憾的是我不明白它是如何实现到程序的源代码中的(Linux 下的初学者 linux 用户和程序员)。
Would love to read your experience and suggestions.
很想阅读您的经验和建议。
采纳答案by yegorich
You need SocketCANdriver, that is available on modern Linux distributions like Ubuntu etc. SocketCAN provides a virtual CAN port driver:
您需要SocketCAN驱动程序,该驱动程序可在现代 Linux 发行版(如 Ubuntu 等)上使用。 SocketCAN 提供了一个虚拟 CAN 端口驱动程序:
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
Now you can send and receive CAN frames over vcan0
device. Wikipedia article provides simple code example on how to use SocketCAN.
现在您可以通过vcan0
设备发送和接收 CAN 帧。维基百科文章提供了关于如何使用 SocketCAN 的简单代码示例。
You'll also need can-utilsfor testing purposes.
您还需要can-utils进行测试。
You'll find more information about SocketCAN and its usage on eLinux.org
您可以在eLinux.org上找到有关 SocketCAN 及其用法的更多信息