如何使用 tty0tty 设置虚拟串口(Linux 空调制解调器模拟器)

yuv*_*uva 13 emulation tty serial-port

我想在Linux中设置一个虚拟串口模拟器。我希望端口是永久性的,这样我每次重新启动时都可以使用它们。我尝试以这种方式为此目的使用socat

socat -d -d pty,raw,echo=0 pty,raw,echo=0
Run Code Online (Sandbox Code Playgroud)

但是在我的测试程序中使用这些 VSP(虚拟串行端口)时,我经常遇到“端口繁忙”问题。此外,我发现很难创建永久/持久的 VSP。

我现在想为我的目的尝试 tty0tty。但是我几乎没有找到有关如何使用它的文档。任何人都可以启发我吗?如果对 socat 也有任何解决方案,请发表评论。

小智 19

安装

  1. 从以下来源之一下载 tty0tty 包:

  2. 提取它:

    tar xf tty0tty-1.2.tgz
    
    Run Code Online (Sandbox Code Playgroud)
  3. 从提供的源构建内核模块:

     cd tty0tty-1.2/module
     make
    
    Run Code Online (Sandbox Code Playgroud)
  4. 将新的内核模块复制到内核模块目录中:

     sudo cp tty0tty.ko /lib/modules/$(uname -r)/kernel/drivers/misc/
    
    Run Code Online (Sandbox Code Playgroud)
  5. 加载模块:

     sudo depmod
     sudo modprobe tty0tty
    
    Run Code Online (Sandbox Code Playgroud)

    您应该会在/dev/( ls /dev/tnt*) 中看到新的串行端口

  6. 为新的串行端口授予适当的权限:

     sudo chmod 666 /dev/tnt*
    
    Run Code Online (Sandbox Code Playgroud)

您现在可以访问串行端口/dev/tnt0(1、2、3、4 等) 请注意,连续端口是互连的。例如,/dev/tnt0/dev/tnt1连接就像使用直接电缆一样。

跨靴子坚持

编辑文件/etc/modules(Debian) 或/etc/modules.conf

sudo nano /etc/modules.conf
Run Code Online (Sandbox Code Playgroud)

并添加以下行:

tty0tty
Run Code Online (Sandbox Code Playgroud)

警告

请注意,此方法不会使模块在内核更新后持续存在,因此如果您曾经更新过内核,请确保再次构建 tty0tty,重复该过程。

如果您不在每次内核更新systemctl --failed时移动模块,将返回该服务已systemd-modules-load.service结束“ failed”。也在journalctl -xe | grep "Failed to find".