Wednesday, 25 December 2013, 3.55PM
As an electronics engineer, and even while doing my projects as a student, I have often had to use serial ports for communication between computer and a serial device. So, while testing my code for the device, I have often felt a need for a virtual serial port pair connected in null modem connection.
Initially, I used tty0tty library from this site. But, though I didn’t have any problems with it myself, I read that it had some stability issues and was not being supported by many developers.
Recently, however, I discovered socat.
It is a simple command line tool which has fairly good documentation and is constantly being developed.You can install it through your package manager or by compiling the source code, instructions for both of which are easily available on the internet.
Now, using socat, all you need to do to create a virtual serial port pair is this
$ socat -d -d pty,raw,echo=0 pty,raw,echo=0
You will get an output which is somewhat like this:
2013/12/25 15:44:52 socat N PTY is /dev/pts/3
2013/12/25 15:44:52 socat N PTY is /dev/pts/4
2013/12/25 15:44:52 socat N starting data transfer loop with FDs [3,3] and [5,5]
Now, you have two ports /dev/pts/3 and /dev/pts/4 which have a null modem connection between them. So, everythong you send to one port will be received by the other.
You can test the connection by listening to one port and sending test data on the other. For this, open a new terminal and type
$ cat < /dev/pts/4
This will listen to port /dev/pts/4. Now, to send some data from port 3, open another terminal and type
$ echo “send to 3” > /dev/pts/3
That message should appear at the terminal which is listening to port 4.
I have tested this connection using pyserial module in python, and so far I have no complaints.
This is how it looked like in my window: