socat虚拟串口工具

用途:linux socat 将2个串口通过虚拟的方式连接起来,这样就可以:直接对串口程序进行调试了。

对串口程序进行调试,不再需要外部串口。

  1. Win OS

Win com0com: 开源,虚拟串口。

Null-modem emulator download | SourceForge.net

  1. Linux OS

虚拟串口工具,在 Linux中,在网络上普遍能够搜索到的是/dev/pts/1 /2这样的成对设备形式,这种设备标识在 cutecom中是无法使用的,怎么办???

在 cutecom中使用,需要使用的设备标识形式为:ttyS0 /1/2/3。

解决办法,只需一条命令:


sudo socat -d -d pty,link=/dev/ttyS0,raw,echo=0 pty,link=/dev/ttyS1,raw,echo=0

注意1:link=/dev/ttyS0,link=/dev/ttyS1需要是真实存在的端口(或虚拟机中已经存在的端口),这种端口直接使用cutecom是可以找得到的。

注意2:必须以 sudo 来运行。

注意3:socat的功能,远不止是作为 普通的虚拟串口对 来使用,这是一条很强大的串口命令。

参考:

Virtual serial port redirection using socat | A Mutable Log

Virtual serial port redirection using socat

Here’s how socat can be used to redirect one serial port to another on Ubuntu, assuming both serial devices are not assigned to a real device


sudo socat -d-d pty,link=/dev/ttyS0,raw,echo=0 pty,link=/dev/ttyS1,raw,echo=0

If the above command is successful, you can send data from one serial port to another using a terminal emulator such as screen


screen /dev/ttyS0 115200

Install screen using sudo apt install screen if not already installed. Repeat the above command on another prompt

for the other serial device, and you’re good to go. Quit screen using

Ctrl-A.

socat can also be used to perform serial to TCP redirection


sudo socat -d-d pty,link=/dev/ttyS0,raw,echo=0 tcp-listen:8000

Now, you should be able to use screen to send and receive data on the serial port, while you use something like netcat to receive and send data to the serial port


netcat 127.0.0.1 8000
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐