如何在 Linux 中创建将数据代理到真实设备的虚拟 io 设备?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11196348/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:02:38  来源:igfitidea点击:

How create a virtual io device in Linux that proxies data to real device?

linuxserial-portpipevirtual-serial-port

提问by videoguy

I have an interesting problem. I am working on an embedded box with multiple instances of Linux running each on an ARM processor. They are connected over internal 1GBps network. I have a serial port device node attached to processor A (Lets say Linux-A running on it). I have a program running on processor B (Lets say on Linux-B) access the serial port device as if it is attached to Linux-B locally.
My program invokes term i/o type api calls on device node to control tty echo, character mode input. What I am wondering is if there is a way to create a virtual serial device that is available on Linux-B somehow talking to real serial device on Linux-A over internal network.

我有一个有趣的问题。我正在开发一个带有多个 Linux 实例的嵌入式盒子,每个实例都在一个 ARM 处理器上运行。它们通过内部 1GBps 网络连接。我有一个连接到处理器 A 的串行端口设备节点(假设在其上运行 Linux-A)。我有一个在处理器 B 上运行的程序(假设在 Linux-B 上)访问串行端口设备,就像它在本地连接到 Linux-B 一样。
我的程序在设备节点上调用 term i/o type api 调用来控制 tty echo、字符模式输入。我想知道是否有一种方法可以创建一个在 Linux-B 上可用的虚拟串行设备,以某种方式通过内部网络与 Linux-A 上的真实串行设备通信。

I am thinking something along the lines of: Linux-B has /dev/ttyvirtual. Anything that gets written to it gets transported over network socket to Linux-A serialserver. The serial server exrcises the api calls on real device lets say /dev/ttys0. Any data waiting on ttys0 gets transported back to /dev/ttyvirtual.

我在想一些事情:Linux-B 有 /dev/ttyvirtual。写入其中的任何内容都会通过网络套接字传输到 Linux-A 串行服务器。串行服务器在真实设备上执行 api 调用,比如 /dev/ttys0。任何在 ttys0 上等待的数据都会被传输回 /dev/ttyvirtual。

What are all the things involved to get this done fast?

快速完成这项工作涉及哪些事项?

Thanks
Videoguy

谢谢
视频人

Update: I found a discussion at http://fixunix.com/bsd/261068-network-socket-serial-port-question.htmlwith great pointers.
Another useful link is http://blog.philippklaus.de/2011/08/make-rs232-serial-devices-accessible-via-ethernet/

更新:我在http://fixunix.com/bsd/261068-network-socket-serial-port-question.html上找到了一个很好的讨论 。
另一个有用的链接是http://blog.philippklaus.de/2011/08/make-rs232-serial-devices-accessible-via-ethernet/

采纳答案by videoguy

I ended up using socat

我最终使用了socat

Examples can be found here: socat examples

可以在此处找到示例socat 示例

You socat back to back on both the machines. One listens on a tcp port and forwards data to local virtual port or pty. The socat on other box uses real device as input and forwards any data to tcp port.

你在两台机器上背靠背。一个监听 tcp 端口并将数据转发到本地虚拟端口或 pty。其他盒子上的 socat 使用真实设备作为输入并将任何数据转发到 tcp 端口。

回答by mrb

Take a look at openpty(3). This lets you create a pseudo-TTY (like /dev/pts/0, the sort that ssh connections use), which will respond as a normal TTY would, but give you direct programmatic control over the connections.

看看openpty(3)。这使您可以创建一个伪 TTY(例如/dev/pts/0ssh 连接使用的排序),它将像普通 TTY 一​​样响应,但可以让您直接以编程方式控制连接。

This way you can host a serial device (eg. /dev/pts/5) that you forward data between a network connection, and then other apps can perform serial operations on it without knowing about the underlying network bridge.

通过这种方式,您可以托管/dev/pts/5在网络连接之间转发数据的串行设备(例如),然后其他应用程序可以在不知道底层网桥的情况下对其执行串行操作。