Skip to content

Socat

Install socat with Homebrew or MacPorts and create a pair of connected virtual serial ports for testing serial communication.

2 min readUpdated

socat (“SOcket CAT”) relays data between two endpoints of almost any kind: files, pipes, TCP and UDP sockets, serial devices and pseudo-terminals. A common use is a pair of virtual serial ports that are connected to each other. One program writes to one end and the other reads from the other end, which lets you test serial communication software without hardware or a null-modem cable.

Install socat

On macOS with Homebrew:

Terminal window
brew install socat

With MacPorts:

Terminal window
sudo port install socat

On Debian or Ubuntu:

Terminal window
sudo apt install socat

Create a pair of virtual serial ports

This command creates two pseudo-terminals (PTYs) and copies everything written to one into the other, in both directions:

Terminal window
socat -d -d pty,raw,echo=0 pty,raw,echo=0
  • -d -d prints notice messages, including the names of the devices socat created.
  • pty creates a new pseudo-terminal for each side.
  • raw passes the data through without line editing or character translation.
  • echo=0 turns off local echo, so data is not sent back to the writer.

socat prints the two device names and keeps running until you stop it with Ctrl+C:

Terminal window
2014/08/07 01:40:56 socat[74896] N PTY is /dev/ttys001
2014/08/07 01:40:56 socat[74896] N PTY is /dev/ttys002
2014/08/07 01:40:56 socat[74896] N starting data transfer loop with FDs [3,3] and [5,5]

On macOS the devices are named /dev/ttysNNN; on Linux they appear as /dev/pts/N. The numbers change on every run.

Recent socat versions mark raw as obsolete and recommend rawer, which also turns off echo: socat -d -d pty,rawer pty,rawer.

To get fixed names that you can put in a program’s configuration, add link= to each side. socat creates a symbolic link to each PTY:

Terminal window
socat -d -d pty,raw,echo=0,link=/tmp/ttyV0 pty,raw,echo=0,link=/tmp/ttyV1

Test the connection

Open a new terminal and read from the first port:

Terminal window
cat /dev/ttys001

Open another terminal and write to the second port:

Terminal window
echo "Hello World" > /dev/ttys002

The first terminal prints “Hello World”. Replace the device names with the ones socat printed.

The PTYs belong to the user who started socat, so sudo is not needed. It would not help with the redirection either: in sudo echo "Hello World" > /dev/ttys002 the shell opens the device before sudo runs.

References

Related notes

1 min read

GNOME

Make the GNOME login screen (GDM) ask for a username instead of showing the list of users.

  • Linux & Systems
4 min read

Jitsi Meet

Self-host Jitsi Meet on Debian: install from the Jitsi repository, add a Let's Encrypt certificate, open the firewall, require login and check the services.

  • Linux & Systems
4 min read

SSH keys

Create and manage SSH keys: generate a key pair, set a passphrase, copy the public key, test Git hosts and recover a server you are locked out of.

  • Security
  • Linux & Systems

Type to search the whole site.