Linux 的虚拟串行端口 [英] Virtual Serial Port for Linux
问题描述
我需要在 Linux 上测试一个串口应用程序,但是我的测试机只有一个串口.
I need to test a serial port application on Linux, however, my test machine only has one serial port.
有没有办法向 Linux 添加虚拟串行端口并通过 shell 或脚本模拟设备来测试我的应用程序?
Is there a way to add a virtual serial port to Linux and test my application by emulating a device through a shell or script?
注意:我无法重新映射端口,它在 ttys2 上硬编码,我需要在编写应用程序时对其进行测试.
Note: I cannot remap the port, it hard coded on ttys2 and I need to test the application as it is written.
推荐答案
您可以为此使用 pty(伪电传打字机",其中串行端口是真正的电传打字机").从一端打开/dev/ptyp5
,然后将你的程序附加到/dev/ttyp5
;ttyp5
将像串行端口一样工作,但会通过/dev/ptyp5 发送/接收它所做的一切.
You can use a pty ("pseudo-teletype", where a serial port is a "real teletype") for this. From one end, open /dev/ptyp5
, and then attach your program to /dev/ttyp5
; ttyp5
will act just like a serial port, but will send/receive everything it does via /dev/ptyp5.
如果您确实需要它与名为 /dev/ttys2
的文件通信,那么只需将旧的 /dev/ttys2
移开并创建一个符号链接从 ptyp5
到 ttys2
.
If you really need it to talk to a file called /dev/ttys2
, then simply move your old /dev/ttys2
out of the way and make a symlink from ptyp5
to ttys2
.
当然,您可以使用 ptyp5
以外的其他数字.也许选择一个数字大的以避免重复,因为您所有的登录终端也将使用 ptys.
Of course you can use some number other than ptyp5
. Perhaps pick one with a high number to avoid duplicates, since all your login terminals will also be using ptys.
维基百科有更多关于 ptys 的信息:http://en.wikipedia.org/wiki/Pseudo_terminal
Wikipedia has more about ptys: http://en.wikipedia.org/wiki/Pseudo_terminal
这篇关于Linux 的虚拟串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!