Creating a Virtual Serial Port in Linux

When developing software that uses a serial port for communication with the outside world it is really helpful to be able to use a virtual serial port and write test code to aid in debugging. Well it turns out there is a program that does that in Linux, it’s called socat. Using this utility you can create a pair of virtual serial ports and connect them together. To install it in Ubuntu run the following commands.

sudo apt-get update
sudo apt-get install socat

Once you have the socat program installed you just need to run the following command to create a pair of virtual tty devices. When you run the program it will print out which ports you need to use.

socat -d -d PTY,b57600 PTY,link=ttyVS1,b57600

This is an example of the output that socat produces. The two ports it has created are /dev/pts/5 and /dev/pts/6. These can vary from time to time so keep an eye on that.

2013/11/15 07:47:13 socat[2970] N PTY is /dev/pts/5
2013/11/15 07:47:13 socat[2970] N PTY is /dev/pts/6
2013/11/15 07:47:13 socat[2970] N starting data transfer loop with FDs [3,3] and [5,5]