HowTo: Virtual Serial Ports on Linux using socat, and more

socat (SOcket CAT) – multipurpose relay – is a command line based utility that establishes two bidirectional byte streams and transfers data between them.
socat is #4 on the Top 100 Network Security Tools list, available in most distro repositories (on Debian/Ubuntu sudo apt-get install socat does the trick), really light on resources, and very efficient.
Sounds simple, does wonders!

Sidenote: socat is actually #71 on the mentioned list, but since socat is a much enhanced version of netcat, which is #4, it seems to me logical to count socat as #4 also.

One of the wonders you can do is creating pairs of “virtual” ports/interfaces/sockets etc., even hybrid pairs like port-socket, etc., where one (or both) ends of the pair can also be real objects. See socat man page for more details.

Creating pairs of virtual serial ports (VSP), is quite often wanted feature, yet it’s hard to find a solution online (try Googling it)… Pair of VSP’s is very useful to have esp. if you’re into embedded device programming, where many embedded development kits support debugging via serial link – PharLap in my case. Yes, it does have Ethernet debugging since ETS 14, but it’s a very unstable option.
Even if you’re using Ethernet debugging, it’s often much easier to have your embedded system in a virtual machine (VirtualBox, VMWare etc.), and do the debugging via virtual NIC.

To cut the talk short, many thanks to Gerhard Rieger, author of socat, for the following tips!

To create a pair of VSP’s

socat -d -d pty,raw,echo=0 pty,raw,echo=0

and that’s it! As long as the socat is running, you have a pair of VSP’s open (their names are printed by socat on initialization). See socat man page for more details on what the above command does.

Connecting executable and VSP

socat -d -d pty,raw,echo=0 "exec:myprog ...,pty,raw,echo=0"

where the executable myprog will be connected with the VSP through stdio.

Virtual network interfaces

In a similar fashion, you can create pairs of virtual network interfaces – tun/tap devices (again, this pair exists as long as the master process – socat – is alive):

sudo socat -d -d tun:10.0.0.1/8 tun:192.168.0.1/24

Yes, that’s it, you’ve got a pair of virtual network interfaces!

12 responses to “HowTo: Virtual Serial Ports on Linux using socat, and more

  1. Ok, this post was really useful.

    openpty / forkpty are nice, but socat is the best way to do it without any of the two serial port ends knowing about a virtual serial port being used.

    socat is extremely useful when one end is a closed app with hard coded serial port. In cases like this, you will have to make a symlink from a terminal anyway (after doing the tty magic on your end of the serial link). Might as well simply use socat from a terminal and forget about adding extra code to your end 🙂

  2. Hello
    I create a virtual com port,and use socat to let ethernet port communicate with serial port
    I send the comman which is
    socat TCP-LISTEN:23000 /dev/ttyS0,b19200,raw,echo=0
    But when I transmit data in two ports,the baudrate is not 19200
    Thank you

  3. I’d like to create a VSP that exchanges data with a couple of named pipes.
    One for tx the other for rx, hence a dual address is nedded on the pipe side.

    As I understand things I should write something like:
    socat -d -d pty,raw,echo=0 PIPE:receive_fifo,rdonly=1!!PIPE:transmit_fifo,wronly=1

    When I try this (or whichever thing contains PIPE:) the command line and history get messed up!

    What am I doing wrong?

    (using socat version 1.7.1.3 on Jul 28 2011 16:19:46)

    Thank you in advance
    Giampiero

  4. Thank you for that post veeery much. Just what I needed. Virtual ptys have been removed from the kernel and I had no idea how to emulate them. Thank you!

  5. Pingback: socat을 이용한 리눅스에서 가상시리얼포트 사용방법 « kyuls

  6. Pingback: Linux: Virtual Serial Port | Gunawan's Blog

  7. Pingback: Fun with x10 and Domoticz | My Pocket Fluff

  8. Pingback: How To Install WordPress On Vsp Insurance – WordPress Hosting

Leave a comment