Serial Port Simulator in Linux


Linux is a robust and proven opensource operating system for almost a decade now. Being a Microsoft fan, I used to ignore and try to pretend that Linux didn’t exist. But now am forced to move some of my important processes to linux.

While writing one such essential program in linux, I bumped on an issue of simulating a com port in linux. Windows has lot of emulator programs which we can google and download. But lot of such search in linux yielded a much better simple and effective result. Just thought will be useful to share this tip for people to search easily.

Com Port Simulator in Linux

The above can also be termed as Serial Port simulator or Virtual Serial Port. In Linux all your com ports are listed as tty0,tty1..ttyN or ttyS0,ttyS1.. ttySN under the /dev folder. To check such devices type

$ ls /dev/*

The above command will list all devices which are connected and not-yet-connected.
Now if you want to generate a Virtual Serial port, there is a very simple and powerful utility in linux called “socat”. You can install it in Fedora with the following command.

$ yum install socat*

My fedora distribution caught hold of socat.i686 and installed it on my dual boot laptop. Please note that for running the “yum” installer, you need to be connected to internet on the Fedora box.

Configuring socat to listen to a Serial port and forward it to TCP Server

Let us assume we want to create a virtual serial port on ttyS2 and forward it to a TCP/IP Server listening at port 12321 on the same PC. Please make sure you also login using super user context by issuing a command su on the terminal.

$su 
$socat PTY,link=/dev/ttyS2,raw,wait-slave TCP4:127.0.0.1:12321

Then run a small TCP Server utility listening for connections at port 12321.

Now you can try using the tool putty, for connecting to the serial port at /dev/ttyS2 and send some command. This command will be displayed on the TCP utility and whatever you try to send from the TCP Utility will be displayed on the putty terminal.

Hope this helps saving a few hours searching for a Virtual Serial Port aka tty simulator aka Com Port Simulator..